From c1c28a5d9f7e87d84438a1281c234b76acc08e7c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 1 Apr 2024 13:32:10 +0300 Subject: [PATCH] Raise the minimum version of PHP to 8.1 (#76) --- .github/workflows/build.yml | 2 +- .../workflows/composer-require-checker.yml | 2 +- .github/workflows/static.yml | 2 +- .gitignore | 2 +- CHANGELOG.md | 4 +- README.md | 2 +- composer.json | 8 +- infection.json.dist | 6 -- phpunit.xml.dist | 37 ++++------ rector.php | 6 +- src/Hydrator.php | 5 -- src/Internal/ReflectionFilter.php | 3 +- tests/Attribute/Parameter/ToStringTest.php | 11 +-- tests/HydratorTest.php | 59 +++++++++++---- .../Classes}/CarInterface.php | 2 +- .../Classes}/ReadonlyClass.php | 2 +- .../Classes}/StringableCar.php | 2 +- tests/Support/Classes/TypeClass.php | 9 +++ tests/TestEnvironments/Php81/HydratorTest.php | 74 ------------------- .../Php81/Support/TypeObject.php | 18 ----- .../TypeCaster/PhpNativeTypeCasterTest.php | 50 ------------- tests/TestEnvironments/Php82/HydratorTest.php | 7 +- .../TypeCaster/HydratorTypeCasterTest.php | 7 +- tests/TypeCaster/HydratorTypeCasterTest.php | 7 +- tests/TypeCaster/NullTypeCasterTest.php | 7 +- tests/TypeCaster/PhpNativeTypeCasterTest.php | 23 +++++- 26 files changed, 123 insertions(+), 234 deletions(-) rename tests/{TestEnvironments/Php81/Support => Support/Classes}/CarInterface.php (60%) rename tests/{TestEnvironments/Php81/Support => Support/Classes}/ReadonlyClass.php (79%) rename tests/{TestEnvironments/Php81/Support => Support/Classes}/StringableCar.php (84%) delete mode 100644 tests/TestEnvironments/Php81/HydratorTest.php delete mode 100644 tests/TestEnvironments/Php81/Support/TypeObject.php delete mode 100644 tests/TestEnvironments/Php81/TypeCaster/PhpNativeTypeCasterTest.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 11d50bb..9e54c37 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,4 +30,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['8.0', '8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3'] diff --git a/.github/workflows/composer-require-checker.yml b/.github/workflows/composer-require-checker.yml index 5473ec9..a857bce 100644 --- a/.github/workflows/composer-require-checker.yml +++ b/.github/workflows/composer-require-checker.yml @@ -31,4 +31,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.0', '8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3'] diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 8694d2d..e33eca8 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -29,4 +29,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.0', '8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3'] diff --git a/.gitignore b/.gitignore index ffca5ff..5ef7afd 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,4 @@ phpunit.phar # local phpunit config /phpunit.xml # phpunit cache -.phpunit.result.cache +/.phpunit.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index 05893f1..f19f9ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Yii Hydrator Change Log -## 1.1.1 under development +## 1.2.0 under development -- no changes in this release. +- Enh #76: Raise the minimum version of PHP to 8.1 (@vjik) ## 1.1.0 February 09, 2024 diff --git a/README.md b/README.md index 61b192c..5a04470 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Features are: ## Requirements -- PHP 8.0 or higher. +- PHP 8.1 or higher. ## Installation diff --git a/composer.json b/composer.json index 27bd12a..6bcefa9 100644 --- a/composer.json +++ b/composer.json @@ -26,18 +26,18 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "psr/container": "^2.0", "yiisoft/injector": "^1.1", "yiisoft/strings": "^2.3" }, "require-dev": { - "maglnet/composer-require-checker": "^4.4", - "phpunit/phpunit": "^9.5", + "maglnet/composer-require-checker": "^4.7", + "phpunit/phpunit": "^10.5", "rector/rector": "^1.0.0", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", - "vimeo/psalm": "^4.30|^5.7", + "vimeo/psalm": "^5.23", "yiisoft/di": "^1.2", "yiisoft/dummy-provider": "^1.0", "yiisoft/test-support": "^3.0" diff --git a/infection.json.dist b/infection.json.dist index be3415e..56d0f8f 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -9,11 +9,5 @@ "stryker": { "report": "master" } - }, - "mutators": { - "@default": true, - "global-ignoreSourceCodeByRegex": [ - "if \\(PHP_VERSION_ID.*" - ] } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 18d6999..7f38c7d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,17 +1,16 @@ - - @@ -23,19 +22,15 @@ ./tests/TestEnvironments - - ./tests/TestEnvironments/Php81 - - ./tests/TestEnvironments/Php82 - + - ./src - ./config + ./src + ./config - + diff --git a/rector.php b/rector.php index 8a56c47..6874c65 100644 --- a/rector.php +++ b/rector.php @@ -5,6 +5,8 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector; +use Rector\Php81\Rector\ClassMethod\NewInInitializerRector; +use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; use Rector\Set\ValueObject\LevelSetList; return static function (RectorConfig $rectorConfig): void { @@ -18,11 +20,13 @@ // define sets of rules $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_80, + LevelSetList::UP_TO_PHP_81, ]); $rectorConfig->skip([ ClosureToArrowFunctionRector::class, + NewInInitializerRector::class, + ReadOnlyPropertyRector::class, __DIR__ . '/tests/Support/Classes/SimpleClass.php', ]); }; diff --git a/src/Hydrator.php b/src/Hydrator.php index 7c46cc3..1405d69 100644 --- a/src/Hydrator.php +++ b/src/Hydrator.php @@ -141,11 +141,6 @@ private function hydrateInternal( new TypeCastContext($this, $property), ); if ($result->isResolved()) { - // @codeCoverageIgnoreStart - if (PHP_VERSION_ID < 80100) { - $property->setAccessible(true); - } - // @codeCoverageIgnoreEnd $property->setValue($object, $result->getValue()); } } diff --git a/src/Internal/ReflectionFilter.php b/src/Internal/ReflectionFilter.php index f4dc969..66c357a 100644 --- a/src/Internal/ReflectionFilter.php +++ b/src/Internal/ReflectionFilter.php @@ -30,8 +30,7 @@ public static function filterProperties( continue; } - /** @psalm-suppress UndefinedMethod Need for PHP 8.0 only */ - if (PHP_VERSION_ID >= 80100 && $property->isReadOnly()) { + if ($property->isReadOnly()) { continue; } $propertyName = $property->getName(); diff --git a/tests/Attribute/Parameter/ToStringTest.php b/tests/Attribute/Parameter/ToStringTest.php index d250053..fc433b4 100644 --- a/tests/Attribute/Parameter/ToStringTest.php +++ b/tests/Attribute/Parameter/ToStringTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Hydrator\Tests\Attribute\Parameter; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Hydrator\ArrayData; use Yiisoft\Hydrator\Attribute\Parameter\ToString; @@ -21,7 +22,7 @@ final class ToStringTest extends TestCase { - public function dataBase(): array + public static function dataBase(): array { return [ ['99', 99], @@ -34,9 +35,7 @@ public function dataBase(): array ]; } - /** - * @dataProvider dataBase - */ + #[DataProvider('dataBase')] public function testBase(mixed $expected, mixed $value): void { $attribute = new ToString(); @@ -52,9 +51,7 @@ public function testBase(mixed $expected, mixed $value): void $this->assertSame($expected, $result->getValue()); } - /** - * @dataProvider dataBase - */ + #[DataProvider('dataBase')] public function testBaseWithHydrator(string $expected, mixed $value): void { $hydrator = new Hydrator(); diff --git a/tests/HydratorTest.php b/tests/HydratorTest.php index 03305ba..721aa56 100644 --- a/tests/HydratorTest.php +++ b/tests/HydratorTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Hydrator\Tests; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use RuntimeException; use stdClass; @@ -34,8 +35,10 @@ use Yiisoft\Hydrator\Tests\Support\Classes\ObjectPropertyModel\ObjectPropertyModel; use Yiisoft\Hydrator\Tests\Support\Classes\ObjectPropertyModel\RedCar; use Yiisoft\Hydrator\Tests\Support\Classes\PreparePropertyClass; +use Yiisoft\Hydrator\Tests\Support\Classes\ReadonlyClass; use Yiisoft\Hydrator\Tests\Support\Classes\SimpleClass; use Yiisoft\Hydrator\Tests\Support\Classes\StaticClass; +use Yiisoft\Hydrator\Tests\Support\Classes\StringableCar; use Yiisoft\Hydrator\Tests\Support\Classes\TypeClass; use Yiisoft\Hydrator\Tests\Support\PrivateConstructor; use Yiisoft\Hydrator\Tests\Support\ProtectedConstructor; @@ -115,7 +118,7 @@ public function testSimpleHydrateStrict(): void $this->assertSame('.', $object->getC()); } - public function dataSimpleHydrateWithMap(): array + public static function dataSimpleHydrateWithMap(): array { return [ 'simple' => [ @@ -141,9 +144,7 @@ public function dataSimpleHydrateWithMap(): array ]; } - /** - * @dataProvider dataSimpleHydrateWithMap - */ + #[DataProvider('dataSimpleHydrateWithMap')] public function testSimpleHydrateWithMap(array $data, array $map): void { $hydrator = new Hydrator(); @@ -177,7 +178,7 @@ public function testCreateNestedObject(): void $this->assertSame('Mike Li', $object->getName()); } - public function dataCreateNestedObjectWithMap(): array + public static function dataCreateNestedObjectWithMap(): array { return [ [ @@ -199,9 +200,7 @@ public function dataCreateNestedObjectWithMap(): array ]; } - /** - * @dataProvider dataCreateNestedObjectWithMap - */ + #[DataProvider('dataCreateNestedObjectWithMap')] public function testCreateNestedObjectWithMap(array $data, array $map): void { $hydrator = new Hydrator(); @@ -211,7 +210,7 @@ public function testCreateNestedObjectWithMap(array $data, array $map): void $this->assertSame('Mike Li', $object->getName()); } - public function dataTypeCast(): array + public static function dataTypeCast(): array { return [ // Integer @@ -393,12 +392,26 @@ public function dataTypeCast(): array ['arrayOrString' => 'test'], ['arrayOrString' => new StringableObject('test')], ], + + // int|string + 'int-to-int-string' => [ + ['intString' => 42], + ['intString' => 42], + ], + 'string-to-int-string' => [ + ['intString' => '42'], + ['intString' => '42'], + ], + + // intersection type casting is not supported + 'intersection' => [ + ['intersection' => 'red car'], + ['intersection' => new StringableCar('yellow')], + ], ]; } - /** - * @dataProvider dataTypeCast - */ + #[DataProvider('dataTypeCast')] public function testTypeCast(array $expectedValues, array $data, ?callable $prepareCallable = null): void { $object = new TypeClass(); @@ -420,6 +433,8 @@ public function testTypeCast(array $expectedValues, array $data, ?callable $prep 'float' => -2.0, 'array' => [-1], 'arrayOrString' => 'x', + 'intString' => -1, + 'intersection' => 'red car', ], $expectedValues ); @@ -436,11 +451,13 @@ public function testTypeCast(array $expectedValues, array $data, ?callable $prep 'float' => $object->float, 'array' => $object->array, 'arrayOrString' => $object->arrayOrString, + 'intString' => $object->intString, + 'intersection' => (string) $object->intersection, ] ); } - public function dataConstructorTypeCast(): array + public static function dataConstructorTypeCast(): array { return [ 'array-to-int' => [ @@ -450,9 +467,7 @@ public function dataConstructorTypeCast(): array ]; } - /** - * @dataProvider dataConstructorTypeCast - */ + #[DataProvider('dataConstructorTypeCast')] public function testConstructorTypeCast(array $expectedValues, array $data): void { $hydrator = new Hydrator(); @@ -720,4 +735,16 @@ public function testCreateNonExistClass(): void $this->expectExceptionMessage('Class "NonExistClass" not exist.'); $hydrator->create('NonExistClass'); } + + public function testReadonlyObject(): void + { + $hydrator = new Hydrator(); + + $object = $hydrator->create(ReadonlyClass::class, ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]); + + $this->assertSame(99, $object->a); + $this->assertSame(2, $object->b); + $this->assertSame(3, $object->c); + $this->assertSame(4, $object->d); + } } diff --git a/tests/TestEnvironments/Php81/Support/CarInterface.php b/tests/Support/Classes/CarInterface.php similarity index 60% rename from tests/TestEnvironments/Php81/Support/CarInterface.php rename to tests/Support/Classes/CarInterface.php index 3833ee5..16bf450 100644 --- a/tests/TestEnvironments/Php81/Support/CarInterface.php +++ b/tests/Support/Classes/CarInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php81\Support; +namespace Yiisoft\Hydrator\Tests\Support\Classes; interface CarInterface { diff --git a/tests/TestEnvironments/Php81/Support/ReadonlyClass.php b/tests/Support/Classes/ReadonlyClass.php similarity index 79% rename from tests/TestEnvironments/Php81/Support/ReadonlyClass.php rename to tests/Support/Classes/ReadonlyClass.php index 52a7749..34a937c 100644 --- a/tests/TestEnvironments/Php81/Support/ReadonlyClass.php +++ b/tests/Support/Classes/ReadonlyClass.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php81\Support; +namespace Yiisoft\Hydrator\Tests\Support\Classes; final class ReadonlyClass { diff --git a/tests/TestEnvironments/Php81/Support/StringableCar.php b/tests/Support/Classes/StringableCar.php similarity index 84% rename from tests/TestEnvironments/Php81/Support/StringableCar.php rename to tests/Support/Classes/StringableCar.php index 0204fc2..115b4f9 100644 --- a/tests/TestEnvironments/Php81/Support/StringableCar.php +++ b/tests/Support/Classes/StringableCar.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php81\Support; +namespace Yiisoft\Hydrator\Tests\Support\Classes; use Stringable; diff --git a/tests/Support/Classes/TypeClass.php b/tests/Support/Classes/TypeClass.php index 7413967..65942ca 100644 --- a/tests/Support/Classes/TypeClass.php +++ b/tests/Support/Classes/TypeClass.php @@ -4,6 +4,8 @@ namespace Yiisoft\Hydrator\Tests\Support\Classes; +use Stringable; + final class TypeClass { public $noType = -1; @@ -15,4 +17,11 @@ final class TypeClass public float $float = -2.0; public array $array = [-1]; public array|string $arrayOrString = 'x'; + public int|string $intString = -1; + public CarInterface&Stringable $intersection; + + public function __construct() + { + $this->intersection = new StringableCar('red'); + } } diff --git a/tests/TestEnvironments/Php81/HydratorTest.php b/tests/TestEnvironments/Php81/HydratorTest.php deleted file mode 100644 index 50bf344..0000000 --- a/tests/TestEnvironments/Php81/HydratorTest.php +++ /dev/null @@ -1,74 +0,0 @@ -create(ReadonlyClass::class, ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]); - - $this->assertSame(99, $object->a); - $this->assertSame(2, $object->b); - $this->assertSame(3, $object->c); - $this->assertSame(4, $object->d); - } - - public function dataTypeCast(): array - { - return [ - // int|string - 'int-to-int-string' => [ - ['intString' => 42], - ['intString' => 42], - ], - 'string-to-int-string' => [ - ['intString' => '42'], - ['intString' => '42'], - ], - - // intersection type casting is not supported - 'intersection' => [ - ['intersection' => 'red car'], - ['intersection' => new StringableCar('yellow')], - ], - ]; - } - - /** - * @dataProvider dataTypeCast - */ - public function testTypeCast(array $expectedValues, array $data): void - { - $object = new TypeObject(); - - $hydrator = new Hydrator(); - $hydrator->hydrate($object, $data); - - $expectedValues = array_merge( - [ - 'intString' => -1, - 'intersection' => 'red car', - ], - $expectedValues - ); - - $this->assertSame( - $expectedValues, - [ - 'intString' => $object->intString, - 'intersection' => (string) $object->intersection, - ] - ); - } -} diff --git a/tests/TestEnvironments/Php81/Support/TypeObject.php b/tests/TestEnvironments/Php81/Support/TypeObject.php deleted file mode 100644 index 4a75194..0000000 --- a/tests/TestEnvironments/Php81/Support/TypeObject.php +++ /dev/null @@ -1,18 +0,0 @@ -intersection = new StringableCar('red'); - } -} diff --git a/tests/TestEnvironments/Php81/TypeCaster/PhpNativeTypeCasterTest.php b/tests/TestEnvironments/Php81/TypeCaster/PhpNativeTypeCasterTest.php deleted file mode 100644 index 7232e82..0000000 --- a/tests/TestEnvironments/Php81/TypeCaster/PhpNativeTypeCasterTest.php +++ /dev/null @@ -1,50 +0,0 @@ - [ - Result::success(42), - 42, - static fn(StringableObject|int|string $a) => null, - ], - 'string to object|int|string' => [ - Result::success('42'), - '42', - static fn(StringableObject|int|string $a) => null, - ], - 'string to object|int' => [ - Result::success(42), - '42', - static fn(StringableObject|int $a) => null, - ], - ]; - } - - /** - * @dataProvider dataBase - */ - public function testBase(Result $expected, mixed $value, Closure $closure): void - { - $typeCaster = new PhpNativeTypeCaster(); - $context = TestHelper::createTypeCastContext($closure); - - $result = $typeCaster->cast($value, $context); - - $this->assertSame($expected->isResolved(), $result->isResolved()); - $this->assertSame($expected->getValue(), $result->getValue()); - } -} diff --git a/tests/TestEnvironments/Php82/HydratorTest.php b/tests/TestEnvironments/Php82/HydratorTest.php index f56dd18..2a7e946 100644 --- a/tests/TestEnvironments/Php82/HydratorTest.php +++ b/tests/TestEnvironments/Php82/HydratorTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php82; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Hydrator\Hydrator; use Yiisoft\Hydrator\Tests\TestEnvironments\Php82\Support\ReadonlyClass; @@ -11,7 +12,7 @@ final class HydratorTest extends TestCase { - public function dataTypeCast(): array + public static function dataTypeCast(): array { return [ 'union-intersection' => [ @@ -21,9 +22,7 @@ public function dataTypeCast(): array ]; } - /** - * @dataProvider dataTypeCast - */ + #[DataProvider('dataTypeCast')] public function testTypeCast(array $expectedValues, array $data): void { $object = new TypeObject(); diff --git a/tests/TestEnvironments/Php82/TypeCaster/HydratorTypeCasterTest.php b/tests/TestEnvironments/Php82/TypeCaster/HydratorTypeCasterTest.php index 1440867..07b73fe 100644 --- a/tests/TestEnvironments/Php82/TypeCaster/HydratorTypeCasterTest.php +++ b/tests/TestEnvironments/Php82/TypeCaster/HydratorTypeCasterTest.php @@ -6,6 +6,7 @@ use Closure; use Countable; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Stringable; use Yiisoft\Hydrator\Result; @@ -15,7 +16,7 @@ final class HydratorTypeCasterTest extends TestCase { - public function dataBase(): array + public static function dataBase(): array { return [ 'array to intersection type' => [ @@ -41,9 +42,7 @@ public function dataBase(): array ]; } - /** - * @dataProvider dataBase - */ + #[DataProvider('dataBase')] public function testBase(Result $expected, mixed $value, Closure $closure): void { $typeCaster = new HydratorTypeCaster(); diff --git a/tests/TypeCaster/HydratorTypeCasterTest.php b/tests/TypeCaster/HydratorTypeCasterTest.php index 2e1ea7f..825072c 100644 --- a/tests/TypeCaster/HydratorTypeCasterTest.php +++ b/tests/TypeCaster/HydratorTypeCasterTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Hydrator\Tests\TypeCaster; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Hydrator\Result; use Yiisoft\Hydrator\Tests\Support\PrivateConstructorObject; @@ -14,7 +15,7 @@ final class HydratorTypeCasterTest extends TestCase { - public function dataBase(): array + public static function dataBase(): array { return [ 'not array to not array' => [ @@ -55,9 +56,7 @@ public function dataBase(): array ]; } - /** - * @dataProvider dataBase - */ + #[DataProvider('dataBase')] public function testBase(Result $expected, mixed $value, TypeCastContext $context): void { $typeCaster = new HydratorTypeCaster(); diff --git a/tests/TypeCaster/NullTypeCasterTest.php b/tests/TypeCaster/NullTypeCasterTest.php index 1859c38..afbf400 100644 --- a/tests/TypeCaster/NullTypeCasterTest.php +++ b/tests/TypeCaster/NullTypeCasterTest.php @@ -5,13 +5,14 @@ namespace TypeCaster; use Closure; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Hydrator\Tests\Support\TestHelper; use Yiisoft\Hydrator\TypeCaster\NullTypeCaster; final class NullTypeCasterTest extends TestCase { - public function dataBase(): array + public static function dataBase(): array { return [ 'default, null to non-type' => [ @@ -131,9 +132,7 @@ public function dataBase(): array ]; } - /** - * @dataProvider dataBase - */ + #[DataProvider('dataBase')] public function testBase(bool $success, NullTypeCaster $typeCaster, mixed $value, Closure $closure): void { $context = TestHelper::createTypeCastContext($closure); diff --git a/tests/TypeCaster/PhpNativeTypeCasterTest.php b/tests/TypeCaster/PhpNativeTypeCasterTest.php index 40aca6b..59dd5cd 100644 --- a/tests/TypeCaster/PhpNativeTypeCasterTest.php +++ b/tests/TypeCaster/PhpNativeTypeCasterTest.php @@ -5,14 +5,16 @@ namespace Yiisoft\Hydrator\Tests\TypeCaster; use Closure; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Hydrator\Result; +use Yiisoft\Hydrator\Tests\Support\StringableObject; use Yiisoft\Hydrator\Tests\Support\TestHelper; use Yiisoft\Hydrator\TypeCaster\PhpNativeTypeCaster; final class PhpNativeTypeCasterTest extends TestCase { - public function dataBase(): array + public static function dataBase(): array { return [ 'string to int' => [ @@ -25,12 +27,25 @@ public function dataBase(): array '42.52', static fn(float $a) => null, ], + 'int to object|int|string' => [ + Result::success(42), + 42, + static fn(StringableObject|int|string $a) => null, + ], + 'string to object|int|string' => [ + Result::success('42'), + '42', + static fn(StringableObject|int|string $a) => null, + ], + 'string to object|int' => [ + Result::success(42), + '42', + static fn(StringableObject|int $a) => null, + ], ]; } - /** - * @dataProvider dataBase - */ + #[DataProvider('dataBase')] public function testBase(Result $expected, mixed $value, Closure $closure): void { $typeCaster = new PhpNativeTypeCaster();