From de0e959dd43d881c374f10fe885929eb6672e21a Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 10 Feb 2024 10:52:48 +0100 Subject: [PATCH 1/6] Allow mock creation for PHPUnit 11 --- composer.json | 2 +- .../ValueProvider/Compound/InstanceProvider.php | 7 ++++++- .../ValueProvider/Compound/IntersectionProvider.php | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 21bba6d..a946efe 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": ">=7.4", "doctrine/inflector": "^2.0", "phpdocumentor/type-resolver": "^1.7", - "phpunit/phpunit": "^9.0 || ^10.0" + "phpunit/phpunit": "^9.0 || ^10.0 || ^11.0" }, "require-dev": { "phpmd/phpmd": "@stable", diff --git a/src/Constraint/ValueProvider/Compound/InstanceProvider.php b/src/Constraint/ValueProvider/Compound/InstanceProvider.php index afe9bb4..3dd4462 100644 --- a/src/Constraint/ValueProvider/Compound/InstanceProvider.php +++ b/src/Constraint/ValueProvider/Compound/InstanceProvider.php @@ -40,7 +40,12 @@ public function getValues(): array } else { $mockGenerator = new \PHPUnit\Framework\MockObject\Generator(); } - $instance = $mockGenerator->getMock($this->typehint, [], [], '', false); + + if (method_exists($mockGenerator, 'testDouble')) { + $instance = $mockGenerator->testDouble($this->typehint, true, true, [], [], '', false, false, false, false); + } else { + $instance = $mockGenerator->getMock($this->typehint, [], [], '', false); + } return [$instance]; } diff --git a/src/Constraint/ValueProvider/Compound/IntersectionProvider.php b/src/Constraint/ValueProvider/Compound/IntersectionProvider.php index 41270a4..ab70d46 100644 --- a/src/Constraint/ValueProvider/Compound/IntersectionProvider.php +++ b/src/Constraint/ValueProvider/Compound/IntersectionProvider.php @@ -62,7 +62,13 @@ public function getValues(): array } else { $mockGenerator = new \PHPUnit\Framework\MockObject\Generator(); } - $instance = $mockGenerator->getMockForAbstractClass($className, [], '', false, false); + + // since phpunit 11 mockAbstractClass is deprecated, and will be removed in phpunit 12. + if (method_exists($mockGenerator, 'testDouble')) { + $instance = $mockGenerator->testDouble($className, true, true, [], [], '', false, false, false, false); + } else { + $instance = $mockGenerator->getMockForAbstractClass($className, [], '', false, false); + } return [$instance]; } From 59fba91425f1953d4d708944853e9bf24615ead7 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 10 Feb 2024 10:57:18 +0100 Subject: [PATCH 2/6] Replace `$this->exporter()` with object creation --- src/Constraint/AccessorPairConstraint.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Constraint/AccessorPairConstraint.php b/src/Constraint/AccessorPairConstraint.php index 7d8b6be..7402021 100644 --- a/src/Constraint/AccessorPairConstraint.php +++ b/src/Constraint/AccessorPairConstraint.php @@ -17,6 +17,7 @@ use ReflectionClass; use ReflectionMethod; use ReflectionParameter; +use SebastianBergmann\Exporter\Exporter; class AccessorPairConstraint extends Constraint { @@ -131,7 +132,6 @@ protected function testPropertyDefaults(array $accessorPairs): void /** * Test all accessorPairs, by passing test values to the setter and expect the exact same value back from the getter. - * * @throws Exception */ protected function testAccessorPair(AccessorPair $accessorPair): void @@ -154,7 +154,6 @@ protected function testAccessorPair(AccessorPair $accessorPair): void /** * The setter's parameter has a default value * Call the setter without provider a parameter. Then call the getter and expect the default value return. - * * @throws Exception */ protected function testOptionalParameter(AccessorPair $accessorPair, ReflectionParameter $parameter): void @@ -173,10 +172,11 @@ protected function testOptionalParameter(AccessorPair $accessorPair, ReflectionP $storedValue = $accessorPair->getGetMethod()->invoke($instance); if ($storedValue !== $expectedReturn) { + $exporter = new Exporter(); $this->fail( $accessorPair->getClass()->getNamespaceName(), - "Stored value (" . $this->exporter()->export($storedValue) . ") does not match " . - "default value (" . $this->exporter()->export($expectedReturn) . ")" + "Stored value (" . $exporter->export($storedValue) . ") does not match " . + "default value (" . $exporter->export($expectedReturn) . ")" ); } } @@ -218,10 +218,11 @@ protected function testParameter(ReflectionParameter $parameter, AccessorPair $a } if ($storedValue !== $expectedReturn) { + $exporter = new Exporter(); $this->fail( $accessorPair->getClass()->getNamespaceName(), - "Stored value (" . $this->exporter()->export($storedValue) . ") does not match " . - "given value (" . $this->exporter()->export($expectedReturn) . ")" + "Stored value (" . $exporter->export($storedValue) . ") does not match " . + "given value (" . $exporter->export($expectedReturn) . ")" ); } } @@ -263,10 +264,11 @@ protected function testConstructorPair(ConstructorPair $constructorPair): void } if ($storedValue !== $expectedReturn) { + $exporter = new Exporter(); $this->fail( $constructorPair->getClass()->getNamespaceName(), - "Stored value (" . $this->exporter()->export($storedValue) . ") does not match " . - "given value (" . $this->exporter()->export($expectedReturn) . ")" + "Stored value (" . $exporter->export($storedValue) . ") does not match " . + "given value (" . $exporter->export($expectedReturn) . ")" ); } } From 2562f9a6ada16aec6c7f7d4901a74af3776d5b63 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 10 Feb 2024 11:08:59 +0100 Subject: [PATCH 3/6] Add additional argument to testDouble method call for phpunit >=11 --- src/Constraint/ValueProvider/Compound/InstanceProvider.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Constraint/ValueProvider/Compound/InstanceProvider.php b/src/Constraint/ValueProvider/Compound/InstanceProvider.php index d12c089..9caa0c4 100644 --- a/src/Constraint/ValueProvider/Compound/InstanceProvider.php +++ b/src/Constraint/ValueProvider/Compound/InstanceProvider.php @@ -6,6 +6,7 @@ use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProvider; use LogicException; use PHPUnit\Framework\MockObject\Generator\Generator; +use PHPUnit\Runner\Version; use UnitEnum; class InstanceProvider implements ValueProvider @@ -38,7 +39,11 @@ public function getValues(): array /** @var \PHPUnit\Framework\MockObject\Generator $mockGenerator */ $mockGenerator = new Generator(); if (method_exists($mockGenerator, 'testDouble')) { - $instance = $mockGenerator->testDouble($this->typehint, true, [], [], '', false); + if (Version::majorVersionNumber() >= 11) { + $instance = $mockGenerator->testDouble($this->typehint, true, true, [], [], '', false); + } else { + $instance = $mockGenerator->testDouble($this->typehint, true, [], [], '', false); + } } else { $instance = $mockGenerator->getMock($this->typehint, [], [], '', false); } From 664d24251e2727c95cf48896e809e3034de5d231 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 10 Feb 2024 11:14:17 +0100 Subject: [PATCH 4/6] Add additional argument to testDouble method call for phpunit >=11 --- src/Constraint/ValueProvider/Compound/InstanceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Constraint/ValueProvider/Compound/InstanceProvider.php b/src/Constraint/ValueProvider/Compound/InstanceProvider.php index 9caa0c4..5f881f2 100644 --- a/src/Constraint/ValueProvider/Compound/InstanceProvider.php +++ b/src/Constraint/ValueProvider/Compound/InstanceProvider.php @@ -39,7 +39,7 @@ public function getValues(): array /** @var \PHPUnit\Framework\MockObject\Generator $mockGenerator */ $mockGenerator = new Generator(); if (method_exists($mockGenerator, 'testDouble')) { - if (Version::majorVersionNumber() >= 11) { + if (method_exists(Version::class, 'majorVersionNumber') && Version::majorVersionNumber() >= 11) { $instance = $mockGenerator->testDouble($this->typehint, true, true, [], [], '', false); } else { $instance = $mockGenerator->testDouble($this->typehint, true, [], [], '', false); From 5a22e9c6b1447569cf5f11908332c0fd5f6b5d85 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 10 Feb 2024 11:19:12 +0100 Subject: [PATCH 5/6] Baseline phpstan --- phpstan-baseline.neon | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 8aeda48..6992b3a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -14,3 +14,8 @@ parameters: message: "#^Class DigitalRevolution\\\\AccessorPairConstraint\\\\Tests\\\\Integration\\\\data\\\\TestEnum not found\\.$#" count: 2 path: tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php + + - + message: "#^Call to function method_exists\\(\\) with 'PHPUnit\\\\\\\\Runner\\\\\\\\Version' and 'majorVersionNumber' will always evaluate to false\\.$#" + count: 1 + path: src/Constraint/ValueProvider/Compound/InstanceProvider.php From bf4643f64b20e09997aaae2b3fd5bd80e1f0e1b3 Mon Sep 17 00:00:00 2001 From: Frank Dekker Date: Sat, 10 Feb 2024 11:21:00 +0100 Subject: [PATCH 6/6] Add php8.3 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7f9e1d2..0db5bc9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.4', '8.0', '8.1', '8.2'] + php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] composer-flags: ['', '--prefer-lowest'] steps: - uses: actions/checkout@v3