Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Aug 2, 2024
1 parent 80b6d41 commit 4eba0b3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 20 deletions.
89 changes: 69 additions & 20 deletions tests/Attribute/Parameter/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException;
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\AttributeHandling\ResolverFactory\ContainerAttributeResolverFactory;
use Yiisoft\Hydrator\DataInterface;
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\Result;
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter;
Expand All @@ -23,6 +24,7 @@
use Yiisoft\Hydrator\Tests\Support\Classes\Chart\Point;
use Yiisoft\Hydrator\Tests\Support\Classes\CounterClass;
use Yiisoft\Hydrator\Tests\Support\Classes\Post;
use Yiisoft\Hydrator\Tests\Support\Classes\PostCategory;
use Yiisoft\Hydrator\Tests\Support\TestHelper;
use Yiisoft\Test\Support\Container\SimpleContainer;

Expand All @@ -49,50 +51,41 @@ public function testUnexpectedAttributeException(): void
public function testNotResolvedValue(): void
{
$hydrator = new Hydrator();
$object = new class () {
#[Collection(Chart::class)]
public array $charts = [];
};
$object = new PostCategory();

$hydrator->hydrate($object, ['chart' => []]);
$this->assertEmpty($object->charts);
$hydrator->hydrate($object, ['post' => []]);
$this->assertEmpty($object->getPosts());
}

public function testInvalidValue(): void
{
$hydrator = new Hydrator();
$object = new class () {
#[Collection(Chart::class)]
public array $charts = [];
};
$object = new PostCategory();

$hydrator->hydrate($object, ['charts' => new stdClass()]);
$this->assertEmpty($object->charts);
$hydrator->hydrate($object, ['posts' => new stdClass()]);
$this->assertEmpty($object->getPosts());
}

public function testInvalidValueItem(): void
{
$hydrator = new Hydrator();
$object = new class () {
#[Collection(Chart::class)]
public array $charts = [];
};
$object = new PostCategory();

$hydrator->hydrate(
$object,
[
'charts' => [
'posts' => [
new stdClass(),
],
],
);
$this->assertEquals($object->charts, [new stdClass()]);
$this->assertEquals($object->getPosts(), [new stdClass()]);
}

public static function dataBase(): array
{
return [
[
'basic' => [
new Collection(Post::class),
[
['name' => 'Post 1'],
Expand All @@ -103,11 +96,67 @@ public static function dataBase(): array
new Post(name: 'Post 2', description: 'Description for post 2'),
],
],
'nested, one to one and one to many relations' => [
new Collection(Chart::class),
[
[
'points' => [
['coordinates' => ['x' => -11, 'y' => 11], 'rgb' => [-1, 256, 0]],
['coordinates' => ['x' => -12, 'y' => 12], 'rgb' => [0, -2, 257]],
],
],
[
'points' => [
['coordinates' => ['x' => -1, 'y' => 1], 'rgb' => [0, 0, 0]],
['coordinates' => ['x' => -2, 'y' => 2], 'rgb' => [255, 255, 255]],
],
],
[
'points' => [
['coordinates' => ['x' => -13, 'y' => 13], 'rgb' => [-3, 258, 0]],
['coordinates' => ['x' => -14, 'y' => 14], 'rgb' => [0, -4, 259]],
],
],
],
[
new Chart([
new Point(new Coordinates(-11, 11), [-1, 256, 0]),
new Point(new Coordinates(-12, 12), [0, -2, 257]),
]),
new Chart([
new Point(new Coordinates(-1, 1), [0, 0, 0]),
new Point(new Coordinates(-2, 2), [255, 255, 255]),
]),
new Chart([
new Point(new Coordinates(-13, 13), [-3, 258, 0]),
new Point(new Coordinates(-14, 14), [0, -4, 259]),
]),
],
],
'value item provided by class' => [
new Collection(Post::class),
[
['name' => 'Post 1'],
new class () implements DataInterface
{
public function getValue(string $name): Result
{
$value = $name === 'name' ? 'Post 2' : 'Description for post 2';

return Result::success($value);
}
}
],
[
new Post(name: 'Post 1'),
new Post(name: 'Post 2', description: 'Description for post 2'),
],
],
];
}

#[DataProvider('dataBase')]
public function testBase(Collection $attribute, array $value, array $expectedValue): void
public function testBase(Collection $attribute, array $value, mixed $expectedValue): void
{
$resolver = new CollectionResolver();
$context = new ParameterAttributeResolveContext(
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/Classes/PostCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\Support\Classes;

use Yiisoft\Hydrator\Attribute\Parameter\Collection;

final class PostCategory
{
public function __construct(
#[Collection(Post::class)]
private array $posts = [],
) {
}

public function getPosts(): array
{
return $this->posts;
}
}

0 comments on commit 4eba0b3

Please sign in to comment.