GetFromFormRequestTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 'even_more_param' => [
  45. 'type' => 'string[]',
  46. 'required' => false,
  47. 'description' => '',
  48. ],
  49. 'book' => [
  50. 'type' => 'object',
  51. 'description' => '',
  52. 'required' => false,
  53. 'example' => [],
  54. ],
  55. 'book.name' => [
  56. 'type' => 'string',
  57. 'description' => '',
  58. 'required' => false,
  59. ],
  60. 'book.author_id' => [
  61. 'type' => 'integer',
  62. 'description' => '',
  63. 'required' => false,
  64. ],
  65. 'book.pages_count' => [
  66. 'type' => 'integer',
  67. 'description' => '',
  68. 'required' => false,
  69. ],
  70. 'ids' => [
  71. 'type' => 'integer[]',
  72. 'description' => '',
  73. 'required' => false,
  74. ],
  75. 'users' => [
  76. 'type' => 'object[]',
  77. 'description' => '',
  78. 'required' => false,
  79. 'example' => [[]],
  80. ],
  81. 'users[].first_name' => [
  82. 'type' => 'string',
  83. 'description' => 'The first name of the user.',
  84. 'required' => false,
  85. 'example' => 'John',
  86. ],
  87. 'users[].last_name' => [
  88. 'type' => 'string',
  89. 'description' => 'The last name of the user.',
  90. 'required' => false,
  91. 'example' => 'Doe',
  92. ],
  93. ], $results);
  94. $this->assertIsArray($results['ids']['example']);
  95. }
  96. /** @test */
  97. public function can_fetch_queryparams_from_form_request()
  98. {
  99. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParams');
  100. $results = $this->fetchViaQueryParams($method);
  101. $this->assertArraySubset([
  102. 'q_param' => [
  103. 'type' => 'integer',
  104. 'description' => 'The param.',
  105. 'required' => true,
  106. 'example' => 9,
  107. ],
  108. ], $results);
  109. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParamsComment');
  110. $results = $this->fetchViaQueryParams($method);
  111. $this->assertArraySubset([
  112. 'type' => 'integer',
  113. 'description' => '',
  114. 'required' => true,
  115. ], $results['q_param']);
  116. }
  117. /** @test */
  118. public function will_ignore_not_relevant_form_request()
  119. {
  120. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  121. $this->assertEquals([], $this->fetchViaQueryParams($method));
  122. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParams');
  123. $this->assertEquals([], $this->fetchViaBodyParams($method));
  124. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameterQueryParamsComment');
  125. $this->assertEquals([], $this->fetchViaBodyParams($method));
  126. }
  127. /** @test */
  128. public function allows_customisation_of_form_request_instantiation()
  129. {
  130. $controllerMethod = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  131. Globals::$__instantiateFormRequestUsing = function ($className, $route, $method) use (&$controllerMethod) {
  132. Assert::assertEquals(TestRequest::class, $className);
  133. Assert::assertEquals(null, $route);
  134. Assert::assertEquals($controllerMethod, $method);
  135. return new TestRequestQueryParams;
  136. };
  137. $this->fetchViaBodyParams($controllerMethod);
  138. Globals::$__instantiateFormRequestUsing = null;
  139. }
  140. protected function fetchViaBodyParams(\ReflectionMethod $method): array
  141. {
  142. $strategy = new BodyParameters\GetFromFormRequest(new DocumentationConfig([]));
  143. return $strategy->getParametersFromFormRequest($method);
  144. }
  145. protected function fetchViaQueryParams(\ReflectionMethod $method): array
  146. {
  147. $strategy = new QueryParameters\GetFromFormRequest(new DocumentationConfig([]));
  148. return $strategy->getParametersFromFormRequest($method);
  149. }
  150. }