IntegrationTestCaseTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.0.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->assertResponseNotEmpty();
  120. $this->assertResponseContains('Not there or here');
  121. $this->assertResponseContains('<!DOCTYPE html>');
  122. }
  123. /**
  124. * Test redirecting and integration tests.
  125. *
  126. * @return void
  127. */
  128. public function testRedirect()
  129. {
  130. $this->post('/tests_apps/redirect_to');
  131. $this->assertResponseSuccess();
  132. $this->assertResponseCode(302);
  133. }
  134. /**
  135. * Test redirecting and integration tests.
  136. *
  137. * @return void
  138. */
  139. public function testRedirectPermanent()
  140. {
  141. $this->post('/tests_apps/redirect_to_permanent');
  142. $this->assertResponseSuccess();
  143. $this->assertResponseCode(301);
  144. }
  145. /**
  146. * Test the responseOk status assertion
  147. *
  148. * @return void
  149. */
  150. public function testAssertResponseStatusCodes()
  151. {
  152. $this->_response = new Response();
  153. $this->_response->statusCode(200);
  154. $this->assertResponseOk();
  155. $this->_response->statusCode(201);
  156. $this->assertResponseOk();
  157. $this->_response->statusCode(204);
  158. $this->assertResponseOk();
  159. $this->_response->statusCode(202);
  160. $this->assertResponseSuccess();
  161. $this->_response->statusCode(302);
  162. $this->assertResponseSuccess();
  163. $this->_response->statusCode(400);
  164. $this->assertResponseError();
  165. $this->_response->statusCode(417);
  166. $this->assertResponseError();
  167. $this->_response->statusCode(500);
  168. $this->assertResponseFailure();
  169. $this->_response->statusCode(505);
  170. $this->assertResponseFailure();
  171. $this->_response->statusCode(301);
  172. $this->assertResponseCode(301);
  173. }
  174. /**
  175. * Test the location header assertion.
  176. *
  177. * @return void
  178. */
  179. public function testAssertRedirect()
  180. {
  181. $this->_response = new Response();
  182. $this->_response->header('Location', 'http://localhost/tasks/index');
  183. $this->assertRedirect();
  184. $this->assertRedirect('/tasks/index');
  185. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  186. $this->assertResponseEmpty();
  187. }
  188. /**
  189. * Test the location header assertion.
  190. *
  191. * @return void
  192. */
  193. public function testAssertNoRedirect()
  194. {
  195. $this->_response = new Response();
  196. $this->assertNoRedirect();
  197. }
  198. /**
  199. * Test the location header assertion.
  200. *
  201. * @return void
  202. */
  203. public function testAssertNoRedirectFail()
  204. {
  205. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  206. $result = $test->run();
  207. ob_start();
  208. $this->assertFalse($result->wasSuccessful());
  209. $this->assertEquals(1, $result->failureCount());
  210. }
  211. /**
  212. * Test the location header assertion string contains
  213. *
  214. * @return void
  215. */
  216. public function testAssertRedirectContains()
  217. {
  218. $this->_response = new Response();
  219. $this->_response->header('Location', 'http://localhost/tasks/index');
  220. $this->assertRedirectContains('/tasks/index');
  221. }
  222. /**
  223. * Test the header assertion.
  224. *
  225. * @return void
  226. */
  227. public function testAssertHeader()
  228. {
  229. $this->_response = new Response();
  230. $this->_response->header('Etag', 'abc123');
  231. $this->assertHeader('Etag', 'abc123');
  232. }
  233. /**
  234. * Test the content type assertion.
  235. *
  236. * @return void
  237. */
  238. public function testAssertContentType()
  239. {
  240. $this->_response = new Response();
  241. $this->_response->type('json');
  242. $this->assertContentType('json');
  243. $this->assertContentType('application/json');
  244. }
  245. /**
  246. * Test the content assertion.
  247. *
  248. * @return void
  249. */
  250. public function testAssertResponseContains()
  251. {
  252. $this->_response = new Response();
  253. $this->_response->body('Some content');
  254. $this->assertResponseContains('content');
  255. }
  256. /**
  257. * Test the negated content assertion.
  258. *
  259. * @return void
  260. */
  261. public function testAssertResponseNotContains()
  262. {
  263. $this->_response = new Response();
  264. $this->_response->body('Some content');
  265. $this->assertResponseNotContains('contents');
  266. }
  267. /**
  268. * Test that works in tandem with testEventManagerReset2 to
  269. * test the EventManager reset.
  270. *
  271. * The return value is passed to testEventManagerReset2 as
  272. * an arguments.
  273. *
  274. * @return \Cake\Event\EventManager
  275. */
  276. public function testEventManagerReset1()
  277. {
  278. return EventManager::instance();
  279. }
  280. /**
  281. * Test if the EventManager is reset between tests.
  282. *
  283. * @depends testEventManagerReset1
  284. * @return void
  285. */
  286. public function testEventManagerReset2($prevEventManager)
  287. {
  288. $this->assertNotSame($prevEventManager, EventManager::instance());
  289. }
  290. }