RequestActionController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. use function Cake\Collection\collection;
  20. /**
  21. * RequestActionController class
  22. */
  23. class RequestActionController extends AppController
  24. {
  25. /**
  26. * The default model to use.
  27. *
  28. * @var string|null
  29. */
  30. protected ?string $modelClass = 'Posts';
  31. /**
  32. * test_request_action method
  33. *
  34. * @return \Cake\Http\Response
  35. */
  36. public function test_request_action()
  37. {
  38. return $this->response->withStringBody('This is a test');
  39. }
  40. /**
  41. * another_ra_test method
  42. *
  43. * @param mixed $id
  44. * @param mixed $other
  45. * @return \Cake\Http\Response
  46. */
  47. public function another_ra_test($id, $other)
  48. {
  49. return $this->response->withStringBody($id + $other);
  50. }
  51. /**
  52. * normal_request_action method
  53. *
  54. * @return \Cake\Http\Response
  55. */
  56. public function normal_request_action()
  57. {
  58. return $this->response->withStringBody('Hello World');
  59. }
  60. /**
  61. * returns $this->here as body
  62. *
  63. * @return \Cake\Http\Response
  64. */
  65. public function return_here()
  66. {
  67. return $this->response->withStringBody($this->here);
  68. }
  69. /**
  70. * post pass, testing post passing
  71. *
  72. * @return \Cake\Http\Response
  73. */
  74. public function post_pass()
  75. {
  76. return $this->response->withStringBody(json_encode($this->request->getData()));
  77. }
  78. /**
  79. * query pass, testing query passing
  80. *
  81. * @return \Cake\Http\Response
  82. */
  83. public function query_pass()
  84. {
  85. return $this->response->withStringBody(json_encode($this->request->getQueryParams()));
  86. }
  87. /**
  88. * cookie pass, testing cookie passing
  89. *
  90. * @return \Cake\Http\Response
  91. */
  92. public function cookie_pass()
  93. {
  94. return $this->response->withStringBody(json_encode($this->request->getCookieParams()));
  95. }
  96. /**
  97. * test param passing and parsing.
  98. *
  99. * @return \Cake\Http\Response
  100. */
  101. public function params_pass()
  102. {
  103. return $this->response->withStringBody(json_encode([
  104. 'params' => $this->request->getAttribute('params'),
  105. 'base' => $this->request->getAttribute('base'),
  106. 'here' => $this->request->getRequestTarget(),
  107. 'webroot' => $this->request->getAttribute('webroot'),
  108. 'query' => $this->request->getQueryParams(),
  109. 'url' => $this->request->getUri()->getPath(),
  110. 'contentType' => $this->request->contentType(),
  111. ]));
  112. }
  113. /**
  114. * param check method.
  115. *
  116. * @return \Cake\Http\Response
  117. */
  118. public function param_check()
  119. {
  120. $this->autoRender = false;
  121. $content = '';
  122. if ($this->request->getParam('0')) {
  123. $content = 'return found';
  124. }
  125. return $this->response->withStringBody($content);
  126. }
  127. /**
  128. * Tests session transmission
  129. *
  130. * @return \Cake\Http\Response
  131. */
  132. public function session_test()
  133. {
  134. return $this->response->withStringBody($this->request->getSession()->read('foo'));
  135. }
  136. /**
  137. * Tests input data transmission
  138. *
  139. * @return \Cake\Http\Response
  140. */
  141. public function input_test()
  142. {
  143. $text = json_decode((string)$this->request->getBody())->hello;
  144. return $this->response->withStringBody($text);
  145. }
  146. /**
  147. * Tests exception handling
  148. *
  149. * @throws \Cake\Http\Exception\NotFoundException
  150. * @return void
  151. */
  152. public function error_method(): never
  153. {
  154. throw new NotFoundException('Not there or here.');
  155. }
  156. /**
  157. * Tests uploaded files
  158. *
  159. * @return \Cake\Http\Response
  160. */
  161. public function uploaded_files()
  162. {
  163. $files = Hash::flatten($this->request->getUploadedFiles());
  164. $names = collection($files)->map(function (UploadedFileInterface $file) {
  165. return $file->getClientFilename();
  166. });
  167. return $this->response->withStringBody(json_encode($names));
  168. }
  169. }