-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a81ea52
commit 49f6e4d
Showing
4 changed files
with
110 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composite\Entity\Columns; | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
class UuidColumn extends AbstractColumn | ||
{ | ||
public function cast(mixed $dbValue): UuidInterface | ||
{ | ||
return Uuid::fromString((string)$dbValue); | ||
} | ||
|
||
/** | ||
* @param UuidInterface $entityValue | ||
* @return string | ||
*/ | ||
public function uncast(mixed $entityValue): string | ||
{ | ||
return $entityValue->toString(); | ||
} | ||
} |
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,83 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composite\Entity\Tests\Columns; | ||
|
||
use Composite\Entity\AbstractEntity; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
final class UuidColumnTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public static function cast_dataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'value' => null, | ||
'expected' => null, | ||
], | ||
[ | ||
'value' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8', | ||
'expected' => Uuid::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'), | ||
], | ||
[ | ||
'value' => '550e8400-e29b-41d4-a716-446655440000', | ||
'expected' => Uuid::fromString('550e8400-e29b-41d4-a716-446655440000'), | ||
], | ||
[ | ||
'value' => 'invalid_uuid', | ||
'expected' => null, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider cast_dataProvider | ||
*/ | ||
public function test_cast(mixed $value, ?UuidInterface $expected): void | ||
{ | ||
$class = new class extends AbstractEntity { | ||
public function __construct( | ||
public ?UuidInterface $column = null, | ||
) {} | ||
}; | ||
$entity = $class::fromArray(['column' => $value]); | ||
$this->assertEquals($expected, $entity->column); | ||
} | ||
|
||
public static function uncast_dataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'value' => null, | ||
'expected' => null, | ||
], | ||
[ | ||
'value' => Uuid::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'), | ||
'expected' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8', | ||
], | ||
[ | ||
'value' => Uuid::fromString('550e8400-e29b-41d4-a716-446655440000'), | ||
'expected' => '550e8400-e29b-41d4-a716-446655440000', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider uncast_dataProvider | ||
*/ | ||
public function test_uncast(?UuidInterface $value, mixed $expected): void | ||
{ | ||
$entity = new class($value) extends AbstractEntity { | ||
public function __construct( | ||
public ?UuidInterface $column, | ||
) {} | ||
}; | ||
$actual = $entity->toArray()['column']; | ||
$this->assertEquals($expected, $actual); | ||
|
||
$newEntity = $entity::fromArray(['column' => $actual]); | ||
$newActual = $newEntity->toArray()['column']; | ||
$this->assertEquals($entity->column, $newEntity->column); | ||
$this->assertEquals($expected, $newActual); | ||
} | ||
} |