IntegrationTestCaseTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. ]);
  54. $this->cookie('split_token', 'def345');
  55. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  56. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  57. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  58. $this->assertEquals('tasks/add', $request->url);
  59. $this->assertEquals(['split_token' => 'def345'], $request->cookies);
  60. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  61. }
  62. /**
  63. * Test sending get requests.
  64. *
  65. * @return void
  66. */
  67. public function testGet()
  68. {
  69. $this->assertNull($this->_response);
  70. $this->get('/request_action/test_request_action');
  71. $this->assertNotEmpty($this->_response);
  72. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  73. $this->assertEquals('This is a test', $this->_response->body());
  74. }
  75. /**
  76. * Test sending requests stores references to controller/view/layout.
  77. *
  78. * @return void
  79. */
  80. public function testRequestSetsProperties()
  81. {
  82. $this->post('/posts/index');
  83. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  84. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  85. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  86. $this->assertTemplate('index');
  87. $this->assertLayout('default');
  88. $this->assertEquals('value', $this->viewVariable('test'));
  89. }
  90. /**
  91. * Test array URLs
  92. *
  93. * @return void
  94. */
  95. public function testArrayUrls()
  96. {
  97. $this->post(['controller' => 'Posts', 'action' => 'index']);
  98. $this->assertEquals('value', $this->viewVariable('test'));
  99. }
  100. /**
  101. * Test flash and cookie assertions
  102. *
  103. * @return void
  104. */
  105. public function testFlashSessionAndCookieAsserts()
  106. {
  107. $this->post('/posts/index');
  108. $this->assertSession('An error message', 'Flash.flash.message');
  109. $this->assertCookie(1, 'remember_me');
  110. }
  111. /**
  112. * Test error handling and error page rendering.
  113. *
  114. * @return void
  115. */
  116. public function testPostAndErrorHandling()
  117. {
  118. $this->post('/request_action/error_method');
  119. $this->assertResponseContains('Not there or here');
  120. $this->assertResponseContains('<!DOCTYPE html>');
  121. }
  122. /**
  123. * Test redirecting and integration tests.
  124. *
  125. * @return void
  126. */
  127. public function testRedirect()
  128. {
  129. $this->post('/tests_apps/redirect_to');
  130. $this->assertResponseOk();
  131. $this->assertResponseCode(302);
  132. }
  133. /**
  134. * Test redirecting and integration tests.
  135. *
  136. * @return void
  137. */
  138. public function testRedirectPermanent()
  139. {
  140. $this->post('/tests_apps/redirect_to_permanent');
  141. $this->assertResponseOk();
  142. $this->assertResponseCode(301);
  143. }
  144. /**
  145. * Test the responseOk status assertion
  146. *
  147. * @return void
  148. */
  149. public function testAssertResponseStatusCodes()
  150. {
  151. $this->_response = new Response();
  152. $this->_response->statusCode(200);
  153. $this->assertResponseOk();
  154. $this->_response->statusCode(201);
  155. $this->assertResponseOk();
  156. $this->_response->statusCode(204);
  157. $this->assertResponseOk();
  158. $this->_response->statusCode(400);
  159. $this->assertResponseError();
  160. $this->_response->statusCode(417);
  161. $this->assertResponseError();
  162. $this->_response->statusCode(500);
  163. $this->assertResponseFailure();
  164. $this->_response->statusCode(505);
  165. $this->assertResponseFailure();
  166. $this->_response->statusCode(301);
  167. $this->assertResponseCode(301);
  168. }
  169. /**
  170. * Test the location header assertion.
  171. *
  172. * @return void
  173. */
  174. public function testAssertRedirect()
  175. {
  176. $this->_response = new Response();
  177. $this->_response->header('Location', 'http://localhost/tasks/index');
  178. $this->assertRedirect();
  179. $this->assertRedirect('/tasks/index');
  180. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  181. }
  182. /**
  183. * Test the location header assertion.
  184. *
  185. * @return void
  186. */
  187. public function testAssertNoRedirect()
  188. {
  189. $this->_response = new Response();
  190. $this->assertNoRedirect();
  191. }
  192. /**
  193. * Test the location header assertion.
  194. *
  195. * @return void
  196. */
  197. public function testAssertNoRedirectFail()
  198. {
  199. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  200. $result = $test->run();
  201. ob_start();
  202. $this->assertFalse($result->wasSuccessful());
  203. $this->assertEquals(1, $result->failureCount());
  204. }
  205. /**
  206. * Test the header assertion.
  207. *
  208. * @return void
  209. */
  210. public function testAssertHeader()
  211. {
  212. $this->_response = new Response();
  213. $this->_response->header('Etag', 'abc123');
  214. $this->assertHeader('Etag', 'abc123');
  215. }
  216. /**
  217. * Test the content type assertion.
  218. *
  219. * @return void
  220. */
  221. public function testAssertContentType()
  222. {
  223. $this->_response = new Response();
  224. $this->_response->type('json');
  225. $this->assertContentType('json');
  226. $this->assertContentType('application/json');
  227. }
  228. /**
  229. * Test the content assertion.
  230. *
  231. * @return void
  232. */
  233. public function testAssertResponseContains()
  234. {
  235. $this->_response = new Response();
  236. $this->_response->body('Some content');
  237. $this->assertResponseContains('content');
  238. }
  239. /**
  240. * Test the negated content assertion.
  241. *
  242. * @return void
  243. */
  244. public function testAssertResponseNotContains()
  245. {
  246. $this->_response = new Response();
  247. $this->_response->body('Some content');
  248. $this->assertResponseNotContains('contents');
  249. }
  250. /**
  251. * Test that works in tandem with testEventManagerReset2 to
  252. * test the EventManager reset.
  253. *
  254. * The return value is passed to testEventManagerReset2 as
  255. * an arguments.
  256. *
  257. * @return \Cake\Event\EventManager
  258. */
  259. public function testEventManagerReset1()
  260. {
  261. return EventManager::instance();
  262. }
  263. /**
  264. * Test if the EventManager is reset between tests.
  265. *
  266. * @depends testEventManagerReset1
  267. * @return void
  268. */
  269. public function testEventManagerReset2($prevEventManager)
  270. {
  271. $this->assertNotSame($prevEventManager, EventManager::instance());
  272. }
  273. }