RequestActionController.php 4.8 KB

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