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.
Fix error on create object by incompatible parameters in `HydratorTyp…
…eCaster` + More tests (#59)
- Loading branch information
Showing
4 changed files
with
159 additions
and
7 deletions.
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
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\Support; | ||
|
||
final class PrivateConstructorObject | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/TestEnvironments/Php82/TypeCaster/HydratorTypeCasterTest.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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeCaster; | ||
|
||
use Closure; | ||
use Countable; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionFunction; | ||
use Stringable; | ||
use Yiisoft\Hydrator\Hydrator; | ||
use Yiisoft\Hydrator\Result; | ||
use Yiisoft\Hydrator\Tests\Support\StringableObject; | ||
use Yiisoft\Hydrator\TypeCaster\HydratorTypeCaster; | ||
use Yiisoft\Hydrator\TypeCaster\TypeCastContext; | ||
|
||
final class HydratorTypeCasterTest extends TestCase | ||
{ | ||
public function dataBase(): array | ||
{ | ||
return [ | ||
'array to intersection type' => [ | ||
Result::fail(), | ||
['string' => 'hello'], | ||
$this->createContext(static fn(Stringable&Countable $object) => null), | ||
], | ||
'array to union type with intersection type' => [ | ||
Result::success(new StringableObject('hello')), | ||
['string' => 'hello'], | ||
$this->createContext(static fn(StringableObject|(Stringable&Countable) $object) => null), | ||
], | ||
'incompatible array to union type with intersection type' => [ | ||
Result::fail(), | ||
['var' => 'hello'], | ||
$this->createContext(static fn(StringableObject|(Stringable&Countable) $object) => null), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataBase | ||
*/ | ||
public function testBase(Result $expected, mixed $value, TypeCastContext $context): void | ||
{ | ||
$typeCaster = new HydratorTypeCaster(); | ||
|
||
$result = $typeCaster->cast($value, $context); | ||
|
||
$this->assertSame($expected->isResolved(), $result->isResolved()); | ||
$this->assertEquals($expected->getValue(), $result->getValue()); | ||
} | ||
|
||
private function createContext(Closure $fn): TypeCastContext | ||
{ | ||
return new TypeCastContext( | ||
new Hydrator(), | ||
(new ReflectionFunction($fn))->getParameters()[0], | ||
); | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\TypeCaster; | ||
|
||
use Closure; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionFunction; | ||
use Yiisoft\Hydrator\Hydrator; | ||
use Yiisoft\Hydrator\Result; | ||
use Yiisoft\Hydrator\Tests\Support\PrivateConstructorObject; | ||
use Yiisoft\Hydrator\Tests\Support\StringableObject; | ||
use Yiisoft\Hydrator\TypeCaster\HydratorTypeCaster; | ||
use Yiisoft\Hydrator\TypeCaster\TypeCastContext; | ||
|
||
final class HydratorTypeCasterTest extends TestCase | ||
{ | ||
public function dataBase(): array | ||
{ | ||
return [ | ||
'not array to not array' => [ | ||
Result::fail(), | ||
'not array', | ||
$this->createContext(static fn(int $a) => null), | ||
], | ||
'array to not array' => [ | ||
Result::fail(), | ||
[5], | ||
$this->createContext(static fn(int $a) => null), | ||
], | ||
'array to without type' => [ | ||
Result::fail(), | ||
[5], | ||
$this->createContext(static fn($a) => null), | ||
], | ||
'array to object' => [ | ||
Result::success(new StringableObject('hello')), | ||
['string' => 'hello'], | ||
$this->createContext(static fn(StringableObject $object) => null), | ||
], | ||
'array to union type object|int' => [ | ||
Result::success(new StringableObject('hello')), | ||
['string' => 'hello'], | ||
$this->createContext(static fn(StringableObject|int $object) => null), | ||
], | ||
'array to union type int|object' => [ | ||
Result::success(new StringableObject('hello')), | ||
['string' => 'hello'], | ||
$this->createContext(static fn(int|StringableObject $object) => null), | ||
], | ||
'array to non-instantiable object' => [ | ||
Result::fail(), | ||
['string' => 'hello'], | ||
$this->createContext(static fn(PrivateConstructorObject $object) => null), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataBase | ||
*/ | ||
public function testBase(Result $expected, mixed $value, TypeCastContext $context): void | ||
{ | ||
$typeCaster = new HydratorTypeCaster(); | ||
|
||
$result = $typeCaster->cast($value, $context); | ||
|
||
$this->assertSame($expected->isResolved(), $result->isResolved()); | ||
$this->assertEquals($expected->getValue(), $result->getValue()); | ||
} | ||
|
||
private function createContext(Closure $fn): TypeCastContext | ||
{ | ||
return new TypeCastContext( | ||
new Hydrator(), | ||
(new ReflectionFunction($fn))->getParameters()[0], | ||
); | ||
} | ||
} |