IntegrationTestCaseTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. * Setup method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. Configure::write('App.namespace', 'TestApp');
  35. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  36. DispatcherFactory::clear();
  37. DispatcherFactory::add('Routing');
  38. DispatcherFactory::add('ControllerFactory');
  39. }
  40. /**
  41. * Test building a request.
  42. *
  43. * @return void
  44. */
  45. public function testRequestBuilding() {
  46. $this->configRequest([
  47. 'headers' => ['X-CSRF-Token' => 'abc123'],
  48. 'base' => '',
  49. 'webroot' => '/'
  50. ]);
  51. $this->cookie('split_token', 'def345');
  52. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  53. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  54. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  55. $this->assertEquals('tasks/add', $request->url);
  56. $this->assertEquals(['split_token' => 'def345'], $request->cookies);
  57. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  58. }
  59. /**
  60. * Test sending get requests.
  61. *
  62. * @return void
  63. */
  64. public function testGet() {
  65. $this->assertNull($this->_response);
  66. $this->get('/request_action/test_request_action');
  67. $this->assertNotEmpty($this->_response);
  68. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  69. $this->assertEquals('This is a test', $this->_response->body());
  70. }
  71. /**
  72. * Test sending requests stores references to controller/view/layout.
  73. *
  74. * @return void
  75. */
  76. public function testRequestSetsProperties() {
  77. $this->post('/posts/index');
  78. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  79. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  80. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  81. $this->assertTemplate('index');
  82. $this->assertLayout('default');
  83. $this->assertEquals('value', $this->viewVariable('test'));
  84. }
  85. /**
  86. * Test array URLs
  87. *
  88. * @return void
  89. */
  90. public function testArrayUrls() {
  91. $this->post(['controller' => 'Posts', 'action' => 'index']);
  92. $this->assertEquals('value', $this->viewVariable('test'));
  93. }
  94. /**
  95. * Test flash and cookie assertions
  96. *
  97. * @return void
  98. */
  99. public function testFlashSessionAndCookieAsserts() {
  100. $this->post('/posts/index');
  101. $this->assertSession('An error message', 'Flash.flash.message');
  102. $this->assertCookie(1, 'remember_me');
  103. }
  104. /**
  105. * Test error handling and error page rendering.
  106. *
  107. * @return void
  108. */
  109. public function testPostAndErrorHandling() {
  110. $this->post('/request_action/error_method');
  111. $this->assertResponseContains('Not there or here');
  112. $this->assertResponseContains('<!DOCTYPE html>');
  113. }
  114. /**
  115. * Test the responseOk status assertion
  116. *
  117. * @return void
  118. */
  119. public function testAssertResponseStatusCodes() {
  120. $this->_response = new Response();
  121. $this->_response->statusCode(200);
  122. $this->assertResponseOk();
  123. $this->_response->statusCode(201);
  124. $this->assertResponseOk();
  125. $this->_response->statusCode(204);
  126. $this->assertResponseOk();
  127. $this->_response->statusCode(400);
  128. $this->assertResponseError();
  129. $this->_response->statusCode(417);
  130. $this->assertResponseError();
  131. $this->_response->statusCode(500);
  132. $this->assertResponseFailure();
  133. $this->_response->statusCode(505);
  134. $this->assertResponseFailure();
  135. $this->_response->statusCode(301);
  136. $this->assertResponseCode(301);
  137. }
  138. /**
  139. * Test the location header assertion.
  140. *
  141. * @return void
  142. */
  143. public function testAssertRedirect() {
  144. $this->_response = new Response();
  145. $this->_response->header('Location', 'http://localhost/tasks/index');
  146. $this->assertRedirect('/tasks/index');
  147. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  148. }
  149. /**
  150. * Test the location header assertion.
  151. *
  152. * @return void
  153. */
  154. public function testAssertNoRedirect() {
  155. $this->_response = new Response();
  156. $this->assertNoRedirect();
  157. }
  158. /**
  159. * Test the location header assertion.
  160. *
  161. * @return void
  162. */
  163. public function testAssertNoRedirectFail() {
  164. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  165. $result = $test->run();
  166. ob_start();
  167. $this->assertFalse($result->wasSuccessful());
  168. $this->assertEquals(1, $result->failureCount());
  169. }
  170. /**
  171. * Test the header assertion.
  172. *
  173. * @return void
  174. */
  175. public function testAssertHeader() {
  176. $this->_response = new Response();
  177. $this->_response->header('Etag', 'abc123');
  178. $this->assertHeader('Etag', 'abc123');
  179. }
  180. /**
  181. * Test the content type assertion.
  182. *
  183. * @return void
  184. */
  185. public function testAssertContentType() {
  186. $this->_response = new Response();
  187. $this->_response->type('json');
  188. $this->assertContentType('json');
  189. $this->assertContentType('application/json');
  190. }
  191. /**
  192. * Test the content assertion.
  193. *
  194. * @return void
  195. */
  196. public function testAssertResponseContains() {
  197. $this->_response = new Response();
  198. $this->_response->body('Some content');
  199. $this->assertResponseContains('content');
  200. }
  201. /**
  202. * Test that works in tandem with testEventManagerReset2 to
  203. * test the EventManager reset.
  204. *
  205. * The return value is passed to testEventManagerReset2 as
  206. * an arguments.
  207. *
  208. * @return \Cake\Event\EventManager
  209. */
  210. public function testEventManagerReset1() {
  211. return EventManager::instance();
  212. }
  213. /**
  214. * Test if the EventManager is reset between tests.
  215. *
  216. * @depends testEventManagerReset1
  217. * @return void
  218. */
  219. public function testEventManagerReset2($prevEventManager) {
  220. $this->assertNotSame($prevEventManager, EventManager::instance());
  221. }
  222. }