Skip to content

Commit

Permalink
Improve "NonPublicConstructorException" + More tests (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Oct 26, 2023
1 parent 40ee56e commit e101145
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/Exception/NonPublicConstructorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

namespace Yiisoft\Hydrator\Exception;

use LogicException;
use ReflectionMethod;

final class NonPublicConstructorException extends NonInstantiableException
{
public function __construct(ReflectionMethod $constructor)
{
$type = $this->getConstructorType($constructor);
parent::__construct(
sprintf(
'%s is not instantiable because contain non-public%s constructor.',
'%s is not instantiable because contain non-public (%s) constructor.',
$constructor->getDeclaringClass()->getName(),
$type !== null ? ' (' . $type . ')' : '',
$this->getConstructorType($constructor),
),
);
}

private function getConstructorType(ReflectionMethod $constructor): ?string
private function getConstructorType(ReflectionMethod $constructor): string
{
if ($constructor->isPrivate()) {
return 'private';
Expand All @@ -30,6 +30,8 @@ private function getConstructorType(ReflectionMethod $constructor): ?string
return 'protected';
}

return null;
throw new LogicException(
'Exception "NonPublicConstructorException" can be used only for non-public constructors.'
);
}
}
4 changes: 3 additions & 1 deletion src/Exception/WrongConstructorArgumentsCountException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Yiisoft\Hydrator\Exception;

use ReflectionMethod;

final class WrongConstructorArgumentsCountException extends NonInstantiableException
{
public function __construct(\ReflectionMethod $constructor, int $countArguments)
public function __construct(ReflectionMethod $constructor, int $countArguments)
{
parent::__construct(
sprintf(
Expand Down
3 changes: 3 additions & 0 deletions src/Temp/RouteArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Attribute;
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface;

/**
* @codeCoverageIgnore
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
final class RouteArgument implements ParameterAttributeInterface
{
Expand Down
3 changes: 3 additions & 0 deletions src/Temp/RouteArgumentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use function array_key_exists;

/**
* @codeCoverageIgnore
*/
final class RouteArgumentResolver implements ParameterAttributeResolverInterface
{
public function __construct(
Expand Down
30 changes: 30 additions & 0 deletions tests/Exception/NonPublicConstructorExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Exception;

use LogicException;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use Yiisoft\Hydrator\Exception\NonPublicConstructorException;

final class NonPublicConstructorExceptionTest extends TestCase
{
public function testWithPublicConstructor(): void
{
$reflection = (new ReflectionObject(
new class () {
public function __construct()
{
}
}
))->getConstructor();

$this->expectException(LogicException::class);
$this->expectExceptionMessage(
'Exception "NonPublicConstructorException" can be used only for non-public constructors.'
);
new NonPublicConstructorException($reflection);
}
}
26 changes: 26 additions & 0 deletions tests/TypeCaster/TypeCastContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace TypeCaster;

use PHPUnit\Framework\TestCase;
use ReflectionFunction;
use ReflectionNamedType;
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\TypeCaster\TypeCastContext;

final class TypeCastContextTest extends TestCase
{
public function testGetters(): void
{
$hydrator = new Hydrator();
$reflection = (new ReflectionFunction(static fn(int $a) => null))->getParameters()[0];

$context = new TypeCastContext($hydrator, $reflection);

$this->assertSame($reflection, $context->getReflection());
$this->assertInstanceOf(ReflectionNamedType::class, $context->getReflectionType());
$this->assertSame($hydrator, $context->getHydrator());
}
}

0 comments on commit e101145

Please sign in to comment.