From 9bb545ad309c208461910e41cf3e4bc43397229a Mon Sep 17 00:00:00 2001 From: Florian Rey Date: Tue, 5 Sep 2017 11:22:19 +0200 Subject: [PATCH] Handle DateTimeImmutable & DateTimeInterface --- .../Parser/ApiResourceMetadataParser.php | 4 ++- tests/Fixtures/Resource/NewsResource.php | 26 +++++++++++++-- tests/Fixtures/res/resources.yml | 10 +++++- .../ApiDoc/ApiResourceMetadataParserTest.php | 32 +++++++++++++++++-- .../ResourceMetadataFactoryTest.php | 16 +++++++++- .../ApiDoc/Extractor/ApiDocExtractorTest.php | 32 +++++++++++++++++-- .../ApiDoc/Swagger/SwaggerIntegrationTest.php | 12 +++++++ 7 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/Bridge/Nelmio/ApiDoc/Parser/ApiResourceMetadataParser.php b/src/Bridge/Nelmio/ApiDoc/Parser/ApiResourceMetadataParser.php index b55e5eb..5b5ceb7 100644 --- a/src/Bridge/Nelmio/ApiDoc/Parser/ApiResourceMetadataParser.php +++ b/src/Bridge/Nelmio/ApiDoc/Parser/ApiResourceMetadataParser.php @@ -83,7 +83,9 @@ private function doParse($class) private function resolveActualType(ResourceAttributeMetadata $attributeMetadata) { - if (\DateTime::class === ltrim($attributeMetadata->getOriginalType(), '\\')) { + $originalTypeClass = ltrim($attributeMetadata->getOriginalType(), '\\'); + + if (in_array($originalTypeClass, [\DateTime::class, \DateTimeImmutable::class, \DateTimeInterface::class])) { return [DataTypes::DATETIME, null]; } diff --git a/tests/Fixtures/Resource/NewsResource.php b/tests/Fixtures/Resource/NewsResource.php index c2140aa..ac17c53 100644 --- a/tests/Fixtures/Resource/NewsResource.php +++ b/tests/Fixtures/Resource/NewsResource.php @@ -35,9 +35,15 @@ class NewsResource */ private $content; - /** @var \DateTime */ + /** @var \DateTimeImmutable */ private $createdAt; + /** @var \DateTime */ + private $updatedAt; + + /** @var \DateTimeInterface */ + private $deletedAt; + /** @var string|null */ private $image; @@ -59,7 +65,9 @@ public function __construct( int $identifier, string $title, string $content, - \DateTime $createdAt, + \DateTimeImmutable $createdAt, + \DateTime $updatedAt, + \DateTimeInterface $deletedAt, string $image = null, bool $urgent = false, array $references = [] @@ -68,6 +76,8 @@ public function __construct( $this->title = $title; $this->content = $content; $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; + $this->deletedAt = $deletedAt; $this->image = $image; $this->urgent = $urgent; $this->references = $references; @@ -88,11 +98,21 @@ public function getContent(): string return $this->content; } - public function getCreatedAt(): \DateTime + public function getCreatedAt(): \DateTimeImmutable { return $this->createdAt; } + public function getUpdatedAt(): \DateTime + { + return $this->updatedAt; + } + + public function getDeletedAt(): \DateTimeInterface + { + return $this->deletedAt; + } + public function getImage(): string { return $this->image; diff --git a/tests/Fixtures/res/resources.yml b/tests/Fixtures/res/resources.yml index 3f04764..f26d223 100644 --- a/tests/Fixtures/res/resources.yml +++ b/tests/Fixtures/res/resources.yml @@ -6,8 +6,16 @@ resources: attributes: createdAt: type: string - originalType: \DateTime + originalType: \DateTimeImmutable description: The news creation date formatted to ISO 8601 + updatedAt: + type: string + originalType: \DateTime + description: The news modification date formatted to ISO 8601 + deletedAt: + type: string + originalType: \DateTimeInterface + description: The news deletion date formatted to ISO 8601 image: type: file Elao.ArticleReference: Elao\ApiResourcesMetadata\Tests\Fixtures\Resource\ArticleReferenceResource diff --git a/tests/Functional/Bridge/Nelmio/ApiDoc/ApiResourceMetadataParserTest.php b/tests/Functional/Bridge/Nelmio/ApiDoc/ApiResourceMetadataParserTest.php index b517d56..ba4db8a 100644 --- a/tests/Functional/Bridge/Nelmio/ApiDoc/ApiResourceMetadataParserTest.php +++ b/tests/Functional/Bridge/Nelmio/ApiDoc/ApiResourceMetadataParserTest.php @@ -76,7 +76,7 @@ public function testParse(array $toParse, $expected) } const NEWS_RESOURCE_DUMP = <<<'DUMP' -array:7 [ +array:9 [ "createdAt" => array:5 [ "dataType" => "string" "actualType" => "datetime" @@ -84,6 +84,20 @@ public function testParse(array $toParse, $expected) "description" => "The news creation date formatted to ISO 8601" "readonly" => null ] + "updatedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news modification date formatted to ISO 8601" + "readonly" => null + ] + "deletedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news deletion date formatted to ISO 8601" + "readonly" => null + ] "image" => array:5 [ "dataType" => "file" "actualType" => "file" @@ -162,7 +176,7 @@ public function testParse(array $toParse, $expected) "description" => "News for the localized feed" "readonly" => null "subType" => "Elao.Newsfeed.News" - "children" => array:7 [ + "children" => array:9 [ "createdAt" => array:5 [ "dataType" => "string" "actualType" => "datetime" @@ -170,6 +184,20 @@ public function testParse(array $toParse, $expected) "description" => "The news creation date formatted to ISO 8601" "readonly" => null ] + "updatedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news modification date formatted to ISO 8601" + "readonly" => null + ] + "deletedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news deletion date formatted to ISO 8601" + "readonly" => null + ] "image" => array:5 [ "dataType" => "file" "actualType" => "file" diff --git a/tests/Functional/ResourceMetadataFactoryTest.php b/tests/Functional/ResourceMetadataFactoryTest.php index c2b08ab..046cbdd 100644 --- a/tests/Functional/ResourceMetadataFactoryTest.php +++ b/tests/Functional/ResourceMetadataFactoryTest.php @@ -138,14 +138,28 @@ public function testGetMetadataFor($class, $dump) +class: "Elao\ApiResourcesMetadata\Tests\Fixtures\Resource\NewsResource" +shortName: "Elao.Newsfeed.News" +description: "A single news from a newsfeed" - +attributes: array:7 [ + +attributes: array:9 [ "createdAt" => Elao\ApiResourcesMetadata\Attribute\ResourceAttributeMetadata { +name: "createdAt" +description: "The news creation date formatted to ISO 8601" +required: true +type: "string" + +originalType: "\DateTimeImmutable" + } + "updatedAt" => Elao\ApiResourcesMetadata\Attribute\ResourceAttributeMetadata { + +name: "updatedAt" + +description: "The news modification date formatted to ISO 8601" + +required: true + +type: "string" +originalType: "\DateTime" } + "deletedAt" => Elao\ApiResourcesMetadata\Attribute\ResourceAttributeMetadata { + +name: "deletedAt" + +description: "The news deletion date formatted to ISO 8601" + +required: true + +type: "string" + +originalType: "\DateTimeInterface" + } "image" => Elao\ApiResourcesMetadata\Attribute\ResourceAttributeMetadata { +name: "image" +description: null diff --git a/tests/Integration/Bridge/Nelmio/ApiDoc/Extractor/ApiDocExtractorTest.php b/tests/Integration/Bridge/Nelmio/ApiDoc/Extractor/ApiDocExtractorTest.php index a471063..4671257 100644 --- a/tests/Integration/Bridge/Nelmio/ApiDoc/Extractor/ApiDocExtractorTest.php +++ b/tests/Integration/Bridge/Nelmio/ApiDoc/Extractor/ApiDocExtractorTest.php @@ -55,7 +55,7 @@ public function testExtractedAnnotations() "description" => "News for the localized feed" "readonly" => null "subType" => "Elao.Newsfeed.News" - "children" => array:7 [ + "children" => array:9 [ "createdAt" => array:5 [ "dataType" => "string" "actualType" => "datetime" @@ -63,6 +63,20 @@ public function testExtractedAnnotations() "description" => "The news creation date formatted to ISO 8601" "readonly" => null ] + "updatedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news modification date formatted to ISO 8601" + "readonly" => null + ] + "deletedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news deletion date formatted to ISO 8601" + "readonly" => null + ] "image" => array:5 [ "dataType" => "file" "actualType" => "file" @@ -195,7 +209,7 @@ public function testExtractedAnnotations() "description" => "News for the localized feed" "readonly" => null "subType" => "Elao.Newsfeed.News" - "children" => array:7 [ + "children" => array:9 [ "createdAt" => array:5 [ "dataType" => "string" "actualType" => "datetime" @@ -203,6 +217,20 @@ public function testExtractedAnnotations() "description" => "The news creation date formatted to ISO 8601" "readonly" => null ] + "updatedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news modification date formatted to ISO 8601" + "readonly" => null + ] + "deletedAt" => array:5 [ + "dataType" => "string" + "actualType" => "datetime" + "required" => true + "description" => "The news deletion date formatted to ISO 8601" + "readonly" => null + ] "image" => array:5 [ "dataType" => "file" "actualType" => "file" diff --git a/tests/Integration/Bridge/Nelmio/ApiDoc/Swagger/SwaggerIntegrationTest.php b/tests/Integration/Bridge/Nelmio/ApiDoc/Swagger/SwaggerIntegrationTest.php index 887eee4..f451bcf 100644 --- a/tests/Integration/Bridge/Nelmio/ApiDoc/Swagger/SwaggerIntegrationTest.php +++ b/tests/Integration/Bridge/Nelmio/ApiDoc/Swagger/SwaggerIntegrationTest.php @@ -98,6 +98,16 @@ public function testSwaggerDump() "description":"The news creation date formatted to ISO 8601", "format":"date-time" }, + "updatedAt":{ + "type":"string", + "description":"The news modification date formatted to ISO 8601", + "format":"date-time" + }, + "deletedAt":{ + "type":"string", + "description":"The news deletion date formatted to ISO 8601", + "format":"date-time" + }, "identifier":{ "type":"integer", "description":"Unique identifier", @@ -130,6 +140,8 @@ public function testSwaggerDump() }, "required":[ "createdAt", + "updatedAt", + "deletedAt", "identifier", "title", "content",