IntegrationTestCaseTest.php 24 KB

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