RequestActionController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <https://book.cakephp.org/view/1196/Testing>
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace TestApp\Controller;
  15. use Cake\Http\Exception\NotFoundException;
  16. use Cake\Utility\Hash;
  17. use Psr\Http\Message\UploadedFileInterface;
  18. /**
  19. * RequestActionController class
  20. */
  21. class RequestActionController extends AppController
  22. {
  23. /**
  24. * The default model to use.
  25. *
  26. * @var string
  27. */
  28. public $modelClass = 'Posts';
  29. /**
  30. * test_request_action method
  31. *
  32. * @return \Cake\Http\Response
  33. */
  34. public function test_request_action()
  35. {
  36. return $this->response->withStringBody('This is a test');
  37. }
  38. /**
  39. * another_ra_test method
  40. *
  41. * @param mixed $id
  42. * @param mixed $other
  43. * @return \Cake\Http\Response
  44. */
  45. public function another_ra_test($id, $other)
  46. {
  47. return $this->response->withStringBody($id + $other);
  48. }
  49. /**
  50. * normal_request_action method
  51. *
  52. * @return \Cake\Http\Response
  53. */
  54. public function normal_request_action()
  55. {
  56. return $this->response->withStringBody('Hello World');
  57. }
  58. /**
  59. * returns $this->here as body
  60. *
  61. * @return \Cake\Http\Response
  62. */
  63. public function return_here()
  64. {
  65. return $this->response->withStringBody($this->here);
  66. }
  67. /**
  68. * paginate_request_action method
  69. *
  70. * @return void
  71. */
  72. public function paginate_request_action()
  73. {
  74. $data = $this->paginate();
  75. $this->autoRender = false;
  76. }
  77. /**
  78. * post pass, testing post passing
  79. *
  80. * @return \Cake\Http\Response
  81. */
  82. public function post_pass()
  83. {
  84. return $this->response->withStringBody(json_encode($this->request->getData()));
  85. }
  86. /**
  87. * query pass, testing query passing
  88. *
  89. * @return \Cake\Http\Response
  90. */
  91. public function query_pass()
  92. {
  93. return $this->response->withStringBody(json_encode($this->request->getQueryParams()));
  94. }
  95. /**
  96. * cookie pass, testing cookie passing
  97. *
  98. * @return \Cake\Http\Response
  99. */
  100. public function cookie_pass()
  101. {
  102. return $this->response->withStringBody(json_encode($this->request->getCookieParams()));
  103. }
  104. /**
  105. * test param passing and parsing.
  106. *
  107. * @return \Cake\Http\Response
  108. */
  109. public function params_pass()
  110. {
  111. return $this->response->withStringBody(json_encode([
  112. 'params' => $this->request->getAttribute('params'),
  113. 'base' => $this->request->getAttribute('base'),
  114. 'here' => $this->request->getRequestTarget(),
  115. 'webroot' => $this->request->getAttribute('webroot'),
  116. 'query' => $this->request->getQueryParams(),
  117. 'url' => $this->request->getUri()->getPath(),
  118. 'contentType' => $this->request->contentType(),
  119. ]));
  120. }
  121. /**
  122. * param check method.
  123. *
  124. * @return \Cake\Http\Response
  125. */
  126. public function param_check()
  127. {
  128. $this->autoRender = false;
  129. $content = '';
  130. if ($this->request->getParam('0')) {
  131. $content = 'return found';
  132. }
  133. return $this->response->withStringBody($content);
  134. }
  135. /**
  136. * Tests session transmission
  137. *
  138. * @return \Cake\Http\Response
  139. */
  140. public function session_test()
  141. {
  142. return $this->response->withStringBody($this->request->getSession()->read('foo'));
  143. }
  144. /**
  145. * Tests input data transmission
  146. *
  147. * @return \Cake\Http\Response
  148. */
  149. public function input_test()
  150. {
  151. $text = $this->request->input('json_decode')->hello;
  152. return $this->response->withStringBody($text);
  153. }
  154. /**
  155. * Tests exception handling
  156. *
  157. * @throws \Cake\Http\Exception\NotFoundException
  158. * @return void
  159. */
  160. public function error_method()
  161. {
  162. throw new NotFoundException('Not there or here.');
  163. }
  164. /**
  165. * Tests uploaded files
  166. *
  167. * @return \Cake\Http\Response
  168. */
  169. public function uploaded_files()
  170. {
  171. $files = Hash::flatten($this->request->getUploadedFiles());
  172. $names = collection($files)->map(function (UploadedFileInterface $file) {
  173. return $file->getClientFilename();
  174. });
  175. return $this->response->withStringBody(json_encode($names));
  176. }
  177. }