From 1426613e62066a772e651376aa0d904c0b90c236 Mon Sep 17 00:00:00 2001 From: butschster Date: Sat, 8 Jun 2024 11:11:15 +0400 Subject: [PATCH] Fixes an issue with decoding `null` values using the Json Value object. Additional changes: - Moved the JsonValue object to the appropriate namespace. - Added unit tests for the Json Value object. - Added unit tests for the Uuid Value object. --- app/database/Factory/EventFactory.php | 2 +- app/database/Factory/WebhookFactory.php | 2 +- app/modules/Events/Domain/Event.php | 2 +- .../Interfaces/Commands/StoreEventHandler.php | 2 +- .../Webhooks/Application/WebhookFactory.php | 2 +- app/modules/Webhooks/Domain/Webhook.php | 2 +- .../Domain/WebhookFactoryInterface.php | 2 +- .../Integartion/CycleOrm/WebhookRegistry.php | 2 +- .../Domain/Entity/ExtendedTypecast.php | 1 + .../Domain/{Entity => ValueObjects}/Json.php | 6 ++- .../Domain/ValueObjects/JsonTest.php | 43 +++++++++++++++++++ .../Domain/ValueObjects/UuidTest.php | 35 +++++++++++++++ 12 files changed, 91 insertions(+), 10 deletions(-) rename app/src/Application/Domain/{Entity => ValueObjects}/Json.php (85%) create mode 100644 tests/Unit/Application/Domain/ValueObjects/JsonTest.php create mode 100644 tests/Unit/Application/Domain/ValueObjects/UuidTest.php diff --git a/app/database/Factory/EventFactory.php b/app/database/Factory/EventFactory.php index 35cdcfe..246cffb 100644 --- a/app/database/Factory/EventFactory.php +++ b/app/database/Factory/EventFactory.php @@ -4,7 +4,7 @@ namespace Database\Factory; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Database\Factory\Partials\InspectorType; use Database\Factory\Partials\MonologType; diff --git a/app/database/Factory/WebhookFactory.php b/app/database/Factory/WebhookFactory.php index a1c68d8..1fc0f96 100644 --- a/app/database/Factory/WebhookFactory.php +++ b/app/database/Factory/WebhookFactory.php @@ -4,7 +4,7 @@ namespace Database\Factory; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Modules\Webhooks\Domain\ValueObject\Url; use Modules\Webhooks\Domain\Webhook; diff --git a/app/modules/Events/Domain/Event.php b/app/modules/Events/Domain/Event.php index b540aec..a07e4f3 100644 --- a/app/modules/Events/Domain/Event.php +++ b/app/modules/Events/Domain/Event.php @@ -4,7 +4,7 @@ namespace Modules\Events\Domain; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Cycle\Annotated\Annotation\Column; use Cycle\Annotated\Annotation\Entity; diff --git a/app/modules/Events/Interfaces/Commands/StoreEventHandler.php b/app/modules/Events/Interfaces/Commands/StoreEventHandler.php index 9eed54b..012ac9b 100644 --- a/app/modules/Events/Interfaces/Commands/StoreEventHandler.php +++ b/app/modules/Events/Interfaces/Commands/StoreEventHandler.php @@ -6,7 +6,7 @@ use App\Application\Commands\FindProjectByKey; use App\Application\Commands\HandleReceivedEvent; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use Modules\Events\Application\EventMetrics; use Modules\Events\Domain\Event; use Modules\Events\Domain\EventRepositoryInterface; diff --git a/app/modules/Webhooks/Application/WebhookFactory.php b/app/modules/Webhooks/Application/WebhookFactory.php index 6fea9ae..3f92a56 100644 --- a/app/modules/Webhooks/Application/WebhookFactory.php +++ b/app/modules/Webhooks/Application/WebhookFactory.php @@ -4,7 +4,7 @@ namespace Modules\Webhooks\Application; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Modules\Webhooks\Domain\ValueObject\Url; use Modules\Webhooks\Domain\Webhook; diff --git a/app/modules/Webhooks/Domain/Webhook.php b/app/modules/Webhooks/Domain/Webhook.php index bdd1701..1590569 100644 --- a/app/modules/Webhooks/Domain/Webhook.php +++ b/app/modules/Webhooks/Domain/Webhook.php @@ -4,7 +4,7 @@ namespace Modules\Webhooks\Domain; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Cycle\Annotated\Annotation\Column; use Cycle\Annotated\Annotation\Entity; diff --git a/app/modules/Webhooks/Domain/WebhookFactoryInterface.php b/app/modules/Webhooks/Domain/WebhookFactoryInterface.php index 3b69d54..fa0e649 100644 --- a/app/modules/Webhooks/Domain/WebhookFactoryInterface.php +++ b/app/modules/Webhooks/Domain/WebhookFactoryInterface.php @@ -4,7 +4,7 @@ namespace Modules\Webhooks\Domain; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use Modules\Webhooks\Domain\ValueObject\Url; interface WebhookFactoryInterface diff --git a/app/modules/Webhooks/Integartion/CycleOrm/WebhookRegistry.php b/app/modules/Webhooks/Integartion/CycleOrm/WebhookRegistry.php index cb0d4ba..9ed36e1 100644 --- a/app/modules/Webhooks/Integartion/CycleOrm/WebhookRegistry.php +++ b/app/modules/Webhooks/Integartion/CycleOrm/WebhookRegistry.php @@ -4,7 +4,7 @@ namespace Modules\Webhooks\Integartion\CycleOrm; -use App\Application\Domain\Entity\Json; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Modules\Webhooks\Application\Locator\Webhook; use Modules\Webhooks\Application\Locator\WebhookRegistryInterface; diff --git a/app/src/Application/Domain/Entity/ExtendedTypecast.php b/app/src/Application/Domain/Entity/ExtendedTypecast.php index 1b86c2e..7784673 100644 --- a/app/src/Application/Domain/Entity/ExtendedTypecast.php +++ b/app/src/Application/Domain/Entity/ExtendedTypecast.php @@ -4,6 +4,7 @@ namespace App\Application\Domain\Entity; +use App\Application\Domain\ValueObjects\Json; use App\Application\Domain\ValueObjects\Uuid; use Cycle\ORM\Parser\CastableInterface; use Cycle\ORM\Parser\UncastableInterface; diff --git a/app/src/Application/Domain/Entity/Json.php b/app/src/Application/Domain/ValueObjects/Json.php similarity index 85% rename from app/src/Application/Domain/Entity/Json.php rename to app/src/Application/Domain/ValueObjects/Json.php index a06d69f..56fd598 100644 --- a/app/src/Application/Domain/Entity/Json.php +++ b/app/src/Application/Domain/ValueObjects/Json.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Application\Domain\Entity; +namespace App\Application\Domain\ValueObjects; final readonly class Json implements \JsonSerializable, \Stringable { @@ -20,7 +20,9 @@ final public static function typecast(mixed $value): self } try { - return new self(\json_decode($value, true)); + return new self( + (array) \json_decode($value, true), + ); } catch (\JsonException $e) { throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } diff --git a/tests/Unit/Application/Domain/ValueObjects/JsonTest.php b/tests/Unit/Application/Domain/ValueObjects/JsonTest.php new file mode 100644 index 0000000..a31cd94 --- /dev/null +++ b/tests/Unit/Application/Domain/ValueObjects/JsonTest.php @@ -0,0 +1,43 @@ + [ + 'null', + [], + ]; + + yield 'empty-string' => [ + '', + [], + ]; + + yield 'empty' => [ + '{}', + [], + ]; + + yield 'simple' => [ + '{"key": "value"}', + ['key' => 'value'], + ]; + } + + #[DataProvider('jsonDataProvider')] + public function testDecode(string $json, mixed $expected): void + { + $object = Json::typecast($json); + + $this->assertEquals($expected, $object->jsonSerialize()); + } +} diff --git a/tests/Unit/Application/Domain/ValueObjects/UuidTest.php b/tests/Unit/Application/Domain/ValueObjects/UuidTest.php new file mode 100644 index 0000000..0c51643 --- /dev/null +++ b/tests/Unit/Application/Domain/ValueObjects/UuidTest.php @@ -0,0 +1,35 @@ +assertNotEquals((string) $uuid1, (string) $uuid2); + } + + public function testEquals(): void + { + $uuid1 = Uuid::generate(); + $uuid2 = Uuid::generate(); + + $this->assertTrue($uuid1->equals($uuid1)); + $this->assertFalse($uuid1->equals($uuid2)); + } + + public function testFromString(): void + { + $uuid = Uuid::fromString($string = 'f47ac10b-58cc-4372-a567-0e02b2c3d479'); + + $this->assertEquals($string, (string) $uuid); + } +}