IntegrationTestCaseTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Core\Configure;
  17. use Cake\Event\EventManager;
  18. use Cake\Network\Response;
  19. use Cake\Routing\DispatcherFactory;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\IntegrationTestCase;
  22. use Cake\Test\Fixture\AssertIntegrationTestCase;
  23. /**
  24. * Self test of the IntegrationTestCase
  25. */
  26. class IntegrationTestCaseTest extends IntegrationTestCase
  27. {
  28. /**
  29. * Setup method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. Configure::write('App.namespace', 'TestApp');
  37. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  38. DispatcherFactory::clear();
  39. DispatcherFactory::add('Routing');
  40. DispatcherFactory::add('ControllerFactory');
  41. }
  42. /**
  43. * Test building a request.
  44. *
  45. * @return void
  46. */
  47. public function testRequestBuilding()
  48. {
  49. $this->configRequest([
  50. 'headers' => ['X-CSRF-Token' => 'abc123'],
  51. 'base' => '',
  52. 'webroot' => '/',
  53. 'environment' => [
  54. 'PHP_AUTH_USER' => 'foo',
  55. 'PHP_AUTH_PW' => 'bar'
  56. ]
  57. ]);
  58. $this->cookie('split_token', 'def345');
  59. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  60. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  61. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  62. $this->assertEquals('tasks/add', $request->url);
  63. $this->assertEquals(['split_token' => 'def345'], $request->cookies);
  64. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  65. $this->assertEquals('foo', $request->env('PHP_AUTH_USER'));
  66. $this->assertEquals('bar', $request->env('PHP_AUTH_PW'));
  67. }
  68. /**
  69. * Test building a request, with query parameters
  70. *
  71. * @return void
  72. */
  73. public function testRequestBuildingQueryParameters()
  74. {
  75. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  76. $this->assertEquals('/tasks/view?archived=yes', $request->here());
  77. $this->assertEquals('yes', $request->query('archived'));
  78. }
  79. /**
  80. * Test sending get requests.
  81. *
  82. * @return void
  83. */
  84. public function testGet()
  85. {
  86. $this->assertNull($this->_response);
  87. $this->get('/request_action/test_request_action');
  88. $this->assertNotEmpty($this->_response);
  89. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  90. $this->assertEquals('This is a test', $this->_response->body());
  91. }
  92. /**
  93. * Test sending requests stores references to controller/view/layout.
  94. *
  95. * @return void
  96. */
  97. public function testRequestSetsProperties()
  98. {
  99. $this->post('/posts/index');
  100. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  101. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  102. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  103. $this->assertTemplate('index');
  104. $this->assertLayout('default');
  105. $this->assertEquals('value', $this->viewVariable('test'));
  106. }
  107. /**
  108. * Assert that the stored template doesn't change when cells are rendered.
  109. *
  110. * @return void
  111. */
  112. public function testAssertTemplateAfterCellRender()
  113. {
  114. $this->get('/posts/get');
  115. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  116. $this->assertTemplate('get');
  117. $this->assertResponseContains('cellcontent');
  118. }
  119. /**
  120. * Test array URLs
  121. *
  122. * @return void
  123. */
  124. public function testArrayUrls()
  125. {
  126. $this->post(['controller' => 'Posts', 'action' => 'index']);
  127. $this->assertEquals('value', $this->viewVariable('test'));
  128. }
  129. /**
  130. * Test flash and cookie assertions
  131. *
  132. * @return void
  133. */
  134. public function testFlashSessionAndCookieAsserts()
  135. {
  136. $this->post('/posts/index');
  137. $this->assertSession('An error message', 'Flash.flash.message');
  138. $this->assertCookie(1, 'remember_me');
  139. }
  140. /**
  141. * Test error handling and error page rendering.
  142. *
  143. * @return void
  144. */
  145. public function testPostAndErrorHandling()
  146. {
  147. $this->post('/request_action/error_method');
  148. $this->assertResponseNotEmpty();
  149. $this->assertResponseContains('Not there or here');
  150. $this->assertResponseContains('<!DOCTYPE html>');
  151. }
  152. /**
  153. * Test that exceptions being thrown are handled correctly.
  154. *
  155. * @return void
  156. */
  157. public function testWithExpectedException()
  158. {
  159. $this->get('/tests_apps/throw_exception');
  160. $this->assertResponseCode(500);
  161. }
  162. /**
  163. * Test that exceptions being thrown are handled correctly.
  164. *
  165. * @expectedException PHPUnit_Framework_AssertionFailedError
  166. * @return void
  167. */
  168. public function testWithUnexpectedException()
  169. {
  170. $this->get('/tests_apps/throw_exception');
  171. $this->assertResponseCode(501);
  172. }
  173. /**
  174. * Test redirecting and integration tests.
  175. *
  176. * @return void
  177. */
  178. public function testRedirect()
  179. {
  180. $this->post('/tests_apps/redirect_to');
  181. $this->assertResponseSuccess();
  182. $this->assertResponseCode(302);
  183. }
  184. /**
  185. * Test redirecting and integration tests.
  186. *
  187. * @return void
  188. */
  189. public function testRedirectPermanent()
  190. {
  191. $this->post('/tests_apps/redirect_to_permanent');
  192. $this->assertResponseSuccess();
  193. $this->assertResponseCode(301);
  194. }
  195. /**
  196. * Test the responseOk status assertion
  197. *
  198. * @return void
  199. */
  200. public function testAssertResponseStatusCodes()
  201. {
  202. $this->_response = new Response();
  203. $this->_response->statusCode(200);
  204. $this->assertResponseOk();
  205. $this->_response->statusCode(201);
  206. $this->assertResponseOk();
  207. $this->_response->statusCode(204);
  208. $this->assertResponseOk();
  209. $this->_response->statusCode(202);
  210. $this->assertResponseSuccess();
  211. $this->_response->statusCode(302);
  212. $this->assertResponseSuccess();
  213. $this->_response->statusCode(400);
  214. $this->assertResponseError();
  215. $this->_response->statusCode(417);
  216. $this->assertResponseError();
  217. $this->_response->statusCode(500);
  218. $this->assertResponseFailure();
  219. $this->_response->statusCode(505);
  220. $this->assertResponseFailure();
  221. $this->_response->statusCode(301);
  222. $this->assertResponseCode(301);
  223. }
  224. /**
  225. * Test the location header assertion.
  226. *
  227. * @return void
  228. */
  229. public function testAssertRedirect()
  230. {
  231. $this->_response = new Response();
  232. $this->_response->header('Location', 'http://localhost/tasks/index');
  233. $this->assertRedirect();
  234. $this->assertRedirect('/tasks/index');
  235. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  236. $this->assertResponseEmpty();
  237. }
  238. /**
  239. * Test the location header assertion.
  240. *
  241. * @return void
  242. */
  243. public function testAssertNoRedirect()
  244. {
  245. $this->_response = new Response();
  246. $this->assertNoRedirect();
  247. }
  248. /**
  249. * Test the location header assertion.
  250. *
  251. * @return void
  252. */
  253. public function testAssertNoRedirectFail()
  254. {
  255. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  256. $result = $test->run();
  257. ob_start();
  258. $this->assertFalse($result->wasSuccessful());
  259. $this->assertEquals(1, $result->failureCount());
  260. }
  261. /**
  262. * Test the location header assertion string contains
  263. *
  264. * @return void
  265. */
  266. public function testAssertRedirectContains()
  267. {
  268. $this->_response = new Response();
  269. $this->_response->header('Location', 'http://localhost/tasks/index');
  270. $this->assertRedirectContains('/tasks/index');
  271. }
  272. /**
  273. * Test the header assertion.
  274. *
  275. * @return void
  276. */
  277. public function testAssertHeader()
  278. {
  279. $this->_response = new Response();
  280. $this->_response->header('Etag', 'abc123');
  281. $this->assertHeader('Etag', 'abc123');
  282. }
  283. /**
  284. * Test the content type assertion.
  285. *
  286. * @return void
  287. */
  288. public function testAssertContentType()
  289. {
  290. $this->_response = new Response();
  291. $this->_response->type('json');
  292. $this->assertContentType('json');
  293. $this->assertContentType('application/json');
  294. }
  295. /**
  296. * Test that type() in an action sets the content-type header.
  297. *
  298. * @return void
  299. */
  300. public function testContentTypeInAction()
  301. {
  302. $this->get('/tests_apps/set_type');
  303. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  304. $this->assertContentType('json');
  305. $this->assertContentType('application/json');
  306. }
  307. /**
  308. * Test the content assertion.
  309. *
  310. * @return void
  311. */
  312. public function testAssertResponseContains()
  313. {
  314. $this->_response = new Response();
  315. $this->_response->body('Some content');
  316. $this->assertResponseContains('content');
  317. }
  318. /**
  319. * Test the negated content assertion.
  320. *
  321. * @return void
  322. */
  323. public function testAssertResponseNotContains()
  324. {
  325. $this->_response = new Response();
  326. $this->_response->body('Some content');
  327. $this->assertResponseNotContains('contents');
  328. }
  329. /**
  330. * Test that works in tandem with testEventManagerReset2 to
  331. * test the EventManager reset.
  332. *
  333. * The return value is passed to testEventManagerReset2 as
  334. * an arguments.
  335. *
  336. * @return \Cake\Event\EventManager
  337. */
  338. public function testEventManagerReset1()
  339. {
  340. return EventManager::instance();
  341. }
  342. /**
  343. * Test if the EventManager is reset between tests.
  344. *
  345. * @depends testEventManagerReset1
  346. * @return void
  347. */
  348. public function testEventManagerReset2($prevEventManager)
  349. {
  350. $this->assertNotSame($prevEventManager, EventManager::instance());
  351. }
  352. }