IntegrationTestCaseTest.php 7.9 KB

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