IntegrationTestCaseTest.php 10 KB

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