GetFromFormRequestTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies;
  3. use Knuckles\Scribe\Extracting\Strategies\BodyParameters;
  4. use Knuckles\Scribe\Extracting\Strategies\QueryParameters;
  5. use Knuckles\Scribe\Tests\BaseLaravelTest;
  6. use Knuckles\Scribe\Tests\Fixtures\TestController;
  7. use Knuckles\Scribe\Tests\Fixtures\TestRequest;
  8. use Knuckles\Scribe\Tests\Fixtures\TestRequestQueryParams;
  9. use Knuckles\Scribe\Tools\DocumentationConfig;
  10. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  11. use Knuckles\Scribe\Tools\Globals;
  12. use PHPUnit\Framework\Assert;
  13. class GetFromFormRequestTest extends BaseLaravelTest
  14. {
  15. use ArraySubsetAsserts;
  16. /** @test */
  17. public function can_fetch_bodyparams_from_form_request()
  18. {
  19. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  20. $results = $this->fetchViaBodyParams($method);
  21. $this->assertArraySubset([
  22. 'user_id' => [
  23. 'type' => 'integer',
  24. 'required' => true,
  25. 'description' => 'The id of the user.',
  26. 'example' => 9,
  27. ],
  28. 'room_id' => [
  29. 'type' => 'string',
  30. 'required' => false,
  31. 'description' => 'The id of the room.',
  32. ],
  33. 'forever' => [
  34. 'type' => 'boolean',
  35. 'required' => false,
  36. 'description' => 'Whether to ban the user forever.',
  37. 'example' => false,
  38. ],
  39. 'another_one' => [
  40. 'type' => 'number',
  41. 'required' => false,
  42. 'description' => 'Just need something here.',
  43. ],
  44. 'no_example_attribute' => [
  45. 'type' => 'number',
  46. 'required' => false,
  47. 'description' => 'Attribute without example.',
  48. 'example' => null,
  49. ],
  50. 'even_more_param' => [
  51. 'type' => 'string[]',
  52. 'required' => false,
  53. 'description' => '',
  54. ],
  55. 'book' => [
  56. 'type' => 'object',
  57. 'description' => '',
  58. 'required' => false,
  59. 'example' => [],
  60. ],
  61. 'book.name' => [
  62. 'type' => 'string',
  63. 'description' => '',
  64. 'required' => false,
  65. ],
  66. 'book.author_id' => [
  67. 'type' => 'integer',
  68. 'description' => '',
  69. 'required' => false,
  70. ],
  71. 'book.pages_count' => [
  72. 'type' => 'integer',
  73. 'description' => '',
  74. 'required' => false,
  75. ],
  76. 'ids' => [
  77. 'type' => 'integer[]',
  78. 'description' => '',
  79. 'required' => false,
  80. ],
  81. 'users' => [
  82. 'type' => 'object[]',
  83. 'description' => '',
  84. 'required' => false,
  85. 'example' => [[]],
  86. ],
  87. 'users[].first_name' => [
  88. 'type' => 'string',
  89. 'description' => 'The first name of the user.',
  90. 'required' => false,
  91. 'example' => 'John',
  92. ],
  93. 'users[].last_name' => [
  94. 'type' => 'string',
  95. 'description' => 'The last name of the user.',
  96. 'required' => false,
  97. 'example' => 'Doe',
  98. ],
  99. ], $results);
  100. $this->assertIsArray($results['ids']['example']);
  101. }
  102. /** @test */
  103. public function can_fetch_queryparams_from_form_request()
  104. {
  105. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParams');
  106. $results = $this->fetchViaQueryParams($method);
  107. $this->assertArraySubset([
  108. 'q_param' => [
  109. 'type' => 'integer',
  110. 'description' => 'The param.',
  111. 'required' => true,
  112. 'example' => 9,
  113. ],
  114. ], $results);
  115. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParamsComment');
  116. $results = $this->fetchViaQueryParams($method);
  117. $this->assertArraySubset([
  118. 'type' => 'integer',
  119. 'description' => '',
  120. 'required' => true,
  121. ], $results['q_param']);
  122. }
  123. /** @test */
  124. public function will_ignore_not_relevant_form_request()
  125. {
  126. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  127. $this->assertEquals([], $this->fetchViaQueryParams($method));
  128. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParams');
  129. $this->assertEquals([], $this->fetchViaBodyParams($method));
  130. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParamsComment');
  131. $this->assertEquals([], $this->fetchViaBodyParams($method));
  132. }
  133. /** @test */
  134. public function allows_customisation_of_form_request_instantiation()
  135. {
  136. $controllerMethod = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  137. Globals::$__instantiateFormRequestUsing = function ($className, $route, $method) use (&$controllerMethod) {
  138. Assert::assertEquals(TestRequest::class, $className);
  139. Assert::assertEquals(null, $route);
  140. Assert::assertEquals($controllerMethod, $method);
  141. return new TestRequestQueryParams;
  142. };
  143. $this->fetchViaBodyParams($controllerMethod);
  144. Globals::$__instantiateFormRequestUsing = null;
  145. }
  146. protected function fetchViaBodyParams(\ReflectionMethod $method): array
  147. {
  148. $strategy = new BodyParameters\GetFromFormRequest(new DocumentationConfig([]));
  149. return $strategy->getParametersFromFormRequest($method);
  150. }
  151. protected function fetchViaQueryParams(\ReflectionMethod $method): array
  152. {
  153. $strategy = new QueryParameters\GetFromFormRequest(new DocumentationConfig([]));
  154. return $strategy->getParametersFromFormRequest($method);
  155. }
  156. }