Skip to content

Commit

Permalink
Merge pull request #197 from buggregator/hotfix/194
Browse files Browse the repository at this point in the history
Fixes an issue with decoding `null` values using the Json Value object.
  • Loading branch information
butschster committed Jun 9, 2024
2 parents 35204db + 1426613 commit ccc422c
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/database/Factory/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/database/Factory/WebhookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Webhooks/Application/WebhookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Webhooks/Domain/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Webhooks/Domain/WebhookFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/Application/Domain/Entity/ExtendedTypecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/Application/Domain/ValueObjects/JsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Application\Domain\ValueObjects;

use App\Application\Domain\ValueObjects\Json;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class JsonTest extends TestCase
{
public static function jsonDataProvider(): iterable
{
yield 'null' => [
'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());
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Application/Domain/ValueObjects/UuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Application\Domain\ValueObjects;

use App\Application\Domain\ValueObjects\Uuid;
use PHPUnit\Framework\TestCase;

final class UuidTest extends TestCase
{
public function testGenerate(): void
{
$uuid1 = Uuid::generate();
$uuid2 = Uuid::generate();

$this->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);
}
}

0 comments on commit ccc422c

Please sign in to comment.