IntegrationTestCaseTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 that exceptions being thrown are handled correctly.
  131. *
  132. * @return void
  133. */
  134. public function testWithExpectedException()
  135. {
  136. $this->get('/tests_apps/throw_exception');
  137. $this->assertResponseCode(500);
  138. }
  139. /**
  140. * Test that exceptions being thrown are handled correctly.
  141. *
  142. * @expectedException PHPUnit_Framework_AssertionFailedError
  143. * @return void
  144. */
  145. public function testWithUnexpectedException()
  146. {
  147. $this->get('/tests_apps/throw_exception');
  148. $this->assertResponseCode(501);
  149. }
  150. /**
  151. * Test redirecting and integration tests.
  152. *
  153. * @return void
  154. */
  155. public function testRedirect()
  156. {
  157. $this->post('/tests_apps/redirect_to');
  158. $this->assertResponseSuccess();
  159. $this->assertResponseCode(302);
  160. }
  161. /**
  162. * Test redirecting and integration tests.
  163. *
  164. * @return void
  165. */
  166. public function testRedirectPermanent()
  167. {
  168. $this->post('/tests_apps/redirect_to_permanent');
  169. $this->assertResponseSuccess();
  170. $this->assertResponseCode(301);
  171. }
  172. /**
  173. * Test the responseOk status assertion
  174. *
  175. * @return void
  176. */
  177. public function testAssertResponseStatusCodes()
  178. {
  179. $this->_response = new Response();
  180. $this->_response->statusCode(200);
  181. $this->assertResponseOk();
  182. $this->_response->statusCode(201);
  183. $this->assertResponseOk();
  184. $this->_response->statusCode(204);
  185. $this->assertResponseOk();
  186. $this->_response->statusCode(202);
  187. $this->assertResponseSuccess();
  188. $this->_response->statusCode(302);
  189. $this->assertResponseSuccess();
  190. $this->_response->statusCode(400);
  191. $this->assertResponseError();
  192. $this->_response->statusCode(417);
  193. $this->assertResponseError();
  194. $this->_response->statusCode(500);
  195. $this->assertResponseFailure();
  196. $this->_response->statusCode(505);
  197. $this->assertResponseFailure();
  198. $this->_response->statusCode(301);
  199. $this->assertResponseCode(301);
  200. }
  201. /**
  202. * Test the location header assertion.
  203. *
  204. * @return void
  205. */
  206. public function testAssertRedirect()
  207. {
  208. $this->_response = new Response();
  209. $this->_response->header('Location', 'http://localhost/tasks/index');
  210. $this->assertRedirect();
  211. $this->assertRedirect('/tasks/index');
  212. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  213. $this->assertResponseEmpty();
  214. }
  215. /**
  216. * Test the location header assertion.
  217. *
  218. * @return void
  219. */
  220. public function testAssertNoRedirect()
  221. {
  222. $this->_response = new Response();
  223. $this->assertNoRedirect();
  224. }
  225. /**
  226. * Test the location header assertion.
  227. *
  228. * @return void
  229. */
  230. public function testAssertNoRedirectFail()
  231. {
  232. $test = new AssertIntegrationTestCase('testBadAssertNoRedirect');
  233. $result = $test->run();
  234. ob_start();
  235. $this->assertFalse($result->wasSuccessful());
  236. $this->assertEquals(1, $result->failureCount());
  237. }
  238. /**
  239. * Test the location header assertion string contains
  240. *
  241. * @return void
  242. */
  243. public function testAssertRedirectContains()
  244. {
  245. $this->_response = new Response();
  246. $this->_response->header('Location', 'http://localhost/tasks/index');
  247. $this->assertRedirectContains('/tasks/index');
  248. }
  249. /**
  250. * Test the header assertion.
  251. *
  252. * @return void
  253. */
  254. public function testAssertHeader()
  255. {
  256. $this->_response = new Response();
  257. $this->_response->header('Etag', 'abc123');
  258. $this->assertHeader('Etag', 'abc123');
  259. }
  260. /**
  261. * Test the content type assertion.
  262. *
  263. * @return void
  264. */
  265. public function testAssertContentType()
  266. {
  267. $this->_response = new Response();
  268. $this->_response->type('json');
  269. $this->assertContentType('json');
  270. $this->assertContentType('application/json');
  271. }
  272. /**
  273. * Test that type() in an action sets the content-type header.
  274. *
  275. * @return void
  276. */
  277. public function testContentTypeInAction()
  278. {
  279. $this->get('/tests_apps/set_type');
  280. $this->assertHeader('Content-Type', 'application/json; charset=UTF-8');
  281. $this->assertContentType('json');
  282. $this->assertContentType('application/json');
  283. }
  284. /**
  285. * Test the content assertion.
  286. *
  287. * @return void
  288. */
  289. public function testAssertResponseContains()
  290. {
  291. $this->_response = new Response();
  292. $this->_response->body('Some content');
  293. $this->assertResponseContains('content');
  294. }
  295. /**
  296. * Test the negated content assertion.
  297. *
  298. * @return void
  299. */
  300. public function testAssertResponseNotContains()
  301. {
  302. $this->_response = new Response();
  303. $this->_response->body('Some content');
  304. $this->assertResponseNotContains('contents');
  305. }
  306. /**
  307. * Test that works in tandem with testEventManagerReset2 to
  308. * test the EventManager reset.
  309. *
  310. * The return value is passed to testEventManagerReset2 as
  311. * an arguments.
  312. *
  313. * @return \Cake\Event\EventManager
  314. */
  315. public function testEventManagerReset1()
  316. {
  317. return EventManager::instance();
  318. }
  319. /**
  320. * Test if the EventManager is reset between tests.
  321. *
  322. * @depends testEventManagerReset1
  323. * @return void
  324. */
  325. public function testEventManagerReset2($prevEventManager)
  326. {
  327. $this->assertNotSame($prevEventManager, EventManager::instance());
  328. }
  329. }