IntegrationTestCaseTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Network\Response;
  18. use Cake\Routing\DispatcherFactory;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\IntegrationTestCase;
  21. /**
  22. * Self test of the IntegrationTestCase
  23. */
  24. class IntegrationTestCaseTest extends IntegrationTestCase {
  25. /**
  26. * Setup method
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. Configure::write('App.namespace', 'TestApp');
  33. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  34. DispatcherFactory::clear();
  35. DispatcherFactory::add('Routing');
  36. DispatcherFactory::add('ControllerFactory');
  37. }
  38. /**
  39. * Test building a request.
  40. *
  41. * @return void
  42. */
  43. public function testRequestBuilding() {
  44. $this->configRequest([
  45. 'headers' => ['X-CSRF-Token' => 'abc123'],
  46. 'base' => '',
  47. 'webroot' => '/'
  48. ]);
  49. $this->cookie('split_token', 'def345');
  50. $this->session(['User' => ['id' => 1, 'username' => 'mark']]);
  51. $request = $this->_buildRequest('/tasks/add', 'POST', ['title' => 'First post']);
  52. $this->assertEquals('abc123', $request->header('X-CSRF-Token'));
  53. $this->assertEquals('tasks/add', $request->url);
  54. $this->assertEquals(['split_token' => 'def345'], $request->cookies);
  55. $this->assertEquals(['id' => '1', 'username' => 'mark'], $request->session()->read('User'));
  56. }
  57. /**
  58. * Test sending get requests.
  59. *
  60. * @return void
  61. */
  62. public function testGet() {
  63. $this->assertNull($this->_response);
  64. $this->get('/request_action/test_request_action');
  65. $this->assertNotEmpty($this->_response);
  66. $this->assertInstanceOf('Cake\Network\Response', $this->_response);
  67. $this->assertEquals('This is a test', $this->_response->body());
  68. }
  69. /**
  70. * Test sending requests stores references to controller/view/layout.
  71. *
  72. * @return void
  73. */
  74. public function testRequestSetsProperties() {
  75. $this->post('/posts/index');
  76. $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller);
  77. $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName);
  78. $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName);
  79. $this->assertTemplate('index');
  80. $this->assertLayout('default');
  81. }
  82. /**
  83. * Test error handling and error page rendering.
  84. *
  85. * @return void
  86. */
  87. public function testPostAndErrorHandling() {
  88. $this->post('/request_action/error_method');
  89. $this->assertResponseContains('Not there or here');
  90. $this->assertResponseContains('<!DOCTYPE html>');
  91. }
  92. /**
  93. * Test the responseOk status assertion
  94. *
  95. * @return void
  96. */
  97. public function testAssertResponseStatusCodes() {
  98. $this->_response = new Response();
  99. $this->_response->statusCode(200);
  100. $this->assertResponseOk();
  101. $this->_response->statusCode(201);
  102. $this->assertResponseOk();
  103. $this->_response->statusCode(204);
  104. $this->assertResponseOk();
  105. $this->_response->statusCode(400);
  106. $this->assertResponseError();
  107. $this->_response->statusCode(417);
  108. $this->assertResponseError();
  109. $this->_response->statusCode(500);
  110. $this->assertResponseFailure();
  111. $this->_response->statusCode(505);
  112. $this->assertResponseFailure();
  113. }
  114. /**
  115. * Test the location header assertion.
  116. *
  117. * @return void
  118. */
  119. public function testAssertRedirect() {
  120. $this->_response = new Response();
  121. $this->_response->header('Location', 'http://localhost/tasks/index');
  122. $this->assertRedirect('/tasks/index');
  123. $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
  124. }
  125. /**
  126. * Test the header assertion.
  127. *
  128. * @return void
  129. */
  130. public function testAssertHeader() {
  131. $this->_response = new Response();
  132. $this->_response->header('Etag', 'abc123');
  133. $this->assertHeader('Etag', 'abc123');
  134. }
  135. /**
  136. * Test the content type assertion.
  137. *
  138. * @return void
  139. */
  140. public function testAssertContentType() {
  141. $this->_response = new Response();
  142. $this->_response->type('json');
  143. $this->assertContentType('json');
  144. $this->assertContentType('application/json');
  145. }
  146. /**
  147. * Test the content assertion.
  148. *
  149. * @return void
  150. */
  151. public function testAssertResponseContains() {
  152. $this->_response = new Response();
  153. $this->_response->body('Some content');
  154. $this->assertResponseContains('content');
  155. }
  156. }