From 8b342746971d1c899328ae4740a4e2392044c209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 17 May 2024 10:43:46 +0200 Subject: [PATCH] Add tests --- src/Configuration.php | 25 ++++++++++++++++--------- tests/AutoMapperTest.php | 35 +++++++++++++++++++++++++++++++++++ tools/phpstan/phpstan.neon | 4 ++-- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 0fa0d676..e13d8176 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -9,6 +9,14 @@ final readonly class Configuration { + /** + * @var list>> + */ + public array $arrayAccessClasses; + + /** + * @param list>> $arrayAccessClasses classes with unknown properties, implemeting ArrayAccess + */ public function __construct( /** * Class prefix used to prefix the class name of the generated mappers. @@ -39,17 +47,16 @@ public function __construct( * Does the mapper should throw an exception if the target is read-only. */ public bool $allowReadOnlyTargetToPopulate = false, - /** - * Classes with unknown properties, implemeting ArrayAccess. - * - * @var list>> $arrayAccessClasses - */ - public array $arrayAccessClasses = [], + array $arrayAccessClasses = [], ) { - $this->arrayAccessClasses[] = \ArrayObject::class; + $arrayAccessClasses[] = \ArrayObject::class; + + // Classes provided by the mongodb extension and mongodb/mongodb package if (class_exists(Document::class, false)) { - $this->arrayAccessClasses[] = Document::class; - $this->arrayAccessClasses[] = BSONDocument::class; + $arrayAccessClasses[] = Document::class; + $arrayAccessClasses[] = BSONDocument::class; } + + $this->arrayAccessClasses = $arrayAccessClasses; } } diff --git a/tests/AutoMapperTest.php b/tests/AutoMapperTest.php index 553f5cc4..2ece60fd 100644 --- a/tests/AutoMapperTest.php +++ b/tests/AutoMapperTest.php @@ -210,6 +210,41 @@ public function testAutoMapperStdObjectToStdObject(): void self::assertEquals($user, $userStd); } + public function testAutoMapperFromArrayObject(): void + { + $this->buildAutoMapper(mapPrivatePropertiesAndMethod: true); + + $user = new \ArrayObject(['id' => 1]); + + /** @var Fixtures\UserDTO $userDto */ + $userDto = $this->autoMapper->map($user, Fixtures\UserDTO::class); + + self::assertInstanceOf(Fixtures\UserDTO::class, $userDto); + self::assertEquals(1, $userDto->id); + } + + public function testAutoMapperToArrayObject(): void + { + $userDto = new Fixtures\UserDTO(); + $userDto->id = 1; + + $user = $this->autoMapper->map($userDto, \ArrayObject::class); + + self::assertInstanceOf(\ArrayObject::class, $user); + self::assertEquals(1, $user['id']); + } + + public function testAutoMapperArrayObjectToArrayObject(): void + { + $user = new \ArrayObject(['id' => 1, 'nested' => new \ArrayObject(['id' => 2])]); + $userStd = $this->autoMapper->map($user, \ArrayObject::class); + + self::assertInstanceOf(\ArrayObject::class, $userStd); + self::assertNotSame($user, $userStd); + self::assertNotSame($user['nested'], $userStd['nested']); + self::assertEquals($user, $userStd); + } + public function testNotReadable(): void { $this->buildAutoMapper(classPrefix: 'CustomDateTime_'); diff --git a/tools/phpstan/phpstan.neon b/tools/phpstan/phpstan.neon index e68139da..0e1a880a 100644 --- a/tools/phpstan/phpstan.neon +++ b/tools/phpstan/phpstan.neon @@ -2,11 +2,11 @@ parameters: level: max paths: - ../../src/ - - tmpDir: cache scanFiles: - stubs/mongodb.php + tmpDir: cache + ignoreErrors: - "#Instantiated class fromIteratorToArray not found#" - "#Instantiated class toArray not found#"