diff --git a/Tests/Model/Issue46/Article.php b/Tests/Model/Issue46/Article.php index ac48748..e9b53f3 100644 --- a/Tests/Model/Issue46/Article.php +++ b/Tests/Model/Issue46/Article.php @@ -15,16 +15,10 @@ class Article ] private $iri; - /** - * @Rest\Attribute(name="id", type="string") - */ #[Rest\Attribute(name: 'id', type: 'string')] private $id; - /** - * @Rest\ManyToOne(name="section", targetEntity="Section") - */ - #[Rest\ManyToOne(name: 'section', targetEntity: 'Section')] + #[Rest\ManyToOne(name: 'section', targetEntity: Section::class)] private $section; public function setId($id): void diff --git a/Tests/Model/Issue46/Section.php b/Tests/Model/Issue46/Section.php index 7044a0e..a2fb68a 100644 --- a/Tests/Model/Issue46/Section.php +++ b/Tests/Model/Issue46/Section.php @@ -21,7 +21,7 @@ class Section #[Rest\Attribute(name: 'title', type: 'string')] private $title; - #[Rest\OneToMany(name: 'articleList', targetEntity: 'Article')] + #[Rest\OneToMany(name: 'articleList', targetEntity: Article::class)] private $articleList = []; public function setId($id): void diff --git a/Tests/Model/Issue75/Article.php b/Tests/Model/Issue75/Article.php index ea8671a..4d30695 100644 --- a/Tests/Model/Issue75/Article.php +++ b/Tests/Model/Issue75/Article.php @@ -9,11 +9,6 @@ #[Rest\Entity(key: 'articles')] class Article { - /** - * @Rest\Id - * - * @Rest\Attribute(name="@id", type="string") - */ #[ Rest\Id, Rest\Attribute(name: '@id', type: 'string') @@ -23,10 +18,10 @@ class Article #[Rest\Attribute(name: 'title', type: 'string')] private $title; - #[Rest\ManyToOne(name: 'tag', targetEntity: 'Tag')] + #[Rest\ManyToOne(name: 'tag', targetEntity: Tag::class)] private $tag; - #[Rest\OneToMany(name: 'tagList', targetEntity: 'Tag')] + #[Rest\OneToMany(name: 'tagList', targetEntity: Tag::class)] private $tagList; public function setTitle($title): void diff --git a/Tests/Model/Issue80/Article.php b/Tests/Model/Issue80/Article.php index 5a50736..a002613 100644 --- a/Tests/Model/Issue80/Article.php +++ b/Tests/Model/Issue80/Article.php @@ -11,16 +11,11 @@ class Article { /** * @var int - * - * @Rest\Id - * - * @Rest\Attribute(name="id", type="int") */ + #[Rest\Id] + #[Rest\Attribute(name: 'id', type: 'string')] private $id; - /** - * @Rest\Attribute(name="title", type="string") - */ #[Rest\Attribute(name: 'title', type: 'string')] private $title; diff --git a/Tests/Model/JsonLd/Cart.php b/Tests/Model/JsonLd/Cart.php index 13cb2e2..754ce67 100644 --- a/Tests/Model/JsonLd/Cart.php +++ b/Tests/Model/JsonLd/Cart.php @@ -27,7 +27,7 @@ class Cart #[Rest\Attribute(name: 'created_at', type: 'datetime')] private $createdAt; - #[Rest\OneToMany(name: 'cart_items', targetEntity: 'CartItem')] + #[Rest\OneToMany(name: 'cart_items', targetEntity: CartItem::class)] private $cartItemList = []; /** @@ -38,7 +38,7 @@ class Cart #[Rest\Attribute(name: 'clientPhoneNumber', type: 'phone_number')] private $clientPhoneNumber; - #[Rest\ManyToOne(name: 'order', targetEntity: 'Order')] + #[Rest\ManyToOne(name: 'order', targetEntity: Order::class)] private $order; /** diff --git a/Tests/Model/JsonLd/CartItem.php b/Tests/Model/JsonLd/CartItem.php index 28ba659..f4f61a7 100644 --- a/Tests/Model/JsonLd/CartItem.php +++ b/Tests/Model/JsonLd/CartItem.php @@ -43,7 +43,7 @@ class CartItem /** * cart */ - #[Rest\ManyToOne(name: 'cart', targetEntity: 'Cart')] + #[Rest\ManyToOne(name: 'cart', targetEntity: Cart::class)] private $cart; /** diff --git a/Tests/Model/JsonLd/Invalid.php b/Tests/Model/JsonLd/Invalid.php new file mode 100644 index 0000000..1acf1ed --- /dev/null +++ b/Tests/Model/JsonLd/Invalid.php @@ -0,0 +1,20 @@ +client; + } +} diff --git a/Tests/Model/JsonLd/Model.php b/Tests/Model/JsonLd/Model.php index 79bcb2e..7ee93ab 100644 --- a/Tests/Model/JsonLd/Model.php +++ b/Tests/Model/JsonLd/Model.php @@ -13,11 +13,6 @@ */ class Model { - /** - * @Rest\Id - * - * @Rest\Attribute(name="id", type="string") - */ #[Rest\Id] #[Rest\Attribute(name: 'id', type: 'string')] private $id; diff --git a/Tests/Units/Mapping/Driver/AttributeDriverTest.php b/Tests/Units/Mapping/Driver/AttributeDriverTest.php index 8021d15..efb7574 100644 --- a/Tests/Units/Mapping/Driver/AttributeDriverTest.php +++ b/Tests/Units/Mapping/Driver/AttributeDriverTest.php @@ -4,8 +4,17 @@ namespace Mapado\RestClientSdk\Tests\Units\Mapping\Driver; +use Mapado\RestClientSdk\Exception\MappingException; +use Mapado\RestClientSdk\Mapping\Attribute; +use Mapado\RestClientSdk\Mapping\ClassMetadata; use Mapado\RestClientSdk\Mapping\Driver\AttributeDriver; use Mapado\RestClientSdk\Mapping\Relation; +use Mapado\RestClientSdk\Tests\Model\JsonLd\Cart; +use Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem; +use Mapado\RestClientSdk\Tests\Model\JsonLd\Client; +use Mapado\RestClientSdk\Tests\Model\JsonLd\Invalid; +use Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository; +use Mapado\RestClientSdk\Tests\Model\JsonLd\Product; use PHPUnit\Framework\TestCase; /** @@ -20,10 +29,22 @@ public function testClassWithoutEntityAnnotation(): void { $testedInstance = new AttributeDriver($this->getCacheDir(), true); - $mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\Client'); + $mapping = $testedInstance->loadClassname(Client::class); $this->assertEmpty($mapping); } + /** + * testClassWithoutEntityAnnotation + */ + public function testClassWithInvalidProperty(): void + { + $testedInstance = new AttributeDriver($this->getCacheDir(), true); + + $this->expectException(MappingException::class); + + $mapping = $testedInstance->loadClassname(Invalid::class); + } + /** * testAnnotationDriver */ @@ -31,20 +52,20 @@ public function testAnnotationDriver(): void { $testedInstance = new AttributeDriver($this->getCacheDir(), true); - $mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\Product'); + $mapping = $testedInstance->loadClassname(Product::class); $this->assertCount(1, $mapping); $classMetadata = current($mapping); - $this->assertInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata', $classMetadata); + $this->assertInstanceOf(ClassMetadata::class, $classMetadata); $this->assertEquals('product', $classMetadata->getKey()); - $this->assertEquals('Mapado\RestClientSdk\Tests\Model\JsonLd\Product', $classMetadata->getModelName()); - $this->assertEquals('Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository', $classMetadata->getRepositoryName()); + $this->assertEquals(Product::class, $classMetadata->getModelName()); + $this->assertEquals(ModelRepository::class, $classMetadata->getRepositoryName()); $attributeList = $classMetadata->getAttributeList(); $this->assertCount(3, $attributeList); $attribute = current($attributeList); - $this->assertInstanceOf('Mapado\RestClientSdk\Mapping\Attribute', $attribute); + $this->assertInstanceOf(Attribute::class, $attribute); $this->assertEquals('id', $attribute->getSerializedKey()); $attribute = next($attributeList); @@ -56,11 +77,11 @@ public function testAnnotationDriverWithRelations(): void { $testedInstance = new AttributeDriver($this->getCacheDir(), true); - $mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart'); + $mapping = $testedInstance->loadClassname(Cart::class); $this->assertCount(1, $mapping); $classMetadata = current($mapping); - $this->assertInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata', $classMetadata); + $this->assertInstanceOf(ClassMetadata::class, $classMetadata); $this->assertEquals('cart', $classMetadata->getKey()); $attributeList = $classMetadata->getAttributeList(); @@ -70,11 +91,11 @@ public function testAnnotationDriverWithRelations(): void $this->assertCount(2, $relationList); $this->assertEquals(Relation::ONE_TO_MANY, current($relationList)->getType()); - $mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'); + $mapping = $testedInstance->loadClassname(CartItem::class); $this->assertCount(1, $mapping); $classMetadata = current($mapping); - $this->assertInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata', $classMetadata); + $this->assertInstanceOf(ClassMetadata::class, $classMetadata); $this->assertEquals('cart_item', $classMetadata->getKey()); $attributeList = $classMetadata->getAttributeList(); @@ -84,7 +105,7 @@ public function testAnnotationDriverWithRelations(): void $this->assertCount(1, $relationList); $relation = current($relationList); $this->assertEquals(Relation::MANY_TO_ONE, $relation->getType()); - $this->assertEquals('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', $relation->getTargetEntity()); + $this->assertEquals(Cart::class, $relation->getTargetEntity()); } public function testLoadDirectory(): void diff --git a/Tests/Units/Model/SerializerTest.php b/Tests/Units/Model/SerializerTest.php index e55376d..93428da 100644 --- a/Tests/Units/Model/SerializerTest.php +++ b/Tests/Units/Model/SerializerTest.php @@ -732,7 +732,7 @@ public function testSerializingIriManyToOne(): void ], $this->testedInstance->serialize( $article, - 'Mapado\RestClientSdk\Tests\Model\Issue46\Article' + Issue46\Article::class ) ); @@ -747,7 +747,7 @@ public function testSerializingIriManyToOne(): void ], $this->testedInstance->serialize( $section, - 'Mapado\RestClientSdk\Tests\Model\Issue46\Section' + Issue46\Section::class ) ); @@ -766,7 +766,7 @@ public function testSerializingIriManyToOne(): void ], $this->testedInstance->serialize( $section, - 'Mapado\RestClientSdk\Tests\Model\Issue46\Section', + Issue46\Section::class, ['serializeRelations' => ['articleList']] ) ); diff --git a/src/Mapping/Attributes/Relation.php b/src/Mapping/Attributes/Relation.php index bf8e074..12f0a17 100644 --- a/src/Mapping/Attributes/Relation.php +++ b/src/Mapping/Attributes/Relation.php @@ -6,6 +6,9 @@ abstract class Relation { + /** + * @param class-string $targetEntity + */ public function __construct( public readonly string $name, public readonly string $targetEntity, diff --git a/src/Mapping/Driver/AttributeDriver.php b/src/Mapping/Driver/AttributeDriver.php index dcc69f5..4558d03 100644 --- a/src/Mapping/Driver/AttributeDriver.php +++ b/src/Mapping/Driver/AttributeDriver.php @@ -9,6 +9,7 @@ use Mapado\RestClientSdk\Mapping\Attributes; use Mapado\RestClientSdk\Mapping\ClassMetadata; use Mapado\RestClientSdk\Mapping\Relation; +use function PHPStan\dumpType; /** * Class AttributeDriver @@ -137,20 +138,15 @@ private function getClassMetadataForClassname( ); } - if ($relation) { + if ($relation instanceof Attributes\Relation) { $attributeList[] = new Attribute( $relation->name, $property->getName(), ); $targetEntity = $relation->targetEntity; - if (false === mb_strpos($targetEntity, '/')) { - $targetEntity = - mb_substr( - $classname, - 0, - mb_strrpos($classname, '\\') + 1, - ) . $targetEntity; + if (null === $this->getClassMetadataForClassname($targetEntity)) { + throw new MappingException("Invalid targetEntity"); } $relationList[] = new Relation( diff --git a/src/Mapping/Relation.php b/src/Mapping/Relation.php index f574195..81152e3 100644 --- a/src/Mapping/Relation.php +++ b/src/Mapping/Relation.php @@ -15,28 +15,13 @@ class Relation public const ONE_TO_MANY = 'OneToMany'; /** - * @var string + * @param class-string $targetEntity */ - private $serializedKey; - - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $targetEntity; - public function __construct( - string $serializedKey, - string $type, - string $targetEntity, + private string $serializedKey, + private string $type, + private string $targetEntity, ) { - $this->serializedKey = $serializedKey; - $this->type = $type; - $this->targetEntity = $targetEntity; } public function getSerializedKey(): string @@ -73,11 +58,17 @@ public function isManyToOne(): bool return self::MANY_TO_ONE === $this->getType(); } + /** + * @return class-string + */ public function getTargetEntity(): string { return $this->targetEntity; } + /** + * @param class-string $targetEntity + */ public function setTargetEntity(string $targetEntity): self { $this->targetEntity = $targetEntity;