GetFromHeaderAttributeTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\Headers;
  3. use Attribute;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Attributes\Header;
  6. use Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderAttribute;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. use PHPUnit\Framework\TestCase;
  9. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  10. use ReflectionClass;
  11. class GetFromHeaderAttributeTest extends TestCase
  12. {
  13. use ArraySubsetAsserts;
  14. /** @test */
  15. public function can_fetch_from_header_attribute()
  16. {
  17. $results = $this->getHeaderFromAttribute('methodWithAttributes');
  18. $this->assertArraySubset([
  19. 'Api-Version' => 'v1',
  20. ], $results);
  21. $this->assertArrayHasKey('Some-Custom', $results);
  22. $this->assertNotEmpty($results['Some-Custom']);
  23. }
  24. /** @test */
  25. public function can_fetch_child_of_header_attribute()
  26. {
  27. $results = $this->getHeaderFromAttribute('methodWithCustomHeaderAttribute');
  28. $this->assertArraySubset([
  29. 'Api-Version' => 'v1',
  30. ], $results);
  31. $this->assertArrayHasKey('hello', $results);
  32. $this->assertEquals('world', $results['hello']);
  33. }
  34. private function getHeaderFromAttribute(string $methodName): array
  35. {
  36. $endpoint = new class extends ExtractedEndpointData {
  37. public function __construct(array $parameters = []) {}
  38. };
  39. $endpoint->controller = new ReflectionClass(\Knuckles\Scribe\Tests\Strategies\Headers\HeaderAttributeTestController::class);
  40. $endpoint->method = $endpoint->controller->getMethod($methodName);
  41. $strategy = new GetFromHeaderAttribute(new DocumentationConfig([]));
  42. return $strategy($endpoint);
  43. }
  44. }
  45. #[Header("Api-Version", "v1")]
  46. class HeaderAttributeTestController
  47. {
  48. #[Header("Some-Custom")]
  49. public function methodWithAttributes()
  50. {
  51. }
  52. #[CustomHeaderClass()]
  53. public function methodWithCustomHeaderAttribute()
  54. {
  55. }
  56. }
  57. #[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
  58. class CustomHeaderClass extends Header
  59. {
  60. public function __construct()
  61. {
  62. parent::__construct('hello', 'world');
  63. }
  64. }