IntegrationTestCaseTest.php 5.6 KB

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