Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage 100% #61

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
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 All @@ -50,7 +50,7 @@
);
}

if ($constructor->getNumberOfRequiredParameters() > 0) {

Check warning on line 53 in src/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactory.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ if (!$constructor->isPublic()) { throw new AttributeResolverNonInstantiableException(sprintf('Class "%s" is not instantiable because contain non-public constructor.', $constructor->getDeclaringClass()->getName())); } - if ($constructor->getNumberOfRequiredParameters() > 0) { + if ($constructor->getNumberOfRequiredParameters() >= 0) { throw new AttributeResolverNonInstantiableException(sprintf('Class "%s" cannot be instantiated because it has %d required parameters in constructor.', $constructor->getDeclaringClass()->getName(), $constructor->getNumberOfRequiredParameters())); } }
throw new AttributeResolverNonInstantiableException(
sprintf(
'Class "%s" cannot be instantiated because it has %d required parameters in constructor.',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\ContainerAttributeResolverFactory;
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter;
use Yiisoft\Hydrator\Tests\Support\Attribute\CounterResolver;
use Yiisoft\Hydrator\Tests\Support\Attribute\CustomValue;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class ContainerAttributeResolverFactoryTest extends TestCase
{
public function testBase(): void
{
$attribute = new Counter('test');
$resolver = new CounterResolver();
$container = new SimpleContainer([CounterResolver::class => $resolver]);
$factory = new ContainerAttributeResolverFactory($container);

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

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

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

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

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

public function testResolverNotExists(): void
{
$attribute = new Counter('test');
$container = new SimpleContainer();
$factory = new ContainerAttributeResolverFactory($container);

$this->expectException(AttributeResolverNonInstantiableException::class);
$this->expectExceptionMessage('Class "' . CounterResolver::class . '" does not exist.');
$factory->create($attribute);
}
}
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.');
}
}
Loading