-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from patchlevel/infer-normalizer
Infer Normalizer
- Loading branch information
Showing
11 changed files
with
277 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Fixture; | ||
|
||
final class InferNormalizerBrokenDto | ||
{ | ||
/** @param array<string> $array */ | ||
public function __construct( | ||
public ProfileCreated $profileCreated, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Fixture; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
|
||
final class InferNormalizerDto | ||
{ | ||
/** @param array<string> $array */ | ||
public function __construct( | ||
public Status $status, | ||
public DateTimeImmutable $dateTimeImmutable, | ||
public DateTime $dateTime, | ||
public DateTimeZone $dateTimeZone, | ||
public array $array, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Fixture; | ||
|
||
final class NormalizerInBaseClassDefinedDto | ||
{ | ||
/** @param array<string> $array */ | ||
public function __construct( | ||
public StatusWithNormalizer $status, | ||
public ProfileCreatedWithNormalizer $profileCreated, | ||
public array $array, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Fixture; | ||
|
||
use Patchlevel\Hydrator\Normalizer\ObjectNormalizer; | ||
|
||
#[ObjectNormalizer] | ||
final class ProfileCreatedWithNormalizer | ||
{ | ||
public function __construct( | ||
#[ProfileIdNormalizer] | ||
public ProfileId $profileId, | ||
#[EmailNormalizer] | ||
public Email $email, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\Hydrator\Tests\Unit\Fixture; | ||
|
||
use Patchlevel\Hydrator\Normalizer\EnumNormalizer; | ||
|
||
#[EnumNormalizer] | ||
enum StatusWithNormalizer: string | ||
{ | ||
case Draft = 'draft'; | ||
case Pending = 'pending'; | ||
case Closed = 'closed'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
namespace Patchlevel\Hydrator\Tests\Unit; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Patchlevel\Hydrator\CircularReference; | ||
use Patchlevel\Hydrator\Cryptography\PayloadCryptographer; | ||
use Patchlevel\Hydrator\DenormalizationFailure; | ||
|
@@ -15,10 +18,16 @@ | |
use Patchlevel\Hydrator\Tests\Unit\Fixture\Circle3Dto; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\DefaultDto; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerBrokenDto; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerDto; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\NormalizerInBaseClassDefinedDto; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentDto; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreatedWithNormalizer; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreatedWrapper; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\StatusWithNormalizer; | ||
use Patchlevel\Hydrator\Tests\Unit\Fixture\WrongNormalizer; | ||
use Patchlevel\Hydrator\TypeMismatch; | ||
use PHPUnit\Framework\TestCase; | ||
|
@@ -231,4 +240,62 @@ public function testEncrypt(): void | |
|
||
self::assertSame($encryptedPayload, $return); | ||
} | ||
|
||
public function testHydrateWithNormalizerInBaseClass(): void | ||
{ | ||
$expected = new NormalizerInBaseClassDefinedDto( | ||
StatusWithNormalizer::Draft, | ||
new ProfileCreatedWithNormalizer( | ||
ProfileId::fromString('1'), | ||
Email::fromString('[email protected]'), | ||
), | ||
['foo'], | ||
); | ||
|
||
$event = $this->hydrator->hydrate( | ||
NormalizerInBaseClassDefinedDto::class, | ||
[ | ||
'status' => 'draft', | ||
'profileCreated' => ['profileId' => '1', 'email' => '[email protected]'], | ||
'array' => ['foo'], | ||
], | ||
); | ||
|
||
self::assertEquals($expected, $event); | ||
} | ||
|
||
public function testHydrateWithInferNormalizer(): void | ||
{ | ||
$expected = new InferNormalizerDto( | ||
Status::Draft, | ||
new DateTimeImmutable('2015-02-13 22:34:32+01:00'), | ||
new DateTime('2015-02-13 22:34:32+01:00'), | ||
new DateTimeZone('EDT'), | ||
['foo'], | ||
); | ||
|
||
$event = $this->hydrator->hydrate( | ||
InferNormalizerDto::class, | ||
[ | ||
'status' => 'draft', | ||
'dateTimeImmutable' => '2015-02-13T22:34:32+01:00', | ||
'dateTime' => '2015-02-13T22:34:32+01:00', | ||
'dateTimeZone' => 'EDT', | ||
'array' => ['foo'], | ||
], | ||
); | ||
|
||
self::assertEquals($expected, $event); | ||
} | ||
|
||
public function testHydrateWithInferNormalizerFailed(): void | ||
{ | ||
$this->expectException(TypeMismatch::class); | ||
$event = $this->hydrator->hydrate( | ||
InferNormalizerBrokenDto::class, | ||
[ | ||
'profileCreated' => ['profileId' => '1', 'email' => '[email protected]'], | ||
], | ||
); | ||
} | ||
} |