Skip to content

Commit

Permalink
Raise the minimum version of PHP to 8.1 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Apr 1, 2024
1 parent 0ef7d40 commit c1c28a5
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
2 changes: 1 addition & 1 deletion .github/workflows/composer-require-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0', '8.1', '8.2', '8.3']
['8.1', '8.2', '8.3']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0', '8.1', '8.2', '8.3']
['8.1', '8.2', '8.3']
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ phpunit.phar
# local phpunit config
/phpunit.xml
# phpunit cache
.phpunit.result.cache
/.phpunit.cache
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Features are:

## Requirements

- PHP 8.0 or higher.
- PHP 8.1 or higher.

## Installation

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 0 additions & 6 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,5 @@
"stryker": {
"report": "master"
}
},
"mutators": {
"@default": true,
"global-ignoreSourceCodeByRegex": [
"if \\(PHP_VERSION_ID.*"
]
}
}
37 changes: 16 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
executionOrder="random"
failOnRisky="true"
failOnWarning="true"
stopOnFailure="false"
colors="true"
>
<php>
<ini name="error_reporting" value="-1"/>
Expand All @@ -23,19 +22,15 @@
<exclude>./tests/TestEnvironments</exclude>
</testsuite>

<testsuite name="Yii Hydrator PHP 8.1 tests">
<directory phpVersion="8.1" phpVersionOperator=">=">./tests/TestEnvironments/Php81</directory>
</testsuite>

<testsuite name="Yii Hydrator PHP 8.2 tests">
<directory phpVersion="8.2" phpVersionOperator=">=">./tests/TestEnvironments/Php82</directory>
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory>./src</directory>
<directory>./config</directory>
<directory suffix=".php">./src</directory>
<directory suffix=".php">./config</directory>
</include>
</coverage>
</source>
</phpunit>
6 changes: 5 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
]);
};
5 changes: 0 additions & 5 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Internal/ReflectionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 4 additions & 7 deletions tests/Attribute/Parameter/ToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,7 @@

final class ToStringTest extends TestCase
{
public function dataBase(): array
public static function dataBase(): array
{
return [
['99', 99],
Expand All @@ -34,9 +35,7 @@ public function dataBase(): array
];
}

/**
* @dataProvider dataBase
*/
#[DataProvider('dataBase')]
public function testBase(mixed $expected, mixed $value): void
{
$attribute = new ToString();
Expand All @@ -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();
Expand Down
59 changes: 43 additions & 16 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Hydrator\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +118,7 @@ public function testSimpleHydrateStrict(): void
$this->assertSame('.', $object->getC());
}

public function dataSimpleHydrateWithMap(): array
public static function dataSimpleHydrateWithMap(): array
{
return [
'simple' => [
Expand All @@ -141,9 +144,7 @@ public function dataSimpleHydrateWithMap(): array
];
}

/**
* @dataProvider dataSimpleHydrateWithMap
*/
#[DataProvider('dataSimpleHydrateWithMap')]
public function testSimpleHydrateWithMap(array $data, array $map): void
{
$hydrator = new Hydrator();
Expand Down Expand Up @@ -177,7 +178,7 @@ public function testCreateNestedObject(): void
$this->assertSame('Mike Li', $object->getName());
}

public function dataCreateNestedObjectWithMap(): array
public static function dataCreateNestedObjectWithMap(): array
{
return [
[
Expand All @@ -199,9 +200,7 @@ public function dataCreateNestedObjectWithMap(): array
];
}

/**
* @dataProvider dataCreateNestedObjectWithMap
*/
#[DataProvider('dataCreateNestedObjectWithMap')]
public function testCreateNestedObjectWithMap(array $data, array $map): void
{
$hydrator = new Hydrator();
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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
);
Expand All @@ -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' => [
Expand All @@ -450,9 +467,7 @@ public function dataConstructorTypeCast(): array
];
}

/**
* @dataProvider dataConstructorTypeCast
*/
#[DataProvider('dataConstructorTypeCast')]
public function testConstructorTypeCast(array $expectedValues, array $data): void
{
$hydrator = new Hydrator();
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php81\Support;
namespace Yiisoft\Hydrator\Tests\Support\Classes;

interface CarInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php81\Support;
namespace Yiisoft\Hydrator\Tests\Support\Classes;

final class ReadonlyClass
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Hydrator\Tests\TestEnvironments\Php81\Support;
namespace Yiisoft\Hydrator\Tests\Support\Classes;

use Stringable;

Expand Down
Loading

0 comments on commit c1c28a5

Please sign in to comment.