GetFromQueryParamTagTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\QueryParameters;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamTag;
  5. use Knuckles\Scribe\Tests\Fixtures\TestController;
  6. use Knuckles\Scribe\Tools\DocumentationConfig;
  7. use Mpociot\Reflection\DocBlock\Tag;
  8. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  9. use PHPUnit\Framework\TestCase;
  10. class GetFromQueryParamTagTest extends TestCase
  11. {
  12. use ArraySubsetAsserts;
  13. /** @test */
  14. public function can_fetch_from_queryparam_tag()
  15. {
  16. $strategy = new GetFromQueryParamTag(new DocumentationConfig([]));
  17. $tags = [
  18. new Tag('queryParam', 'location_id required The id of the location.'),
  19. new Tag('queryParam', 'user_id required The id of the user. Example: me'),
  20. new Tag('queryParam', 'page required The page number. Example: 4'),
  21. new Tag('queryParam', 'filters.* The filters.'),
  22. new Tag('queryParam', 'url_encoded Used for testing that URL parameters will be URL-encoded where needed. Example: + []&='),
  23. ];
  24. $results = $strategy->getQueryParametersFromDocBlock($tags);
  25. $this->assertArraySubset([
  26. 'location_id' => [
  27. 'required' => true,
  28. 'description' => 'The id of the location.',
  29. ],
  30. 'user_id' => [
  31. 'required' => true,
  32. 'description' => 'The id of the user.',
  33. 'value' => 'me',
  34. ],
  35. 'page' => [
  36. 'required' => true,
  37. 'description' => 'The page number.',
  38. 'value' => '4',
  39. ],
  40. 'filters.*' => [
  41. 'required' => false,
  42. 'description' => 'The filters.',
  43. ],
  44. ], $results);
  45. }
  46. /** @test */
  47. public function can_fetch_from_form_request_method_argument()
  48. {
  49. $methodName = 'withFormRequestParameter';
  50. $method = new \ReflectionMethod(TestController::class, $methodName);
  51. $route = new Route(['POST'], "/$methodName", ['uses' => TestController::class . "@$methodName"]);
  52. $strategy = new GetFromQueryParamTag(new DocumentationConfig([]));
  53. $results = $strategy->getQueryParametersFromFormRequestOrMethod($route, $method);
  54. $this->assertArraySubset([
  55. 'location_id' => [
  56. 'required' => true,
  57. 'description' => 'The id of the location.',
  58. ],
  59. 'user_id' => [
  60. 'required' => true,
  61. 'description' => 'The id of the user.',
  62. 'value' => 'me',
  63. ],
  64. 'page' => [
  65. 'required' => true,
  66. 'description' => 'The page number.',
  67. 'value' => '4',
  68. ],
  69. 'filters.*' => [
  70. 'required' => false,
  71. 'description' => 'The filters.',
  72. ],
  73. ], $results);
  74. }
  75. }