PhpBrowserRestTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. use Codeception\Test\Unit;
  3. use Codeception\Util\Stub as Stub;
  4. class PhpBrowserRestTest extends Unit
  5. {
  6. /**
  7. * @var \Codeception\Module\REST
  8. */
  9. protected $module;
  10. /**
  11. * @var \Codeception\Module\PhpBrowser
  12. */
  13. protected $phpBrowser;
  14. public function setUp()
  15. {
  16. $this->phpBrowser = new \Codeception\Module\PhpBrowser(make_container());
  17. $url = 'http://localhost:8010';
  18. $this->phpBrowser->_setConfig(['url' => $url]);
  19. $this->phpBrowser->_initialize();
  20. $this->module = Stub::make('\Codeception\Module\REST');
  21. $this->module->_inject($this->phpBrowser);
  22. $this->module->_initialize();
  23. $this->module->_before(Stub::makeEmpty('\Codeception\Test\Cest'));
  24. $this->phpBrowser->_before(Stub::makeEmpty('\Codeception\Test\Cest'));
  25. }
  26. private function setStubResponse($response)
  27. {
  28. $this->phpBrowser = Stub::make('\Codeception\Module\PhpBrowser', ['_getResponseContent' => $response]);
  29. $this->module->_inject($this->phpBrowser);
  30. $this->module->_initialize();
  31. $this->module->_before(Stub::makeEmpty('\Codeception\Test\Cest'));
  32. }
  33. public function testGet()
  34. {
  35. $this->module->sendGET('/rest/user/');
  36. $this->module->seeResponseIsJson();
  37. $this->module->seeResponseContains('davert');
  38. $this->module->seeResponseContainsJson(['name' => 'davert']);
  39. $this->module->seeResponseCodeIs(200);
  40. $this->module->dontSeeResponseCodeIs(404);
  41. }
  42. public function testSendAbsoluteUrlGet()
  43. {
  44. $this->module->sendGET('http://127.0.0.1:8010/rest/user/');
  45. $this->module->seeResponseCodeIs(200);
  46. }
  47. public function testPost()
  48. {
  49. $this->module->sendPOST('/rest/user/', ['name' => 'john']);
  50. $this->module->seeResponseContains('john');
  51. $this->module->seeResponseContainsJson(['name' => 'john']);
  52. }
  53. public function testValidJson()
  54. {
  55. $this->setStubResponse('{"xxx": "yyy"}');
  56. $this->module->seeResponseIsJson();
  57. $this->setStubResponse('{"xxx": "yyy", "zzz": ["a","b"]}');
  58. $this->module->seeResponseIsJson();
  59. $this->module->seeResponseEquals('{"xxx": "yyy", "zzz": ["a","b"]}');
  60. }
  61. public function testInvalidJson()
  62. {
  63. $this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
  64. $this->setStubResponse('{xxx = yyy}');
  65. $this->module->seeResponseIsJson();
  66. }
  67. public function testValidXml()
  68. {
  69. $this->setStubResponse('<xml></xml>');
  70. $this->module->seeResponseIsXml();
  71. $this->setStubResponse('<xml><name>John</name></xml>');
  72. $this->module->seeResponseIsXml();
  73. $this->module->seeResponseEquals('<xml><name>John</name></xml>');
  74. }
  75. public function testInvalidXml()
  76. {
  77. $this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
  78. $this->setStubResponse('<xml><name>John</surname></xml>');
  79. $this->module->seeResponseIsXml();
  80. }
  81. public function testSeeInJson()
  82. {
  83. $this->setStubResponse(
  84. '{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}, "labels": null}}'
  85. );
  86. $this->module->seeResponseIsJson();
  87. $this->module->seeResponseContainsJson(['name' => 'Davert']);
  88. $this->module->seeResponseContainsJson(['user' => ['name' => 'Davert']]);
  89. $this->module->seeResponseContainsJson(['ticket' => ['title' => 'Bug should be fixed']]);
  90. $this->module->seeResponseContainsJson(['ticket' => ['user' => ['name' => 'Davert']]]);
  91. $this->module->seeResponseContainsJson(array('ticket' => array('labels' => null)));
  92. }
  93. public function testSeeInJsonCollection()
  94. {
  95. $this->setStubResponse(
  96. '[{"user":"Blacknoir","age":27,"tags":["wed-dev","php"]},'
  97. . '{"user":"John Doe","age":27,"tags":["web-dev","java"]}]'
  98. );
  99. $this->module->seeResponseIsJson();
  100. $this->module->seeResponseContainsJson(array('tags' => array('web-dev', 'java')));
  101. $this->module->seeResponseContainsJson(array('user' => 'John Doe', 'age' => 27));
  102. }
  103. public function testArrayJson()
  104. {
  105. $this->setStubResponse(
  106. '[{"id":1,"title": "Bug should be fixed"},{"title": "Feature should be implemented","id":2}]'
  107. );
  108. $this->module->seeResponseContainsJson(array('id' => 1));
  109. }
  110. /**
  111. * @issue https://github.com/Codeception/Codeception/issues/4202
  112. */
  113. public function testSeeResponseContainsJsonFailsGracefullyWhenJsonResultIsNotArray()
  114. {
  115. $this->shouldFail();
  116. $this->setStubResponse(json_encode('no_status'));
  117. $this->module->seeResponseContainsJson(array('id' => 1));
  118. }
  119. public function testDontSeeResponseJsonMatchesJsonPathPassesWhenJsonResultIsNotArray()
  120. {
  121. $this->setStubResponse(json_encode('no_status'));
  122. $this->module->dontSeeResponseJsonMatchesJsonPath('$.error');
  123. }
  124. public function testDontSeeInJson()
  125. {
  126. $this->setStubResponse('{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}}}');
  127. $this->module->seeResponseIsJson();
  128. $this->module->dontSeeResponseContainsJson(array('name' => 'Davet'));
  129. $this->module->dontSeeResponseContainsJson(array('user' => array('name' => 'Davet')));
  130. $this->module->dontSeeResponseContainsJson(array('user' => array('title' => 'Bug should be fixed')));
  131. }
  132. public function testApplicationJsonIncludesJsonAsContent()
  133. {
  134. $this->module->haveHttpHeader('Content-Type', 'application/json');
  135. $this->module->sendPOST('/', array('name' => 'john'));
  136. /** @var $request \Symfony\Component\BrowserKit\Request **/
  137. $request = $this->module->client->getRequest();
  138. $this->assertContains('application/json', $request->getServer());
  139. $server = $request->getServer();
  140. $this->assertEquals('application/json', $server['HTTP_CONTENT_TYPE']);
  141. $this->assertJson($request->getContent());
  142. $this->assertEmpty($request->getParameters());
  143. }
  144. /**
  145. * @issue https://github.com/Codeception/Codeception/issues/3516
  146. */
  147. public function testApplicationJsonHeaderCheckIsCaseInsensitive()
  148. {
  149. $this->module->haveHttpHeader('content-type', 'application/json');
  150. $this->module->sendPOST('/', array('name' => 'john'));
  151. /** @var $request \Symfony\Component\BrowserKit\Request **/
  152. $request = $this->module->client->getRequest();
  153. $server = $request->getServer();
  154. $this->assertEquals('application/json', $server['HTTP_CONTENT_TYPE']);
  155. $this->assertJson($request->getContent());
  156. $this->assertEmpty($request->getParameters());
  157. }
  158. public function testGetApplicationJsonNotIncludesJsonAsContent()
  159. {
  160. $this->module->haveHttpHeader('Content-Type', 'application/json');
  161. $this->module->sendGET('/', array('name' => 'john'));
  162. /** @var $request \Symfony\Component\BrowserKit\Request **/
  163. $request = $this->module->client->getRequest();
  164. $this->assertNull($request->getContent());
  165. $this->assertContains('john', $request->getParameters());
  166. }
  167. /**
  168. * @Issue https://github.com/Codeception/Codeception/issues/2075
  169. * Client is undefined for the second test
  170. */
  171. public function testTwoTests()
  172. {
  173. $cest1 = Stub::makeEmpty('\Codeception\Test\Cest');
  174. $cest2 = Stub::makeEmpty('\Codeception\Test\Cest');
  175. $this->module->sendGET('/rest/user/');
  176. $this->module->seeResponseIsJson();
  177. $this->module->seeResponseContains('davert');
  178. $this->module->seeResponseContainsJson(array('name' => 'davert'));
  179. $this->module->seeResponseCodeIs(200);
  180. $this->module->dontSeeResponseCodeIs(404);
  181. $this->phpBrowser->_after($cest1);
  182. $this->module->_after($cest1);
  183. $this->module->_before($cest2);
  184. $this->phpBrowser->_before($cest2);
  185. $this->module->sendGET('/rest/user/');
  186. $this->module->seeResponseIsJson();
  187. $this->module->seeResponseContains('davert');
  188. $this->module->seeResponseContainsJson(array('name' => 'davert'));
  189. $this->module->seeResponseCodeIs(200);
  190. $this->module->dontSeeResponseCodeIs(404);
  191. }
  192. /**
  193. * @Issue https://github.com/Codeception/Codeception/issues/2070
  194. */
  195. public function testArrayOfZeroesInJsonResponse()
  196. {
  197. $this->module->haveHttpHeader('Content-Type', 'application/json');
  198. $this->module->sendGET('/rest/zeroes');
  199. $this->module->dontSeeResponseContainsJson([
  200. 'responseCode' => 0,
  201. 'data' => [
  202. 0,
  203. 0,
  204. 0,
  205. ]
  206. ]);
  207. }
  208. public function testFileUploadWithKeyValueArray()
  209. {
  210. $tmpFileName = tempnam('/tmp', 'test_');
  211. file_put_contents($tmpFileName, 'test data');
  212. $files = [
  213. 'file' => $tmpFileName,
  214. ];
  215. $this->module->sendPOST('/rest/file-upload', [], $files);
  216. $this->module->seeResponseContainsJson([
  217. 'uploaded' => true,
  218. ]);
  219. }
  220. public function testFileUploadWithFilesArray()
  221. {
  222. $tmpFileName = tempnam('/tmp', 'test_');
  223. file_put_contents($tmpFileName, 'test data');
  224. $files = [
  225. 'file' => [
  226. 'name' => 'file.txt',
  227. 'type' => 'text/plain',
  228. 'size' => 9,
  229. 'tmp_name' => $tmpFileName,
  230. ]
  231. ];
  232. $this->module->sendPOST('/rest/file-upload', [], $files);
  233. $this->module->seeResponseContainsJson([
  234. 'uploaded' => true,
  235. ]);
  236. }
  237. public function testCanInspectResultOfPhpBrowserRequest()
  238. {
  239. $this->phpBrowser->amOnPage('/rest/user/');
  240. $this->module->seeResponseCodeIs(200);
  241. $this->module->seeResponseIsJson();
  242. }
  243. /**
  244. * @Issue 4203 https://github.com/Codeception/Codeception/issues/4203
  245. */
  246. public function testSessionHeaderBackup()
  247. {
  248. $this->module->haveHttpHeader('foo', 'bar');
  249. $this->module->sendGET('/rest/foo/');
  250. $this->module->seeResponseContains('foo: "bar"');
  251. $session = $this->phpBrowser->_backupSession();
  252. $this->module->haveHttpHeader('foo', 'baz');
  253. $this->module->sendGET('/rest/foo/');
  254. $this->module->seeResponseContains('foo: "baz"');
  255. $this->phpBrowser->_loadSession($session);
  256. $this->module->sendGET('/rest/foo/');
  257. $this->module->seeResponseContains('foo: "bar"');
  258. }
  259. protected function shouldFail()
  260. {
  261. $this->setExpectedException('PHPUnit\Framework\AssertionFailedError');
  262. }
  263. public function testGrabFromCurrentUrl()
  264. {
  265. $this->module->sendGET('/rest/foo/');
  266. $this->assertEquals('/rest/foo/', $this->phpBrowser->grabFromCurrentUrl());
  267. }
  268. }