Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jun 17, 2024
1 parent 8141d97 commit 294590b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/AttributeTypecastHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,55 @@

namespace Vjik\CycleTypecast\Tests;

use Cycle\ORM\SchemaInterface;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Vjik\CycleTypecast\AttributeTypecastHandler;
use Vjik\CycleTypecast\Tests\Support\EntityWithAttributes;

final class AttributeTypecastHandlerTest extends TestCase
{
public function testBase(): void
{
$schema = $this->createMock(SchemaInterface::class);
$schema
->method('define')
->with('role_user', SchemaInterface::ENTITY)
->willReturn(EntityWithAttributes::class);

$typecastHandler = new AttributeTypecastHandler($schema, 'role_user');

$uuid = Uuid::fromString('1f2d3897-a226-4eec-bd2c-d0145ef25df9');

$data = $typecastHandler->uncast([
'id' => '1f2d3897-a226-4eec-bd2c-d0145ef25df9',
'names' => ['John', 'Doe'],
]);
$this->assertSame(
[
'id' => $uuid->getBytes(),
'names' => 'John,Doe',
],
$data,
);

$data = $typecastHandler->cast([
'id' => $uuid->getBytes(),
'names' => 'John,Doe',
]);
$this->assertSame(
[
'id' => '1f2d3897-a226-4eec-bd2c-d0145ef25df9',
'names' => ['John', 'Doe'],
],
$data,
);

$rules = $typecastHandler->setRules([
'id' => 'string',
'names' => 'array',
'age' => 'int',
]);
$this->assertSame(['age' => 'int'], $rules);
}
}
19 changes: 19 additions & 0 deletions tests/Support/EntityWithAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Vjik\CycleTypecast\Tests\Support;

use Vjik\CycleTypecast\ArrayToStringType;
use Vjik\CycleTypecast\UuidString\UuidStringToBytesType;

final class EntityWithAttributes
{
#[UuidStringToBytesType]
public string $id;

#[ArrayToStringType(',')]
public array $names;

public int $age;
}

0 comments on commit 294590b

Please sign in to comment.