Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Oct 26, 2023
1 parent bf468b3 commit 4f9b678
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function create(DataAttributeInterface|ParameterAttributeInterface $attri
if ($reflectionClass->isAbstract()) {
throw new AttributeResolverNonInstantiableException(
sprintf(
'%s is not instantiable because it is abstract.',
'"%s" is not instantiable because it is abstract.',
$reflectionClass->getName(),
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\AttributeHandling\ResolverFactory;

use PHPUnit\Framework\TestCase;
use Yiisoft\Hydrator\AttributeHandling\Exception\AttributeResolverNonInstantiableException;
use Yiisoft\Hydrator\AttributeHandling\ResolverFactory\ReflectionAttributeResolverFactory;
use Yiisoft\Hydrator\Tests\Support\Attribute\AbstractResolver;
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter;
use Yiisoft\Hydrator\Tests\Support\Attribute\CounterResolver;
use Yiisoft\Hydrator\Tests\Support\Attribute\CustomResolverAttr;
use Yiisoft\Hydrator\Tests\Support\Attribute\CustomValue;
use Yiisoft\Hydrator\Tests\Support\Attribute\ParameterizedResolver;
use Yiisoft\Hydrator\Tests\Support\Attribute\PrivateConstructorResolver;

final class ReflectionAttributeResolverFactoryTest extends TestCase
{
public function testBase(): void
{
$attribute = new Counter('test');
$factory = new ReflectionAttributeResolverFactory();

$result = $factory->create($attribute);

$this->assertInstanceOf(CounterResolver::class, $result);
}

public function testResolverIsAttributeItself(): void
{
$attribute = new CustomValue('test');
$factory = new ReflectionAttributeResolverFactory();

$result = $factory->create($attribute);

$this->assertSame($attribute, $result);
}

public function testNonExistClass(): void
{
$attribute = new CustomResolverAttr('NonExistClass');
$factory = new ReflectionAttributeResolverFactory();

$this->expectException(AttributeResolverNonInstantiableException::class);
$this->expectExceptionMessage('Class "NonExistClass" does not exist.');
$factory->create($attribute);
}

public function testAbstractClass(): void
{
$attribute = new CustomResolverAttr(AbstractResolver::class);
$factory = new ReflectionAttributeResolverFactory();

$this->expectException(AttributeResolverNonInstantiableException::class);
$this->expectExceptionMessage('"' . AbstractResolver::class . '" is not instantiable because it is abstract.');
$factory->create($attribute);
}

public function testNonPublicConstructor(): void
{
$attribute = new CustomResolverAttr(PrivateConstructorResolver::class);
$factory = new ReflectionAttributeResolverFactory();

$this->expectException(AttributeResolverNonInstantiableException::class);
$this->expectExceptionMessage(
'Class "' . PrivateConstructorResolver::class . '" is not instantiable because contain non-public constructor.'
);
$factory->create($attribute);
}

public function testConstructorWithParameters(): void
{
$attribute = new CustomResolverAttr(ParameterizedResolver::class);
$factory = new ReflectionAttributeResolverFactory();

$this->expectException(AttributeResolverNonInstantiableException::class);
$this->expectExceptionMessage(
'Class "' . ParameterizedResolver::class . '" cannot be instantiated because it has 1 required parameters in constructor.'
);
$factory->create($attribute);
}
}
11 changes: 11 additions & 0 deletions tests/Support/Attribute/AbstractResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\Support\Attribute;

use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;

abstract class AbstractResolver implements ParameterAttributeInterface
{
}
21 changes: 21 additions & 0 deletions tests/Support/Attribute/CustomResolverAttr.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\Attribute;

use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface;

final class CustomResolverAttr implements ParameterAttributeInterface
{
public function __construct(
private string|ParameterAttributeResolverInterface $resolver
) {
}

public function getResolver(): string|ParameterAttributeResolverInterface
{
return $this->resolver;
}
}
25 changes: 25 additions & 0 deletions tests/Support/Attribute/ParameterizedResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\Support\Attribute;

use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\Result;

final class ParameterizedResolver implements ParameterAttributeResolverInterface
{
public function __construct(
private string $result,
) {
}

public function getParameterValue(
ParameterAttributeInterface $attribute,
ParameterAttributeResolveContext $context
): Result {
return Result::success($this->result);
}
}
25 changes: 25 additions & 0 deletions tests/Support/Attribute/PrivateConstructorResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\Support\Attribute;

use LogicException;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext;
use Yiisoft\Hydrator\Result;

final class PrivateConstructorResolver implements ParameterAttributeResolverInterface
{
private function __construct()
{
}

public function getParameterValue(
ParameterAttributeInterface $attribute,
ParameterAttributeResolveContext $context
): Result {
throw new LogicException('Not implemented.');
}
}

0 comments on commit 4f9b678

Please sign in to comment.