IntegrationTestCaseTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. 'environment' => [
  54. 'PHP_AUTH_USER' => 'foo',
  55. 'PHP_AUTH_PW' => 'bar'
  56. ]
  57. ]);
  58. $this->cookie('split_token', 'def345');
  59. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  60. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  61. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  62. $this->assertEquals('tasks/add', $request->url);
  63. $this->assertEquals(['split_token' => 'def345'], $request->cookies);
  64. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  65. $this->assertEquals('foo', $request->env('PHP_AUTH_USER'));
  66. $this->assertEquals('bar', $request->env('PHP_AUTH_PW'));
  67. }
  68. /**
  69. * Test sending get requests.
  70. *
  71. * @return void
  72. */
  73. public function testGet()
  74. {
  75. $this->assertNull($this->_response);
  76. $this->get('/request_action/test_request_action');
  77. $this->assertNotEmpty($this->_response);
  78. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  79. $this->assertEquals('This is a test', $this->_response->body());
  80. }
  81. /**
  82. * Test sending requests stores references to controller/view/layout.
  83. *
  84. * @return void
  85. */
  86. public function testRequestSetsProperties()
  87. {
  88. $this->post('/posts/index');
  89. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  90. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  91. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  92. $this->assertTemplate('index');
  93. $this->assertLayout('default');
  94. $this->assertEquals('value', $this->viewVariable('test'));
  95. }
  96. /**
  97. * Test array URLs
  98. *
  99. * @return void
  100. */
  101. public function testArrayUrls()
  102. {
  103. $this->post(['controller' => 'Posts', 'action' => 'index']);
  104. $this->assertEquals('value', $this->viewVariable('test'));
  105. }
  106. /**
  107. * Test flash and cookie assertions
  108. *
  109. * @return void
  110. */
  111. public function testFlashSessionAndCookieAsserts()
  112. {
  113. $this->post('/posts/index');
  114. $this->assertSession('An error message', 'Flash.flash.message');
  115. $this->assertCookie(1, 'remember_me');
  116. }
  117. /**
  118. * Test error handling and error page rendering.
  119. *
  120. * @return void
  121. */
  122. public function testPostAndErrorHandling()
  123. {
  124. $this->post('/request_action/error_method');
  125. $this->assertResponseNotEmpty();
  126. $this->assertResponseContains('Not there or here');
  127. $this->assertResponseContains('<!DOCTYPE html>');
  128. }
  129. /**
  130. * Test redirecting and integration tests.
  131. *
  132. * @return void
  133. */
  134. public function testRedirect()
  135. {
  136. $this->post('/tests_apps/redirect_to');
  137. $this->assertResponseSuccess();
  138. $this->assertResponseCode(302);
  139. }
  140. /**
  141. * Test redirecting and integration tests.
  142. *
  143. * @return void
  144. */
  145. public function testRedirectPermanent()
  146. {
  147. $this->post('/tests_apps/redirect_to_permanent');
  148. $this->assertResponseSuccess();
  149. $this->assertResponseCode(301);
  150. }
  151. /**
  152. * Test the responseOk status assertion
  153. *
  154. * @return void
  155. */
  156. public function testAssertResponseStatusCodes()
  157. {
  158. $this->_response = new Response();
  159. $this->_response->statusCode(200);
  160. $this->assertResponseOk();
  161. $this->_response->statusCode(201);
  162. $this->assertResponseOk();
  163. $this->_response->statusCode(204);
  164. $this->assertResponseOk();
  165. $this->_response->statusCode(202);
  166. $this->assertResponseSuccess();
  167. $this->_response->statusCode(302);
  168. $this->assertResponseSuccess();
  169. $this->_response->statusCode(400);
  170. $this->assertResponseError();
  171. $this->_response->statusCode(417);
  172. $this->assertResponseError();
  173. $this->_response->statusCode(500);
  174. $this->assertResponseFailure();
  175. $this->_response->statusCode(505);
  176. $this->assertResponseFailure();
  177. $this->_response->statusCode(301);
  178. $this->assertResponseCode(301);
  179. }
  180. /**
  181. * Test the location header assertion.
  182. *
  183. * @return void
  184. */
  185. public function testAssertRedirect()
  186. {
  187. $this->_response = new Response();
  188. $this->_response->header('Location', 'http://localhost/tasks/index');
  189. $this->assertRedirect();
  190. $this->assertRedirect('/tasks/index');
  191. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  192. $this->assertResponseEmpty();
  193. }
  194. /**
  195. * Test the location header assertion.
  196. *
  197. * @return void
  198. */
  199. public function testAssertNoRedirect()
  200. {
  201. $this->_response = new Response();
  202. $this->assertNoRedirect();
  203. }
  204. /**
  205. * Test the location header assertion.
  206. *
  207. * @return void
  208. */
  209. public function testAssertNoRedirectFail()
  210. {
  211. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  212. $result = $test->run();
  213. ob_start();
  214. $this->assertFalse($result->wasSuccessful());
  215. $this->assertEquals(1, $result->failureCount());
  216. }
  217. /**
  218. * Test the location header assertion string contains
  219. *
  220. * @return void
  221. */
  222. public function testAssertRedirectContains()
  223. {
  224. $this->_response = new Response();
  225. $this->_response->header('Location', 'http://localhost/tasks/index');
  226. $this->assertRedirectContains('/tasks/index');
  227. }
  228. /**
  229. * Test the header assertion.
  230. *
  231. * @return void
  232. */
  233. public function testAssertHeader()
  234. {
  235. $this->_response = new Response();
  236. $this->_response->header('Etag', 'abc123');
  237. $this->assertHeader('Etag', 'abc123');
  238. }
  239. /**
  240. * Test the content type assertion.
  241. *
  242. * @return void
  243. */
  244. public function testAssertContentType()
  245. {
  246. $this->_response = new Response();
  247. $this->_response->type('json');
  248. $this->assertContentType('json');
  249. $this->assertContentType('application/json');
  250. }
  251. /**
  252. * Test that type() in an action sets the content-type header.
  253. *
  254. * @return void
  255. */
  256. public function testContentTypeInAction()
  257. {
  258. $this->get('/tests_apps/set_type');
  259. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  260. $this->assertContentType('json');
  261. $this->assertContentType('application/json');
  262. }
  263. /**
  264. * Test the content assertion.
  265. *
  266. * @return void
  267. */
  268. public function testAssertResponseContains()
  269. {
  270. $this->_response = new Response();
  271. $this->_response->body('Some content');
  272. $this->assertResponseContains('content');
  273. }
  274. /**
  275. * Test the negated content assertion.
  276. *
  277. * @return void
  278. */
  279. public function testAssertResponseNotContains()
  280. {
  281. $this->_response = new Response();
  282. $this->_response->body('Some content');
  283. $this->assertResponseNotContains('contents');
  284. }
  285. /**
  286. * Test that works in tandem with testEventManagerReset2 to
  287. * test the EventManager reset.
  288. *
  289. * The return value is passed to testEventManagerReset2 as
  290. * an arguments.
  291. *
  292. * @return \Cake\Event\EventManager
  293. */
  294. public function testEventManagerReset1()
  295. {
  296. return EventManager::instance();
  297. }
  298. /**
  299. * Test if the EventManager is reset between tests.
  300. *
  301. * @depends testEventManagerReset1
  302. * @return void
  303. */
  304. public function testEventManagerReset2($prevEventManager)
  305. {
  306. $this->assertNotSame($prevEventManager, EventManager::instance());
  307. }
  308. }