IntegrationTestCaseTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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. use Cake\Utility\Security;
  24. /**
  25. * Self test of the IntegrationTestCase
  26. */
  27. class IntegrationTestCaseTest extends IntegrationTestCase
  28. {
  29. /**
  30. * Setup method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  39. DispatcherFactory::clear();
  40. DispatcherFactory::add('Routing');
  41. DispatcherFactory::add('ControllerFactory');
  42. $this->useHttpServer(false);
  43. }
  44. /**
  45. * Test building a request.
  46. *
  47. * @return void
  48. */
  49. public function testRequestBuilding()
  50. {
  51. $this->configRequest([
  52. 'headers' => [
  53. 'X-CSRF-Token' => 'abc123',
  54. 'Content-Type' => 'application/json',
  55. 'Accept' => 'application/json'
  56. ],
  57. 'base' => '',
  58. 'webroot' => '/',
  59. 'environment' => [
  60. 'PHP_AUTH_USER' => 'foo',
  61. 'PHP_AUTH_PW' => 'bar'
  62. ]
  63. ]);
  64. $this->cookie('split_token', 'def345');
  65. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  66. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  67. $this->assertEquals('abc123', $request['environment']['HTTP_X_CSRF_TOKEN']);
  68. $this->assertEquals('application/json', $request['environment']['CONTENT_TYPE']);
  69. $this->assertEquals('/tasks/add', $request['url']);
  70. $this->assertArrayHasKey('split_token', $request['cookies']);
  71. $this->assertEquals('def345', $request['cookies']['split_token']);
  72. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request['session']->read('User'));
  73. $this->assertEquals('foo', $request['environment']['PHP_AUTH_USER']);
  74. $this->assertEquals('bar', $request['environment']['PHP_AUTH_PW']);
  75. }
  76. /**
  77. * Test request building adds csrf tokens
  78. *
  79. * @return void
  80. */
  81. public function testRequestBuildingCsrfTokens()
  82. {
  83. $this->enableCsrfToken();
  84. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  85. $this->assertArrayHasKey('csrfToken', $request['cookies']);
  86. $this->assertArrayHasKey('_csrfToken', $request['post']);
  87. $this->assertSame($request['cookies']['csrfToken'], $request['post']['_csrfToken']);
  88. $this->cookie('csrfToken', '');
  89. $request = $this->_buildRequest('/tasks/add', 'POST', [
  90. '_csrfToken' => 'fale',
  91. 'title' => 'First post'
  92. ]);
  93. $this->assertSame('', $request['cookies']['csrfToken']);
  94. $this->assertSame('fale', $request['post']['_csrfToken']);
  95. }
  96. /**
  97. * Test multiple actions using CSRF tokens don't fail
  98. *
  99. * @return void
  100. */
  101. public function testEnableCsrfMultipleRequests()
  102. {
  103. $this->enableCsrfToken();
  104. $first = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  105. $second = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'Second post']);
  106. $this->assertSame(
  107. $first['cookies']['csrfToken'],
  108. $second['post']['_csrfToken'],
  109. 'Csrf token should match cookie'
  110. );
  111. $this->assertSame(
  112. $first['post']['_csrfToken'],
  113. $second['post']['_csrfToken'],
  114. 'Tokens should be consistent per test method'
  115. );
  116. }
  117. /**
  118. * Test pre-determined CSRF tokens.
  119. *
  120. * @return void
  121. */
  122. public function testEnableCsrfPredeterminedCookie()
  123. {
  124. $this->enableCsrfToken();
  125. $value = 'I am a teapot';
  126. $this->cookie('csrfToken', $value);
  127. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  128. $this->assertSame($value, $request['cookies']['csrfToken'], 'Csrf token should match cookie');
  129. $this->assertSame($value, $request['post']['_csrfToken'], 'Tokens should match');
  130. }
  131. /**
  132. * Test building a request, with query parameters
  133. *
  134. * @return void
  135. */
  136. public function testRequestBuildingQueryParameters()
  137. {
  138. $request = $this->_buildRequest('/tasks/view?archived=yes', 'GET', []);
  139. $this->assertEquals('/tasks/view', $request['url']);
  140. $this->assertEquals('yes', $request['query']['archived']);
  141. }
  142. /**
  143. * Test cookie encrypted
  144. *
  145. * @see CookieComponentControllerTest
  146. */
  147. public function testCookieEncrypted()
  148. {
  149. Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  150. $this->cookieEncrypted('KeyOfCookie', 'Encrypted with aes by default');
  151. $request = $this->_buildRequest('/tasks/view', 'GET', []);
  152. $this->assertStringStartsWith('Q2FrZQ==.', $request['cookies']['KeyOfCookie']);
  153. }
  154. /**
  155. * Test sending get requests.
  156. *
  157. * @return void
  158. */
  159. public function testGet()
  160. {
  161. $this->assertNull($this->_response);
  162. $this->get('/request_action/test_request_action');
  163. $this->assertNotEmpty($this->_response);
  164. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  165. $this->assertEquals('This is a test', $this->_response->body());
  166. }
  167. /**
  168. * Test customizing the app class.
  169. *
  170. * @expectedException LogicException
  171. * @expectedExceptionMessage Cannot load "TestApp\MissingApp" for use in integration
  172. * @return void
  173. */
  174. public function testConfigApplication()
  175. {
  176. DispatcherFactory::clear();
  177. $this->useHttpServer(true);
  178. $this->configApplication('TestApp\MissingApp', []);
  179. $this->get('/request_action/test_request_action');
  180. }
  181. /**
  182. * Test sending get requests with Http\Server
  183. *
  184. * @return void
  185. */
  186. public function testGetHttpServer()
  187. {
  188. DispatcherFactory::clear();
  189. $this->useHttpServer(true);
  190. $this->assertNull($this->_response);
  191. $this->get('/request_action/test_request_action');
  192. $this->assertNotEmpty($this->_response);
  193. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  194. $this->assertEquals('This is a test', $this->_response->body());
  195. $this->assertHeader('X-Middleware', 'true');
  196. }
  197. /**
  198. * Test that the PSR7 requests get query string data
  199. *
  200. * @return void
  201. */
  202. public function testQueryStringHttpServer()
  203. {
  204. $this->useHttpServer(true);
  205. $this->configRequest(['headers' => ['Content-Type' => 'text/plain']]);
  206. $this->get('/request_action/params_pass?q=query');
  207. $this->assertResponseOk();
  208. $this->assertResponseContains('"q":"query"');
  209. $this->assertResponseContains('"contentType":"text\/plain"');
  210. $this->assertHeader('X-Middleware', 'true');
  211. }
  212. /**
  213. * Test that the PSR7 requests get cookies
  214. *
  215. * @return void
  216. */
  217. public function testGetCookiesHttpServer()
  218. {
  219. $this->useHttpServer(true);
  220. $this->configRequest(['cookies' => ['split_test' => 'abc']]);
  221. $this->get('/request_action/cookie_pass');
  222. $this->assertResponseOk();
  223. $this->assertResponseContains('"split_test":"abc"');
  224. $this->assertHeader('X-Middleware', 'true');
  225. }
  226. /**
  227. * Test that the PSR7 requests receive post data
  228. *
  229. * @return void
  230. */
  231. public function testPostDataHttpServer()
  232. {
  233. $this->useHttpServer(true);
  234. $this->post('/request_action/post_pass', ['title' => 'value']);
  235. $data = json_decode($this->_response->body());
  236. $this->assertEquals('value', $data->title);
  237. $this->assertHeader('X-Middleware', 'true');
  238. }
  239. /**
  240. * Test that the PSR7 requests receive encoded data.
  241. *
  242. * @return void
  243. */
  244. public function testInputDataHttpServer()
  245. {
  246. $this->useHttpServer(true);
  247. $this->post('/request_action/input_test', '{"hello":"world"}');
  248. $this->assertSame('world', $this->_response->body());
  249. $this->assertHeader('X-Middleware', 'true');
  250. }
  251. /**
  252. * Test that the PSR7 requests get cookies
  253. *
  254. * @return void
  255. */
  256. public function testSessionHttpServer()
  257. {
  258. $this->useHttpServer(true);
  259. $this->session(['foo' => 'session data']);
  260. $this->get('/request_action/session_test');
  261. $this->assertResponseOk();
  262. $this->assertResponseContains('session data');
  263. $this->assertHeader('X-Middleware', 'true');
  264. }
  265. /**
  266. * Test sending requests stores references to controller/view/layout.
  267. *
  268. * @return void
  269. */
  270. public function testRequestSetsProperties()
  271. {
  272. $this->post('/posts/index');
  273. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  274. $this->assertNotEmpty($this->_viewName, 'View name not set');
  275. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  276. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  277. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  278. $this->assertTemplate('index');
  279. $this->assertLayout('default');
  280. $this->assertEquals('value', $this->viewVariable('test'));
  281. }
  282. /**
  283. * Test PSR7 requests store references to controller/view/layout
  284. *
  285. * @return void
  286. */
  287. public function testRequestSetsPropertiesHttpServer()
  288. {
  289. $this->useHttpServer(true);
  290. DispatcherFactory::clear();
  291. $this->post('/posts/index');
  292. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  293. $this->assertNotEmpty($this->_viewName, 'View name not set');
  294. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  295. $this->assertNotEmpty($this->_layoutName, 'Layout name not set');
  296. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  297. $this->assertTemplate('index');
  298. $this->assertLayout('default');
  299. $this->assertEquals('value', $this->viewVariable('test'));
  300. }
  301. /**
  302. * Assert that the stored template doesn't change when cells are rendered.
  303. *
  304. * @return void
  305. */
  306. public function testAssertTemplateAfterCellRender()
  307. {
  308. $this->get('/posts/get');
  309. $this->assertContains('Template' . DS . 'Posts' . DS . 'get.ctp', $this->_viewName);
  310. $this->assertTemplate('get');
  311. $this->assertResponseContains('cellcontent');
  312. }
  313. /**
  314. * Test array URLs
  315. *
  316. * @return void
  317. */
  318. public function testArrayUrls()
  319. {
  320. $this->post(['controller' => 'Posts', 'action' => 'index']);
  321. $this->assertEquals('value', $this->viewVariable('test'));
  322. }
  323. /**
  324. * Test flash and cookie assertions
  325. *
  326. * @return void
  327. */
  328. public function testFlashSessionAndCookieAsserts()
  329. {
  330. $this->post('/posts/index');
  331. $this->assertSession('An error message', 'Flash.flash.0.message');
  332. $this->assertCookie(1, 'remember_me');
  333. $this->assertCookieNotSet('user_id');
  334. }
  335. /**
  336. * Test flash and cookie assertions
  337. *
  338. * @return void
  339. */
  340. public function testFlashSessionAndCookieAssertsHttpServer()
  341. {
  342. $this->useHttpServer(true);
  343. $this->post('/posts/index');
  344. $this->assertSession('An error message', 'Flash.flash.0.message');
  345. $this->assertCookieNotSet('user_id');
  346. $this->assertCookie(1, 'remember_me');
  347. }
  348. /**
  349. * Tests the failure message for assertCookieNotSet
  350. *
  351. * @expectedException PHPUnit_Framework_AssertionFailedError
  352. * @expectedExceptionMessage Cookie 'remember_me' has been set
  353. * @return void
  354. */
  355. public function testCookieNotSetFailure()
  356. {
  357. $this->post('/posts/index');
  358. $this->assertCookieNotSet('remember_me');
  359. }
  360. /**
  361. * Tests the failure message for assertCookieNotSet when no
  362. * response whas generated
  363. *
  364. * @expectedException PHPUnit_Framework_AssertionFailedError
  365. * @expectedExceptionMessage No response set, cannot assert cookies.
  366. * @return void
  367. */
  368. public function testCookieNotSetFailureNoResponse()
  369. {
  370. $this->assertCookieNotSet('remember_me');
  371. }
  372. /**
  373. * Test error handling and error page rendering.
  374. *
  375. * @return void
  376. */
  377. public function testPostAndErrorHandling()
  378. {
  379. $this->post('/request_action/error_method');
  380. $this->assertResponseNotEmpty();
  381. $this->assertResponseContains('Not there or here');
  382. $this->assertResponseContains('<!DOCTYPE html>');
  383. }
  384. /**
  385. * Test posting to a secured form action.
  386. *
  387. * @return void
  388. */
  389. public function testPostSecuredForm()
  390. {
  391. $this->enableSecurityToken();
  392. $data = [
  393. 'title' => 'Some title',
  394. 'body' => 'Some text'
  395. ];
  396. $this->post('/posts/securePost', $data);
  397. $this->assertResponseOk();
  398. $this->assertResponseContains('Request was accepted');
  399. }
  400. /**
  401. * Test posting to a secured form action with nested data.
  402. *
  403. * @return void
  404. */
  405. public function testPostSecuredFormNestedData()
  406. {
  407. $this->enableSecurityToken();
  408. $data = [
  409. 'title' => 'New post',
  410. 'comments' => [
  411. ['comment' => 'A new comment']
  412. ],
  413. 'tags' => ['_ids' => [1, 2, 3, 4]]
  414. ];
  415. $this->post('/posts/securePost', $data);
  416. $this->assertResponseOk();
  417. $this->assertResponseContains('Request was accepted');
  418. }
  419. /**
  420. * Test posting to a secured form action.
  421. *
  422. * @return void
  423. */
  424. public function testPostSecuredFormWithQuery()
  425. {
  426. $this->enableSecurityToken();
  427. $data = [
  428. 'title' => 'Some title',
  429. 'body' => 'Some text'
  430. ];
  431. $this->post('/posts/securePost?foo=bar', $data);
  432. $this->assertResponseOk();
  433. $this->assertResponseContains('Request was accepted');
  434. }
  435. /**
  436. * Test posting to a secured form action action.
  437. *
  438. * @return void
  439. */
  440. public function testPostSecuredFormFailure()
  441. {
  442. $data = [
  443. 'title' => 'Some title',
  444. 'body' => 'Some text'
  445. ];
  446. $this->post('/posts/securePost', $data);
  447. $this->assertResponseError();
  448. }
  449. /**
  450. * Test that exceptions being thrown are handled correctly.
  451. *
  452. * @return void
  453. */
  454. public function testWithExpectedException()
  455. {
  456. $this->get('/tests_apps/throw_exception');
  457. $this->assertResponseCode(500);
  458. }
  459. /**
  460. * Test that exceptions being thrown are handled correctly by the psr7 stack.
  461. *
  462. * @return void
  463. */
  464. public function testWithExpectedExceptionHttpServer()
  465. {
  466. DispatcherFactory::clear();
  467. $this->useHttpServer(true);
  468. $this->get('/tests_apps/throw_exception');
  469. $this->assertResponseCode(500);
  470. }
  471. /**
  472. * Test that exceptions being thrown are handled correctly.
  473. *
  474. * @expectedException PHPUnit_Framework_AssertionFailedError
  475. * @return void
  476. */
  477. public function testWithUnexpectedException()
  478. {
  479. $this->get('/tests_apps/throw_exception');
  480. $this->assertResponseCode(501);
  481. }
  482. /**
  483. * Test redirecting and integration tests.
  484. *
  485. * @return void
  486. */
  487. public function testRedirect()
  488. {
  489. $this->post('/tests_apps/redirect_to');
  490. $this->assertResponseSuccess();
  491. $this->assertResponseCode(302);
  492. }
  493. /**
  494. * Test redirecting and psr7 stack
  495. *
  496. * @return void
  497. */
  498. public function testRedirectHttpServer()
  499. {
  500. DispatcherFactory::clear();
  501. $this->useHttpServer(true);
  502. $this->post('/tests_apps/redirect_to');
  503. $this->assertResponseCode(302);
  504. $this->assertHeader('X-Middleware', 'true');
  505. }
  506. /**
  507. * Test redirecting and integration tests.
  508. *
  509. * @return void
  510. */
  511. public function testRedirectPermanent()
  512. {
  513. $this->post('/tests_apps/redirect_to_permanent');
  514. $this->assertResponseSuccess();
  515. $this->assertResponseCode(301);
  516. }
  517. /**
  518. * Test the responseOk status assertion
  519. *
  520. * @return void
  521. */
  522. public function testAssertResponseStatusCodes()
  523. {
  524. $this->_response = new Response();
  525. $this->_response->statusCode(200);
  526. $this->assertResponseOk();
  527. $this->_response->statusCode(201);
  528. $this->assertResponseOk();
  529. $this->_response->statusCode(204);
  530. $this->assertResponseOk();
  531. $this->_response->statusCode(202);
  532. $this->assertResponseSuccess();
  533. $this->_response->statusCode(302);
  534. $this->assertResponseSuccess();
  535. $this->_response->statusCode(400);
  536. $this->assertResponseError();
  537. $this->_response->statusCode(417);
  538. $this->assertResponseError();
  539. $this->_response->statusCode(500);
  540. $this->assertResponseFailure();
  541. $this->_response->statusCode(505);
  542. $this->assertResponseFailure();
  543. $this->_response->statusCode(301);
  544. $this->assertResponseCode(301);
  545. }
  546. /**
  547. * Test the location header assertion.
  548. *
  549. * @return void
  550. */
  551. public function testAssertRedirect()
  552. {
  553. $this->_response = new Response();
  554. $this->_response->header('Location', 'http://localhost/tasks/index');
  555. $this->assertRedirect();
  556. $this->assertRedirect('/tasks/index');
  557. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  558. $this->assertResponseEmpty();
  559. }
  560. /**
  561. * Test the location header assertion.
  562. *
  563. * @return void
  564. */
  565. public function testAssertNoRedirect()
  566. {
  567. $this->_response = new Response();
  568. $this->assertNoRedirect();
  569. }
  570. /**
  571. * Test the location header assertion.
  572. *
  573. * @return void
  574. */
  575. public function testAssertNoRedirectFail()
  576. {
  577. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  578. $result = $test->run();
  579. ob_start();
  580. $this->assertFalse($result->wasSuccessful());
  581. $this->assertEquals(1, $result->failureCount());
  582. }
  583. /**
  584. * Test the location header assertion string contains
  585. *
  586. * @return void
  587. */
  588. public function testAssertRedirectContains()
  589. {
  590. $this->_response = new Response();
  591. $this->_response->header('Location', 'http://localhost/tasks/index');
  592. $this->assertRedirectContains('/tasks/index');
  593. }
  594. /**
  595. * Test the header assertion.
  596. *
  597. * @return void
  598. */
  599. public function testAssertHeader()
  600. {
  601. $this->_response = new Response();
  602. $this->_response->header('Etag', 'abc123');
  603. $this->assertHeader('Etag', 'abc123');
  604. }
  605. /**
  606. * Test the header contains assertion.
  607. *
  608. * @return void
  609. */
  610. public function testAssertHeaderContains()
  611. {
  612. $this->_response = new Response();
  613. $this->_response->header('Etag', 'abc123');
  614. $this->assertHeaderContains('Etag', 'abc');
  615. }
  616. /**
  617. * Test the content type assertion.
  618. *
  619. * @return void
  620. */
  621. public function testAssertContentType()
  622. {
  623. $this->_response = new Response();
  624. $this->_response->type('json');
  625. $this->assertContentType('json');
  626. $this->assertContentType('application/json');
  627. }
  628. /**
  629. * Test that type() in an action sets the content-type header.
  630. *
  631. * @return void
  632. */
  633. public function testContentTypeInAction()
  634. {
  635. $this->get('/tests_apps/set_type');
  636. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  637. $this->assertContentType('json');
  638. $this->assertContentType('application/json');
  639. }
  640. /**
  641. * Test the content assertion.
  642. *
  643. * @return void
  644. */
  645. public function testAssertResponseContains()
  646. {
  647. $this->_response = new Response();
  648. $this->_response->body('Some content');
  649. $this->assertResponseContains('content');
  650. }
  651. /**
  652. * Test the negated content assertion.
  653. *
  654. * @return void
  655. */
  656. public function testAssertResponseNotContains()
  657. {
  658. $this->_response = new Response();
  659. $this->_response->body('Some content');
  660. $this->assertResponseNotContains('contents');
  661. }
  662. /**
  663. * Test that works in tandem with testEventManagerReset2 to
  664. * test the EventManager reset.
  665. *
  666. * The return value is passed to testEventManagerReset2 as
  667. * an arguments.
  668. *
  669. * @return \Cake\Event\EventManager
  670. */
  671. public function testEventManagerReset1()
  672. {
  673. return EventManager::instance();
  674. }
  675. /**
  676. * Test if the EventManager is reset between tests.
  677. *
  678. * @depends testEventManagerReset1
  679. * @return void
  680. */
  681. public function testEventManagerReset2($prevEventManager)
  682. {
  683. $this->assertNotSame($prevEventManager, EventManager::instance());
  684. }
  685. /**
  686. * Test sending file in requests.
  687. *
  688. * @return void
  689. */
  690. public function testSendFile()
  691. {
  692. $this->get('/posts/file');
  693. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  694. }
  695. /**
  696. * Test sending file with psr7 stack
  697. *
  698. * @return void
  699. */
  700. public function testSendFileHttpServer()
  701. {
  702. DispatcherFactory::clear();
  703. $this->useHttpServer(true);
  704. $this->get('/posts/file');
  705. $this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
  706. }
  707. /**
  708. * Test that assertFile requires a response
  709. *
  710. * @expectedException PHPUnit_Framework_AssertionFailedError
  711. * @expectedExceptionMessage No response set, cannot assert file
  712. * @return void
  713. */
  714. public function testAssertFileNoReponse()
  715. {
  716. $this->assertFileResponse('foo');
  717. }
  718. /**
  719. * Test that assertFile requires a file
  720. *
  721. * @expectedException PHPUnit_Framework_AssertionFailedError
  722. * @expectedExceptionMessage No file was sent in this response
  723. * @return void
  724. */
  725. public function testAssertFileNoFile()
  726. {
  727. $this->get('/posts/get');
  728. $this->assertFileResponse('foo');
  729. }
  730. }