generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tests/AttributeHandling/ResolverFactory/ReflectionAttributeResolverFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |