IntegrationTestCaseTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 array URLs
  86. *
  87. * @return void
  88. */
  89. public function testArrayUrls() {
  90. $this->post(['controller' => 'Posts', 'action' => 'index']);
  91. $this->assertEquals('value', $this->viewVariable('test'));
  92. }
  93. /**
  94. * Test flash and cookie assertions
  95. *
  96. * @return void
  97. */
  98. public function testFlashSessionAndCookieAsserts() {
  99. $this->post('/posts/index');
  100. $this->assertSession('An error message', 'Flash.flash.message');
  101. $this->assertCookie(1, 'remember_me');
  102. }
  103. /**
  104. * Test error handling and error page rendering.
  105. *
  106. * @return void
  107. */
  108. public function testPostAndErrorHandling() {
  109. $this->post('/request_action/error_method');
  110. $this->assertResponseContains('Not there or here');
  111. $this->assertResponseContains('<!DOCTYPE html>');
  112. }
  113. /**
  114. * Test the responseOk status assertion
  115. *
  116. * @return void
  117. */
  118. public function testAssertResponseStatusCodes() {
  119. $this->_response = new Response();
  120. $this->_response->statusCode(200);
  121. $this->assertResponseOk();
  122. $this->_response->statusCode(201);
  123. $this->assertResponseOk();
  124. $this->_response->statusCode(204);
  125. $this->assertResponseOk();
  126. $this->_response->statusCode(400);
  127. $this->assertResponseError();
  128. $this->_response->statusCode(417);
  129. $this->assertResponseError();
  130. $this->_response->statusCode(500);
  131. $this->assertResponseFailure();
  132. $this->_response->statusCode(505);
  133. $this->assertResponseFailure();
  134. $this->_response->statusCode(301);
  135. $this->assertResponseCode(301);
  136. }
  137. /**
  138. * Test the location header assertion.
  139. *
  140. * @return void
  141. */
  142. public function testAssertRedirect() {
  143. $this->_response = new Response();
  144. $this->_response->header('Location', 'http://localhost/tasks/index');
  145. $this->assertRedirect('/tasks/index');
  146. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  147. }
  148. /**
  149. * Test the header assertion.
  150. *
  151. * @return void
  152. */
  153. public function testAssertHeader() {
  154. $this->_response = new Response();
  155. $this->_response->header('Etag', 'abc123');
  156. $this->assertHeader('Etag', 'abc123');
  157. }
  158. /**
  159. * Test the content type assertion.
  160. *
  161. * @return void
  162. */
  163. public function testAssertContentType() {
  164. $this->_response = new Response();
  165. $this->_response->type('json');
  166. $this->assertContentType('json');
  167. $this->assertContentType('application/json');
  168. }
  169. /**
  170. * Test the content assertion.
  171. *
  172. * @return void
  173. */
  174. public function testAssertResponseContains() {
  175. $this->_response = new Response();
  176. $this->_response->body('Some content');
  177. $this->assertResponseContains('content');
  178. }
  179. /**
  180. * Test that works in tandem with testEventManagerReset2 to
  181. * test the EventManager reset.
  182. *
  183. * The return value is passed to testEventManagerReset2 as
  184. * an arguments.
  185. *
  186. * @return \Cake\Event\EventManager
  187. */
  188. public function testEventManagerReset1() {
  189. return EventManager::instance();
  190. }
  191. /**
  192. * Test if the EventManager is reset between tests.
  193. *
  194. * @depends testEventManagerReset1
  195. * @return void
  196. */
  197. public function testEventManagerReset2($prevEventManager) {
  198. $this->assertNotSame($prevEventManager, EventManager::instance());
  199. }
  200. }