PhpBrowserRestTest.php 10 KB

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