From 40e41901f5e7fb15f60f1d0aa6cec7309c12917b Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 4 Jun 2021 14:26:58 +0200 Subject: [PATCH 1/6] Comment out test code that triggers "Deprecated: Implicit conversion from non-compatible float 0.9 to int" with PHP 8.1 --- tests/end-to-end/regression/GitHub/1337.phpt | 4 ++-- tests/end-to-end/regression/GitHub/1337/Issue1337Test.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/end-to-end/regression/GitHub/1337.phpt b/tests/end-to-end/regression/GitHub/1337.phpt index 742d77d47a7..63a06aa15fc 100644 --- a/tests/end-to-end/regression/GitHub/1337.phpt +++ b/tests/end-to-end/regression/GitHub/1337.phpt @@ -12,8 +12,8 @@ PHPUnit\TextUI\Command::main(); --EXPECTF-- PHPUnit %s by Sebastian Bergmann and contributors. -.. 2 / 2 (100%) +. 1 / 1 (100%) Time: %s, Memory: %s -OK (2 tests, 2 assertions) +OK (1 test, 1 assertion) diff --git a/tests/end-to-end/regression/GitHub/1337/Issue1337Test.php b/tests/end-to-end/regression/GitHub/1337/Issue1337Test.php index d479ea7f050..e601fe68fe4 100644 --- a/tests/end-to-end/regression/GitHub/1337/Issue1337Test.php +++ b/tests/end-to-end/regression/GitHub/1337/Issue1337Test.php @@ -23,7 +23,8 @@ public function dataProvider() { return [ 'c:\\' => [true], - 0.9 => [true], + // The following is commented out because it no longer works in PHP >= 8.1 + //0.9 => [true], ]; } } From 1f67d32b1de2a097db8504af82d9455bb7c6e0b8 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 4 Jun 2021 14:27:27 +0200 Subject: [PATCH 2/6] Delete useless tests --- tests/unit/Framework/TestBuilderTest.php | 52 ------------------------ 1 file changed, 52 deletions(-) diff --git a/tests/unit/Framework/TestBuilderTest.php b/tests/unit/Framework/TestBuilderTest.php index 09adbc27692..73edcb5f4f7 100644 --- a/tests/unit/Framework/TestBuilderTest.php +++ b/tests/unit/Framework/TestBuilderTest.php @@ -9,10 +9,8 @@ */ namespace PHPUnit\Framework; -use function assert; use EmptyDataProviderTest; use ModifiedConstructorTestCase; -use PHPUnit\Framework\MockObject\MockObject; use ReflectionClass; use TestWithAnnotations; @@ -21,56 +19,6 @@ */ final class TestBuilderTest extends TestCase { - public function testCreateTestForConstructorlessTestClass(): void - { - $reflector = $this->getMockBuilder(ReflectionClass::class) - ->setConstructorArgs([$this]) - ->getMock(); - - assert($reflector instanceof MockObject); - assert($reflector instanceof ReflectionClass); - - $reflector->expects($this->once()) - ->method('getConstructor') - ->willReturn(null); - - $reflector->expects($this->once()) - ->method('isInstantiable') - ->willReturn(true); - - $reflector->expects($this->once()) - ->method('getName') - ->willReturn(__CLASS__); - - $this->expectException(Exception::class); - $this->expectExceptionMessage('No valid test provided.'); - - (new TestBuilder)->build($reflector, 'TestForConstructorlessTestClass'); - } - - public function testCreateTestForNotInstantiableTestClass(): void - { - $reflector = $this->getMockBuilder(ReflectionClass::class) - ->setConstructorArgs([$this]) - ->getMock(); - - assert($reflector instanceof MockObject); - assert($reflector instanceof ReflectionClass); - - $reflector->expects($this->once()) - ->method('isInstantiable') - ->willReturn(false); - - $reflector->expects($this->once()) - ->method('getName') - ->willReturn('foo'); - - $test = (new TestBuilder)->build($reflector, 'TestForNonInstantiableTestClass'); - $this->assertInstanceOf(WarningTestCase::class, $test); - /* @var WarningTestCase $test */ - $this->assertSame('Cannot instantiate class "foo".', $test->getMessage()); - } - public function testCreateTestForTestClassWithModifiedConstructor(): void { $test = (new TestBuilder)->build(new ReflectionClass(ModifiedConstructorTestCase::class), 'testCase'); From a617443eda4d3ec2d0401600e31e9d7e5c078c26 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 4 Jun 2021 14:27:50 +0200 Subject: [PATCH 3/6] Fix doc comment --- src/Framework/MockObject/MockMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 1aa728f1a95..15a90dceddd 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -53,7 +53,7 @@ final class MockMethod private $cloneArguments; /** - * @var string string + * @var string */ private $modifier; From 398db60ec5f46d93885415c567c66ce140997037 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 4 Jun 2021 14:28:37 +0200 Subject: [PATCH 4/6] Eliminate dead code --- src/Framework/MockObject/MockMethod.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 15a90dceddd..2388d157cc2 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -92,11 +92,6 @@ final class MockMethod */ private $deprecation; - /** - * @var bool - */ - private $allowsReturnNull; - /** * @throws RuntimeException */ @@ -140,8 +135,7 @@ public static function fromReflection(ReflectionMethod $method, bool $callOrigin $reference, $callOriginalMethod, $method->isStatic(), - $deprecation, - $method->hasReturnType() && $method->getReturnType()->allowsNull() + $deprecation ); } @@ -159,11 +153,10 @@ public static function fromName(string $fullClassName, string $methodName, bool false, false, null, - false ); } - public function __construct(string $className, string $methodName, bool $cloneArguments, string $modifier, string $argumentsForDeclaration, string $argumentsForCall, Type $returnType, string $reference, bool $callOriginalMethod, bool $static, ?string $deprecation, bool $allowsReturnNull) + public function __construct(string $className, string $methodName, bool $cloneArguments, string $modifier, string $argumentsForDeclaration, string $argumentsForCall, Type $returnType, string $reference, bool $callOriginalMethod, bool $static, ?string $deprecation) { $this->className = $className; $this->methodName = $methodName; @@ -176,7 +169,6 @@ public function __construct(string $className, string $methodName, bool $cloneAr $this->callOriginalMethod = $callOriginalMethod; $this->static = $static; $this->deprecation = $deprecation; - $this->allowsReturnNull = $allowsReturnNull; } public function getName(): string From f60f6d1cc1358d812939730c577e95d210b5e068 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 4 Jun 2021 14:28:55 +0200 Subject: [PATCH 5/6] Fix CS/WS issue --- src/Framework/MockObject/MockMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 2388d157cc2..69e50b2fb73 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -346,7 +346,7 @@ private static function deriveReturnType(ReflectionMethod $method): Type $returnType = $method->getReturnType(); if ($returnType === null) { - return new UnknownType(); + return new UnknownType; } // @see https://bugs.php.net/bug.php?id=70722 From 757f003e208053265a44473f2fb5ee592a8617c0 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 4 Jun 2021 14:41:08 +0200 Subject: [PATCH 6/6] Closes #4694 --- ChangeLog-8.5.md | 1 + src/Framework/MockObject/MockMethod.php | 36 +++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/ChangeLog-8.5.md b/ChangeLog-8.5.md index 79678eed8af..b46f3df57ce 100644 --- a/ChangeLog-8.5.md +++ b/ChangeLog-8.5.md @@ -13,6 +13,7 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil * [#4663](https://github.com/sebastianbergmann/phpunit/issues/4663): `TestCase::expectError()` works on PHP 7.3, but not on PHP >= 7.4 * [#4678](https://github.com/sebastianbergmann/phpunit/pull/4678): Stubbed methods with `iterable` return types should return empty array by default * [#4692](https://github.com/sebastianbergmann/phpunit/issues/4692): Annotations in single-line doc-comments are not handled correctly +* [#4694](https://github.com/sebastianbergmann/phpunit/issues/4694): `TestCase::getMockFromWsdl()` does not work with PHP 8.1-dev ## [8.5.15] - 2021-03-17 diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 69e50b2fb73..91a686450cc 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -12,15 +12,18 @@ use const DIRECTORY_SEPARATOR; use function implode; use function is_string; +use function method_exists; use function preg_match; use function preg_replace; use function sprintf; +use function str_replace; use function substr_count; use function trim; use function var_export; use ReflectionException; use ReflectionMethod; use ReflectionNamedType; +use ReflectionType; use SebastianBergmann\Type\ObjectType; use SebastianBergmann\Type\Type; use SebastianBergmann\Type\UnknownType; @@ -152,7 +155,7 @@ public static function fromName(string $fullClassName, string $methodName, bool '', false, false, - null, + null ); } @@ -208,13 +211,23 @@ public function generateCode(): string $deprecation = $deprecationTemplate->render(); } + /** + * This is required as the version of sebastian/type used + * by PHPUnit 8.5 does now know about the mixed type. + */ + $returnTypeDeclaration = str_replace( + '?mixed', + 'mixed', + $this->returnType->getReturnTypeDeclaration() + ); + $template = $this->getTemplate($templateFile); $template->setVar( [ 'arguments_decl' => $this->argumentsForDeclaration, 'arguments_call' => $this->argumentsForCall, - 'return_declaration' => $this->returnType->getReturnTypeDeclaration(), + 'return_declaration' => $returnTypeDeclaration, 'arguments_count' => !empty($this->argumentsForCall) ? substr_count($this->argumentsForCall, ',') + 1 : 0, 'class_name' => $this->className, 'method_name' => $this->methodName, @@ -343,19 +356,19 @@ private static function getMethodParametersForCall(ReflectionMethod $method): st private static function deriveReturnType(ReflectionMethod $method): Type { - $returnType = $method->getReturnType(); + $returnType = self::reflectionMethodGetReturnType($method); if ($returnType === null) { return new UnknownType; } // @see https://bugs.php.net/bug.php?id=70722 - if ($returnType->getName() === 'self') { + if ($returnType instanceof ReflectionNamedType && $returnType->getName() === 'self') { return ObjectType::fromName($method->getDeclaringClass()->getName(), $returnType->allowsNull()); } // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/406 - if ($returnType->getName() === 'parent') { + if ($returnType instanceof ReflectionNamedType && $returnType->getName() === 'parent') { $parentClass = $method->getDeclaringClass()->getParentClass(); if ($parentClass === false) { @@ -374,4 +387,17 @@ private static function deriveReturnType(ReflectionMethod $method): Type return Type::fromName($returnType->getName(), $returnType->allowsNull()); } + + private static function reflectionMethodGetReturnType(ReflectionMethod $method): ?ReflectionType + { + if ($method->hasReturnType()) { + return $method->getReturnType(); + } + + if (!method_exists($method, 'getTentativeReturnType')) { + return null; + } + + return $method->getTentativeReturnType(); + } }