IntegrationTestCase.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. App::uses('MyControllerTestCase', 'Tools.TestSuite');
  3. App::uses('Router', 'Routing');
  4. App::uses('Dispatcher', 'Routing');
  5. App::uses('EventManager', 'Event');
  6. /**
  7. * A test case class intended to make integration tests of
  8. * your controllers easier.
  9. *
  10. * This class has been backported from 3.0
  11. *
  12. * This test class provides a number of helper methods and features
  13. * that make dispatching requests and checking their responses simpler.
  14. * It favours full integration tests over mock objects as you can test
  15. * more of your code easily and avoid some of the maintenance pitfalls
  16. * that mock objects create.
  17. */
  18. abstract class IntegrationTestCase extends MyControllerTestCase {
  19. /**
  20. * The data used to build the next request.
  21. *
  22. * @var array
  23. */
  24. protected $_requestData = [];
  25. /**
  26. * Session data to use in the next request.
  27. *
  28. * @var array
  29. */
  30. protected $_sessionData = [];
  31. /**
  32. * Cookie data to use in the next request.
  33. *
  34. * @var array
  35. */
  36. protected $_cookieData = [];
  37. /**
  38. * Configure the data for the *next* request.
  39. *
  40. * This data is cleared in the tearDown() method.
  41. *
  42. * You can call this method multiple times to append into
  43. * the current state.
  44. *
  45. * @param array $data The request data to use.
  46. * @return void
  47. */
  48. public function configRequest(array $data) {
  49. $this->_requestData = $data + $this->_requestData;
  50. }
  51. /**
  52. * Set session data.
  53. *
  54. * This method lets you configure the session data
  55. * you want to be used for requests that follow. The session
  56. * state is reset in each tearDown().
  57. *
  58. * You can call this method multiple times to append into
  59. * the current state.
  60. *
  61. * @param array $data The session data to use.
  62. * @return void
  63. */
  64. public function session(array $data) {
  65. $this->_sessionData = $data + $this->_sessionData;
  66. }
  67. /**
  68. * Set a request cookie for future requests.
  69. *
  70. * This method lets you configure the session data
  71. * you want to be used for requests that follow. The session
  72. * state is reset in each tearDown().
  73. *
  74. * You can call this method multiple times to append into
  75. * the current state.
  76. *
  77. * @param string $name The cookie name to use.
  78. * @param mixed $value The value of the cookie.
  79. * @return void
  80. */
  81. public function cookie($name, $value) {
  82. $this->_cookieData[$name] = $value;
  83. }
  84. /**
  85. * Perform a GET request using the current request data.
  86. *
  87. * The response of the dispatched request will be stored as
  88. * a property. You can use various assert methods to check the
  89. * response.
  90. *
  91. * @param string $url The url to request.
  92. * @return void
  93. */
  94. public function get($url) {
  95. return $this->_sendRequest($url, 'GET');
  96. }
  97. /**
  98. * Perform a POST request using the current request data.
  99. *
  100. * The response of the dispatched request will be stored as
  101. * a property. You can use various assert methods to check the
  102. * response.
  103. *
  104. * @param string $url The url to request.
  105. * @param array $data The data for the request.
  106. * @return void
  107. */
  108. public function post($url, $data = []) {
  109. return $this->_sendRequest($url, 'POST', $data);
  110. }
  111. /**
  112. * Perform a PATCH request using the current request data.
  113. *
  114. * The response of the dispatched request will be stored as
  115. * a property. You can use various assert methods to check the
  116. * response.
  117. *
  118. * @param string $url The url to request.
  119. * @param array $data The data for the request.
  120. * @return void
  121. */
  122. public function patch($url, $data = []) {
  123. return $this->_sendRequest($url, 'PATCH', $data);
  124. }
  125. /**
  126. * Perform a PUT request using the current request data.
  127. *
  128. * The response of the dispatched request will be stored as
  129. * a property. You can use various assert methods to check the
  130. * response.
  131. *
  132. * @param string $url The url to request.
  133. * @param array $data The data for the request.
  134. * @return void
  135. */
  136. public function put($url, $data = []) {
  137. return $this->_sendRequest($url, 'PUT', $data);
  138. }
  139. /**
  140. * Perform a DELETE request using the current request data.
  141. *
  142. * The response of the dispatched request will be stored as
  143. * a property. You can use various assert methods to check the
  144. * response.
  145. *
  146. * @param string $url The url to request.
  147. * @return void
  148. */
  149. public function delete($url) {
  150. return $this->_sendRequest($url, 'DELETE');
  151. }
  152. /**
  153. * Create and send the request into a Dispatcher instance.
  154. *
  155. * Receives and stores the response for future inspection.
  156. *
  157. * @param string $url The url
  158. * @param string $method The HTTP method
  159. * @param array|null $data The request data.
  160. * @return mixed
  161. * @throws \Exception
  162. */
  163. protected function _sendRequest($url, $method, $data = []) {
  164. $options = array(
  165. 'data' => $data,
  166. 'method' => $method,
  167. 'return' => 'vars'
  168. );
  169. if (isset($this->_requestData['headers'])) {
  170. foreach ($this->_requestData['headers'] as $k => $v) {
  171. $env['HTTP_' . str_replace('-', '_', strtoupper($k))] = $v;
  172. }
  173. unset($this->_requestData['headers']);
  174. }
  175. CakeSession::write($this->_sessionData);
  176. $result = $this->testAction($url, $options);
  177. $this->_response = $this->controller->response;
  178. $this->_request = $this->controller->request;
  179. $this->_requestSession = $this->controller->Session;
  180. return $result;
  181. }
  182. /**
  183. * Fetch a view variable by name.
  184. *
  185. * If the view variable does not exist null will be returned.
  186. *
  187. * @param string $name The view variable to get.
  188. * @return mixed The view variable if set.
  189. */
  190. public function viewVariable($name) {
  191. if (empty($this->controller->viewVars)) {
  192. $this->fail('There are no view variables, perhaps you need to run a request?');
  193. }
  194. if (isset($this->controller->viewVars[$name])) {
  195. return $this->controller->viewVars[$name];
  196. }
  197. return null;
  198. }
  199. /**
  200. * Assert that the response status code is in the 2xx range.
  201. *
  202. * @return void
  203. */
  204. public function assertResponseOk() {
  205. $this->_assertStatus(200, 204, 'Status code is not between 200 and 204');
  206. }
  207. /**
  208. * Assert that the response status code is in the 4xx range.
  209. *
  210. * @return void
  211. */
  212. public function assertResponseError() {
  213. $this->_assertStatus(400, 417, 'Status code is not between 400 and 417');
  214. }
  215. /**
  216. * Assert that the response status code is in the 5xx range.
  217. *
  218. * @return void
  219. */
  220. public function assertResponseFailure() {
  221. $this->_assertStatus(500, 505, 'Status code is not between 500 and 505');
  222. }
  223. /**
  224. * Asserts a specific response status code.
  225. *
  226. * @param int $code Status code to assert.
  227. * @return void
  228. */
  229. public function assertResponseCode($code) {
  230. $actual = $this->_response->statusCode();
  231. $this->_assertStatus($code, $code, 'Status code is not ' . $code . ' but ' . $actual);
  232. }
  233. /**
  234. * Helper method for status assertions.
  235. *
  236. * @param int $min Min status code.
  237. * @param int $max Max status code.
  238. * @param string $message The error message.
  239. * @return void
  240. */
  241. protected function _assertStatus($min, $max, $message) {
  242. if (!$this->_response) {
  243. $this->fail('No response set, cannot assert status code.');
  244. }
  245. $status = $this->_response->statusCode();
  246. $this->assertGreaterThanOrEqual($min, $status, $message);
  247. $this->assertLessThanOrEqual($max, $status, $message);
  248. }
  249. /**
  250. * Assert that the Location header is correct.
  251. *
  252. * @param string|array $url The url you expected the client to go to. This
  253. * can either be a string URL or an array compatible with Router::url()
  254. * @param string $message The failure message that will be appended to the generated message.
  255. * @return void
  256. */
  257. public function assertRedirect($url, $message = '') {
  258. if (!$this->_response) {
  259. $this->fail('No response set, cannot assert location header. ' . $message);
  260. }
  261. $result = $this->_response->header();
  262. if (empty($result['Location'])) {
  263. $this->fail('No location header set. ' . $message);
  264. }
  265. $this->assertEquals(Router::url($url, ['_full' => true]), $result['Location'], $message);
  266. }
  267. /**
  268. * Assert response headers
  269. *
  270. * @param string $header The header to check
  271. * @param string $content The content to check for.
  272. * @param string $message The failure message that will be appended to the generated message.
  273. * @return void
  274. */
  275. public function assertHeader($header, $content, $message = '') {
  276. if (!$this->_response) {
  277. $this->fail('No response set, cannot assert headers. ' . $message);
  278. }
  279. $headers = $this->_response->header();
  280. if (!isset($headers[$header])) {
  281. $this->fail("The '$header' header is not set. " . $message);
  282. }
  283. $this->assertEquals($headers[$header], $content, $message);
  284. }
  285. /**
  286. * Assert content type
  287. *
  288. * @param string $type The content-type to check for.
  289. * @param string $message The failure message that will be appended to the generated message.
  290. * @return void
  291. */
  292. public function assertContentType($type, $message = '') {
  293. if (!$this->_response) {
  294. $this->fail('No response set, cannot assert content-type. ' . $message);
  295. }
  296. $alias = $this->_response->getMimeType($type);
  297. if ($alias !== false) {
  298. $type = $alias;
  299. }
  300. $result = $this->_response->type();
  301. $this->assertEquals($type, $result, $message);
  302. }
  303. /**
  304. * Assert content exists in the response body.
  305. *
  306. * @param string $content The content to check for.
  307. * @param string $message The failure message that will be appended to the generated message.
  308. * @return void
  309. */
  310. public function assertResponseContains($content, $message = '') {
  311. if (!$this->_response) {
  312. $this->fail('No response set, cannot assert content. ' . $message);
  313. }
  314. $this->assertContains($content, $this->_response->body(), $message);
  315. }
  316. /**
  317. * Assert that the search string was in the template name.
  318. *
  319. * @param string $content The content to check for.
  320. * @param string $message The failure message that will be appended to the generated message.
  321. * @return void
  322. */
  323. public function assertTemplate($content, $message = '') {
  324. if (!$this->_viewName) {
  325. $this->fail('No view name stored. ' . $message);
  326. }
  327. $this->assertContains($content, $this->_viewName, $message);
  328. }
  329. /**
  330. * Assert that the search string was in the layout name.
  331. *
  332. * @param string $content The content to check for.
  333. * @param string $message The failure message that will be appended to the generated message.
  334. * @return void
  335. */
  336. public function assertLayout($content, $message = '') {
  337. if (!$this->_layoutName) {
  338. $this->fail('No layout name stored. ' . $message);
  339. }
  340. $this->assertContains($content, $this->_layoutName, $message);
  341. }
  342. /**
  343. * Assert session contents
  344. *
  345. * @param string $expected The expected contents.
  346. * @param string $path The session data path. Uses Hash::get() compatible notation
  347. * @param string $message The failure message that will be appended to the generated message.
  348. * @return void
  349. */
  350. public function assertSession($expected, $path, $message = '') {
  351. if (empty($this->_requestSession)) {
  352. $this->fail('There is no stored session data. Perhaps you need to run a request?');
  353. }
  354. $result = $this->_requestSession->read($path);
  355. $this->assertEquals($expected, $result, 'Session content differs. ' . $message);
  356. }
  357. /**
  358. * Assert cookie values
  359. *
  360. * @param string $expected The expected contents.
  361. * @param string $name The cookie name.
  362. * @param string $message The failure message that will be appended to the generated message.
  363. * @return void
  364. */
  365. public function assertCookie($expected, $name, $message = '') {
  366. if (empty($this->_response)) {
  367. $this->fail('Not response set, cannot assert cookies.');
  368. }
  369. $result = $this->_response->cookie($name);
  370. $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
  371. }
  372. }