From 5c4407ef1fcd562f15bad66c1862ec00cb42264c Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Sat, 27 Apr 2019 17:07:14 +0200 Subject: [PATCH 01/11] #3122 #3602 PHPUnit Code Sprint with bschulze --- .../IncompatibleReturnValueException.php | 17 ++ .../MockObject/Builder/InvocationMocker.php | 53 ++++++- .../MockObject/ConfigurableMethod.php | 39 +++++ .../MockObject/ConfigurableMethods.php | 29 ++++ src/Framework/MockObject/Generator.php | 57 ++----- .../Generator/mocked_class.tpl.dist | 7 +- .../Generator/mocked_method.tpl.dist | 4 +- .../Generator/mocked_method_void.tpl.dist | 4 +- .../Generator/mocked_static_method.tpl.dist | 2 +- .../Generator/proxied_method.tpl.dist | 4 +- .../Generator/proxied_method_void.tpl.dist | 4 +- .../MockObject/Generator/trait_class.tpl.dist | 2 + .../MockObject/Generator/wsdl_class.tpl.dist | 2 + src/Framework/MockObject/Invocation.php | 2 + src/Framework/MockObject/InvocationMocker.php | 2 +- .../MockObject/Matcher/MethodName.php | 4 + src/Framework/MockObject/MockBrick.php | 8 + src/Framework/MockObject/MockClass.php | 57 +++++++ src/Framework/MockObject/MockMethod.php | 148 +++++++++--------- src/Framework/MockObject/MockTrait.php | 50 ++++++ src/Framework/MockObject/ObjectType.php | 52 ++++++ src/Framework/MockObject/SimpleType.php | 64 ++++++++ src/Framework/MockObject/Type.php | 57 +++++++ src/Framework/MockObject/TypeName.php | 79 ++++++++++ src/Framework/MockObject/UnknownType.php | 23 +++ tests/_files/mock-object/ChildClass.php | 14 ++ .../ClassAUsingConfigurableMethods.php | 19 +++ .../ClassBUsingConfigurableMethods.php | 22 +++ .../ClassWithoutParentButParentReturnType.php | 20 +++ tests/_files/mock-object/ParentClass.php | 17 ++ .../someNamespaceA/NamespacedClass.php | 7 + .../someNamespaceB/NamespacedClass.php | 7 + .../mock-objects/generator/232.phpt | 11 +- .../3154_namespaced_constant_resolving.phpt | 13 +- .../mock-objects/generator/3530.phpt | 2 + .../mock-objects/generator/397.phpt | 13 +- .../generator/abstract_class.phpt | 11 +- .../mock-objects/generator/class.phpt | 11 +- .../generator/class_call_parent_clone.phpt | 11 +- .../class_call_parent_constructor.phpt | 11 +- .../class_dont_call_parent_clone.phpt | 11 +- .../class_dont_call_parent_constructor.phpt | 11 +- ...ing_interface_call_parent_constructor.phpt | 11 +- ...nterface_dont_call_parent_constructor.phpt | 11 +- .../generator/class_nonexistent_method.phpt | 11 +- .../mock-objects/generator/class_partial.phpt | 11 +- .../class_with_deprecated_method.phpt | 11 +- .../generator/class_with_final_method.phpt | 11 +- .../class_with_method_named_method.phpt | 11 +- ...ullable_typehinted_variadic_arguments.phpt | 11 +- ...od_with_typehinted_variadic_arguments.phpt | 11 +- ...s_with_method_with_variadic_arguments.phpt | 11 +- .../constant_as_parameter_default_value.phpt | 11 +- .../mock-objects/generator/interface.phpt | 11 +- .../invocation_object_clone_object.phpt | 11 +- .../generator/namespaced_class.phpt | 11 +- .../namespaced_class_call_parent_clone.phpt | 11 +- ...espaced_class_call_parent_constructor.phpt | 11 +- ...mespaced_class_dont_call_parent_clone.phpt | 11 +- ...ed_class_dont_call_parent_constructor.phpt | 11 +- ...ing_interface_call_parent_constructor.phpt | 11 +- ...nterface_dont_call_parent_constructor.phpt | 11 +- .../generator/namespaced_class_partial.phpt | 11 +- .../generator/namespaced_interface.phpt | 11 +- .../generator/nonexistent_class.phpt | 11 +- .../nonexistent_class_with_namespace.phpt | 11 +- ...ith_namespace_starting_with_separator.phpt | 11 +- .../generator/nullable_types.phpt | 11 +- .../mock-objects/generator/proxy.phpt | 11 +- .../return_type_declarations_closure.phpt | 13 +- .../return_type_declarations_final.phpt | 13 +- .../return_type_declarations_generator.phpt | 13 +- .../return_type_declarations_nullable.phpt | 13 +- ...eturn_type_declarations_object_method.phpt | 13 +- .../return_type_declarations_parent.phpt | 13 +- .../return_type_declarations_self.phpt | 13 +- ...eturn_type_declarations_static_method.phpt | 11 +- .../return_type_declarations_void.phpt | 13 +- .../generator/scalar_type_declarations.phpt | 11 +- .../mock-objects/generator/wsdl_class.phpt | 2 + .../generator/wsdl_class_namespace.phpt | 2 + .../generator/wsdl_class_partial.phpt | 2 + .../call_original_with_return_type_void.phpt | 4 +- .../return_by_reference_with_return_type.phpt | 4 +- .../mock-objects/mock-method/return_type.phpt | 4 +- .../mock-method/return_type_parent.phpt | 4 +- .../mock-method/return_type_self.phpt | 4 +- .../regression/GitHub/2366/Issue2366Test.php | 4 +- .../Builder/InvocationMockerTest.php | 76 ++++++--- .../MockObject/ConfigurableMethodTest.php | 28 ++++ .../MockObject/ConfigurableMethodsTest.php | 20 +++ .../Framework/MockObject/MockMethodTest.php | 20 +-- .../Framework/MockObject/ObjectTypeTest.php | 84 ++++++++++ .../Framework/MockObject/TypeNameTest.php | 56 +++++++ 94 files changed, 1343 insertions(+), 375 deletions(-) create mode 100644 src/Framework/Exception/IncompatibleReturnValueException.php create mode 100644 src/Framework/MockObject/ConfigurableMethod.php create mode 100644 src/Framework/MockObject/ConfigurableMethods.php create mode 100644 src/Framework/MockObject/MockBrick.php create mode 100644 src/Framework/MockObject/MockClass.php create mode 100644 src/Framework/MockObject/MockTrait.php create mode 100644 src/Framework/MockObject/ObjectType.php create mode 100644 src/Framework/MockObject/SimpleType.php create mode 100644 src/Framework/MockObject/Type.php create mode 100644 src/Framework/MockObject/TypeName.php create mode 100644 src/Framework/MockObject/UnknownType.php create mode 100644 tests/_files/mock-object/ChildClass.php create mode 100644 tests/_files/mock-object/ClassAUsingConfigurableMethods.php create mode 100644 tests/_files/mock-object/ClassBUsingConfigurableMethods.php create mode 100644 tests/_files/mock-object/ClassWithoutParentButParentReturnType.php create mode 100644 tests/_files/mock-object/ParentClass.php create mode 100644 tests/_files/namespace/someNamespaceA/NamespacedClass.php create mode 100644 tests/_files/namespace/someNamespaceB/NamespacedClass.php create mode 100644 tests/unit/Framework/MockObject/ConfigurableMethodTest.php create mode 100644 tests/unit/Framework/MockObject/ConfigurableMethodsTest.php create mode 100644 tests/unit/Framework/MockObject/ObjectTypeTest.php create mode 100644 tests/unit/Framework/MockObject/TypeNameTest.php diff --git a/src/Framework/Exception/IncompatibleReturnValueException.php b/src/Framework/Exception/IncompatibleReturnValueException.php new file mode 100644 index 00000000000..9e1c2147cc9 --- /dev/null +++ b/src/Framework/Exception/IncompatibleReturnValueException.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class IncompatibleReturnValueException extends Exception +{ +} diff --git a/src/Framework/MockObject/Builder/InvocationMocker.php b/src/Framework/MockObject/Builder/InvocationMocker.php index 9186c26faa6..d3b568688e2 100644 --- a/src/Framework/MockObject/Builder/InvocationMocker.php +++ b/src/Framework/MockObject/Builder/InvocationMocker.php @@ -10,6 +10,8 @@ namespace PHPUnit\Framework\MockObject\Builder; use PHPUnit\Framework\Constraint\Constraint; +use PHPUnit\Framework\IncompatibleReturnValueException; +use PHPUnit\Framework\MockObject\ConfigurableMethod; use PHPUnit\Framework\MockObject\Matcher; use PHPUnit\Framework\MockObject\Matcher\Invocation; use PHPUnit\Framework\MockObject\RuntimeException; @@ -32,11 +34,11 @@ final class InvocationMocker implements MethodNameMatch private $matcher; /** - * @var string[] + * @var ConfigurableMethod[] */ private $configurableMethods; - public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, array $configurableMethods) + public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, ConfigurableMethod... $configurableMethods) { $this->collection = $collection; $this->matcher = new Matcher($invocationMatcher); @@ -68,11 +70,12 @@ public function will(Stub $stub): Identity public function willReturn($value, ...$nextValues): self { if (\count($nextValues) === 0) { + $this->ensureTypeOfReturnValues([$value]); $stub = new Stub\ReturnStub($value); } else { - $stub = new Stub\ConsecutiveCalls( - \array_merge([$value], $nextValues) - ); + $values = \array_merge([$value], $nextValues); + $this->ensureTypeOfReturnValues($values); + $stub = new Stub\ConsecutiveCalls($values); } return $this->will($stub); @@ -193,7 +196,13 @@ public function method($constraint): self ); } - if (\is_string($constraint) && !\in_array(\strtolower($constraint), $this->configurableMethods, true)) { + $configurableMethodNames = array_map( + function (ConfigurableMethod $configurable) { + return strtolower($configurable->getName()); + }, + $this->configurableMethods + ); + if (\is_string($constraint) && !\in_array(\strtolower($constraint), $configurableMethodNames, true)) { throw new RuntimeException( \sprintf( 'Trying to configure method "%s" which cannot be configured because it does not exist, has not been specified, is final, or is static', @@ -227,4 +236,36 @@ private function canDefineParameters(): void ); } } + + private function getConfiguredMethod(): ?ConfigurableMethod + { + $configuredMethod = null; + foreach ($this->configurableMethods as $configurableMethod) { + if ($this->matcher->getMethodNameMatcher()->matchesMethodName($configurableMethod->getName())) { + if ($configuredMethod !== null) { + return null; + } + $configuredMethod = $configurableMethod; + } + } + return $configuredMethod; + } + + private function ensureTypeOfReturnValues(array $values): void + { + $configuredMethod = $this->getConfiguredMethod(); + if ($configuredMethod === null) { + return; + } + foreach ($values as $value) { + if (!$configuredMethod->mayReturn($value)) { + throw new IncompatibleReturnValueException(sprintf( + 'Method %s may not return value of type %s', + $configuredMethod->getName(), + gettype($value) + )); + } + } + } + } diff --git a/src/Framework/MockObject/ConfigurableMethod.php b/src/Framework/MockObject/ConfigurableMethod.php new file mode 100644 index 00000000000..0bf094fcdb6 --- /dev/null +++ b/src/Framework/MockObject/ConfigurableMethod.php @@ -0,0 +1,39 @@ +name = $name; + $this->returnType = $returnType; + } + + public function getName(): string + { + return $this->name; + } + + public function mayReturn($value): bool + { + if (isNull($value) && $this->returnType->allowsNull()) { + return true; + } + return $this->returnType->isAssignable(Type::fromValue($value, false)); + } + +} diff --git a/src/Framework/MockObject/ConfigurableMethods.php b/src/Framework/MockObject/ConfigurableMethods.php new file mode 100644 index 00000000000..5eb2e5e9de3 --- /dev/null +++ b/src/Framework/MockObject/ConfigurableMethods.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Framework\MockObject; + +trait ConfigurableMethods +{ + + /** + * @var ConfigurableMethods[] + */ + private static $__phpunit_configurableMethods; + + public static function __phpunit_initConfigurableMethods(\PHPUnit\Framework\MockObject\ConfigurableMethod... $configurable) + { + if (isset(static::$__phpunit_configurableMethods)) { + // TODO: improve exception + throw new \Exception('nogo!'); + } + static::$__phpunit_configurableMethods = $configurable; + } +} diff --git a/src/Framework/MockObject/Generator.php b/src/Framework/MockObject/Generator.php index 20fee252bb1..cf9d53e6afb 100644 --- a/src/Framework/MockObject/Generator.php +++ b/src/Framework/MockObject/Generator.php @@ -167,8 +167,7 @@ function ($type) { ); return $this->getObject( - $mock['code'], - $mock['mockClassName'], + $mock, $type, $callOriginalConstructor, $callAutoload, @@ -263,10 +262,8 @@ public function getMockForTrait(string $traitName, array $arguments = [], string ] ); - $this->evalClass( - $classTemplate->render(), - $className['className'] - ); + $mockClass = new MockTrait($classTemplate->render(), $className['className']); + $mockClass->bringIntoExistence(); return $this->getMockForAbstractClass($className['className'], $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $mockedMethods, $cloneArguments); } @@ -303,15 +300,10 @@ public function getObjectForTrait(string $traitName, string $traitClassName = '' ] ); - return $this->getObject($classTemplate->render(), $className['className']); + return $this->getObject(new MockTrait($classTemplate->render(), $className['className'])); } - /** - * @param array|string $type - * - * @throws RuntimeException - */ - public function generate($type, array $methods = null, string $mockClassName = '', bool $callOriginalClone = true, bool $callAutoload = true, bool $cloneArguments = true, bool $callOriginalMethods = false): array + public function generate($type, array $methods = null, string $mockClassName = '', bool $callOriginalClone = true, bool $callAutoload = true, bool $cloneArguments = true, bool $callOriginalMethods = false): MockClass { if (\is_array($type)) { \sort($type); @@ -522,14 +514,9 @@ private function getInterfaceOwnMethods(string $interfaceName): array return $methods; } - /** - * @param array|string $type - * - * @throws RuntimeException - */ - private function getObject(string $code, string $className, $type = '', bool $callOriginalConstructor = false, bool $callAutoload = false, array $arguments = [], bool $callOriginalMethods = false, object $proxyTarget = null, bool $returnValueGeneration = true) + private function getObject(MockBrick $mockClass, $type = '', bool $callOriginalConstructor = false, bool $callAutoload = false, array $arguments = [], bool $callOriginalMethods = false, object $proxyTarget = null, bool $returnValueGeneration = true) { - $this->evalClass($code, $className); + $className = $mockClass->bringIntoExistence(); if ($callOriginalConstructor && \is_string($type) && @@ -586,20 +573,12 @@ private function getObject(string $code, string $className, $type = '', bool $ca return $object; } - private function evalClass(string $code, string $className): void - { - if (!\class_exists($className, false)) { - eval($code); - } - } - /** * @param array|string $type - * @param null|array $explicitMethods * * @throws RuntimeException */ - private function generateMock($type, $explicitMethods, string $mockClassName, bool $callOriginalClone, bool $callAutoload, bool $cloneArguments, bool $callOriginalMethods): array + private function generateMock($type, ?array $explicitMethods, string $mockClassName, bool $callOriginalClone, bool $callAutoload, bool $cloneArguments, bool $callOriginalMethods): MockClass { $classTemplate = $this->getTemplate('mocked_class.tpl'); @@ -821,7 +800,7 @@ private function generateMock($type, $explicitMethods, string $mockClassName, bo foreach ($mockMethods->asArray() as $mockMethod) { $mockedMethods .= $mockMethod->generateCode(); - $configurable[] = \strtolower($mockMethod->getName()); + $configurable[] = new ConfigurableMethod($mockMethod->getName(), $mockMethod->getReturnType()); } $method = ''; @@ -843,22 +822,14 @@ private function generateMock($type, $explicitMethods, string $mockClassName, bo 'mock_class_name' => $mockClassName['className'], 'mocked_methods' => $mockedMethods, 'method' => $method, - 'configurable' => '[' . \implode( - ', ', - \array_map( - function ($m) { - return '\'' . $m . '\''; - }, - $configurable - ) - ) . ']', ] ); - return [ - 'code' => $classTemplate->render(), - 'mockClassName' => $mockClassName['className'], - ]; + return new MockClass( + $classTemplate->render(), + $mockClassName['className'], + $configurable + ); } /** diff --git a/src/Framework/MockObject/Generator/mocked_class.tpl.dist b/src/Framework/MockObject/Generator/mocked_class.tpl.dist index cff1dbfc8a1..3628b685b24 100644 --- a/src/Framework/MockObject/Generator/mocked_class.tpl.dist +++ b/src/Framework/MockObject/Generator/mocked_class.tpl.dist @@ -1,8 +1,11 @@ +declare(strict_types=1); + {prologue}{class_declaration} { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = {configurable}; private $__phpunit_returnValueGeneration = true; {clone}{mocked_methods} @@ -24,7 +27,7 @@ public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/src/Framework/MockObject/Generator/mocked_method.tpl.dist b/src/Framework/MockObject/Generator/mocked_method.tpl.dist index cff0cffcbf3..5e69c792d13 100644 --- a/src/Framework/MockObject/Generator/mocked_method.tpl.dist +++ b/src/Framework/MockObject/Generator/mocked_method.tpl.dist @@ -1,5 +1,5 @@ - {modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type} + {modifier} function {reference}{method_name}({arguments_decl}){return_declaration} {{deprecation} $__phpunit_arguments = [{arguments_call}]; $__phpunit_count = func_num_args(); @@ -14,7 +14,7 @@ $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - '{class_name}', '{method_name}', $__phpunit_arguments, '{return_type}', $this, {clone_arguments} + '{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments} ) ); diff --git a/src/Framework/MockObject/Generator/mocked_method_void.tpl.dist b/src/Framework/MockObject/Generator/mocked_method_void.tpl.dist index fe267f4cd3f..ecef25e2449 100644 --- a/src/Framework/MockObject/Generator/mocked_method_void.tpl.dist +++ b/src/Framework/MockObject/Generator/mocked_method_void.tpl.dist @@ -1,5 +1,5 @@ - {modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type} + {modifier} function {reference}{method_name}({arguments_decl}){return_declaration} {{deprecation} $__phpunit_arguments = [{arguments_call}]; $__phpunit_count = func_num_args(); @@ -14,7 +14,7 @@ $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - '{class_name}', '{method_name}', $__phpunit_arguments, '{return_type}', $this, {clone_arguments} + '{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments} ) ); } diff --git a/src/Framework/MockObject/Generator/mocked_static_method.tpl.dist b/src/Framework/MockObject/Generator/mocked_static_method.tpl.dist index 56b561b65f2..5e5cf23cddd 100644 --- a/src/Framework/MockObject/Generator/mocked_static_method.tpl.dist +++ b/src/Framework/MockObject/Generator/mocked_static_method.tpl.dist @@ -1,5 +1,5 @@ - {modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type} + {modifier} function {reference}{method_name}({arguments_decl}){return_declaration} { throw new \PHPUnit\Framework\MockObject\BadMethodCallException('Static method "{method_name}" cannot be invoked on mock object'); } diff --git a/src/Framework/MockObject/Generator/proxied_method.tpl.dist b/src/Framework/MockObject/Generator/proxied_method.tpl.dist index ca94a5f1598..59fc6dd93ce 100644 --- a/src/Framework/MockObject/Generator/proxied_method.tpl.dist +++ b/src/Framework/MockObject/Generator/proxied_method.tpl.dist @@ -1,5 +1,5 @@ - {modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type} + {modifier} function {reference}{method_name}({arguments_decl}){return_declaration} { $__phpunit_arguments = [{arguments_call}]; $__phpunit_count = func_num_args(); @@ -14,7 +14,7 @@ $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - '{class_name}', '{method_name}', $__phpunit_arguments, '{return_type}', $this, {clone_arguments}, true + '{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments}, true ) ); diff --git a/src/Framework/MockObject/Generator/proxied_method_void.tpl.dist b/src/Framework/MockObject/Generator/proxied_method_void.tpl.dist index 6208319b541..099e83815c7 100644 --- a/src/Framework/MockObject/Generator/proxied_method_void.tpl.dist +++ b/src/Framework/MockObject/Generator/proxied_method_void.tpl.dist @@ -1,5 +1,5 @@ - {modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type} + {modifier} function {reference}{method_name}({arguments_decl}){return_declaration} { $__phpunit_arguments = [{arguments_call}]; $__phpunit_count = func_num_args(); @@ -14,7 +14,7 @@ $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - '{class_name}', '{method_name}', $__phpunit_arguments, '{return_type}', $this, {clone_arguments}, true + '{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments}, true ) ); diff --git a/src/Framework/MockObject/Generator/trait_class.tpl.dist b/src/Framework/MockObject/Generator/trait_class.tpl.dist index 4143b0f66af..a8fe470fdc4 100644 --- a/src/Framework/MockObject/Generator/trait_class.tpl.dist +++ b/src/Framework/MockObject/Generator/trait_class.tpl.dist @@ -1,3 +1,5 @@ +declare(strict_types=1); + {prologue}class {class_name} { use {trait_name}; diff --git a/src/Framework/MockObject/Generator/wsdl_class.tpl.dist b/src/Framework/MockObject/Generator/wsdl_class.tpl.dist index cc69fd341cc..b3100b41417 100644 --- a/src/Framework/MockObject/Generator/wsdl_class.tpl.dist +++ b/src/Framework/MockObject/Generator/wsdl_class.tpl.dist @@ -1,3 +1,5 @@ +declare(strict_types=1); + {namespace}class {class_name} extends \SoapClient { public function __construct($wsdl, array $options) diff --git a/src/Framework/MockObject/Invocation.php b/src/Framework/MockObject/Invocation.php index 9224c10190f..dada8047526 100644 --- a/src/Framework/MockObject/Invocation.php +++ b/src/Framework/MockObject/Invocation.php @@ -61,6 +61,8 @@ public function __construct(string $className, string $methodName, array $parame $this->object = $object; $this->proxiedCall = $proxiedCall; + $returnType = ltrim($returnType, ': '); + if (\strtolower($methodName) === '__tostring') { $returnType = 'string'; } diff --git a/src/Framework/MockObject/InvocationMocker.php b/src/Framework/MockObject/InvocationMocker.php index c56372d3e1c..b623bd3e15e 100644 --- a/src/Framework/MockObject/InvocationMocker.php +++ b/src/Framework/MockObject/InvocationMocker.php @@ -93,7 +93,7 @@ public function expects(MatcherInvocation $matcher): BuilderInvocationMocker return new BuilderInvocationMocker( $this, $matcher, - $this->configurableMethods + ...$this->configurableMethods ); } diff --git a/src/Framework/MockObject/Matcher/MethodName.php b/src/Framework/MockObject/Matcher/MethodName.php index 5855ee2ea20..cfb55357530 100644 --- a/src/Framework/MockObject/Matcher/MethodName.php +++ b/src/Framework/MockObject/Matcher/MethodName.php @@ -62,4 +62,8 @@ public function matches(BaseInvocation $invocation): bool { return $this->constraint->evaluate($invocation->getMethodName(), '', true); } + + public function matchesMethodName(string $methodName) : bool { + return $this->constraint->evaluate($methodName, '', true); + } } diff --git a/src/Framework/MockObject/MockBrick.php b/src/Framework/MockObject/MockBrick.php new file mode 100644 index 00000000000..2869e2f62f2 --- /dev/null +++ b/src/Framework/MockObject/MockBrick.php @@ -0,0 +1,8 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Framework\MockObject; + +class MockClass implements MockBrick +{ + + /** + * @var string + */ + private $classCode; + + /** + * @var string + */ + private $mockName; + + /** + * @var ConfigurableMethod[] + */ + private $configurableMethods; + + public function __construct(string $classCode, string $mockName, array $configurableMethods) + { + $this->classCode = $classCode; + $this->mockName = $mockName; + $this->configurableMethods = $configurableMethods; + } + + public function getClassCode(): string + { + return $this->classCode; + } + + private function getMockName(): string + { + return $this->mockName; + } + + public function bringIntoExistence(): string + { + if (!\class_exists($this->getMockName(), false)) { + eval($this->getClassCode()); + call_user_func(array($this->getMockName(), '__phpunit_initConfigurableMethods'), ...$this->configurableMethods); + } + return $this->getMockName(); + } + +} diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 793cbe8d2ff..7e2a66663c6 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace PHPUnit\Framework\MockObject; /** @@ -50,7 +51,7 @@ final class MockMethod private $argumentsForCall; /** - * @var string + * @var Type */ private $returnType; @@ -84,6 +85,8 @@ final class MockMethod */ public static function fromReflection(\ReflectionMethod $method, bool $callOriginalMethod, bool $cloneArguments): self { + $className = $method->getDeclaringClass()->getName(); + if ($method->isPrivate()) { $modifier = 'private'; } elseif ($method->isProtected()) { @@ -102,12 +105,6 @@ public static function fromReflection(\ReflectionMethod $method, bool $callOrigi $reference = ''; } - if ($method->hasReturnType()) { - $returnType = (string) $method->getReturnType(); - } else { - $returnType = ''; - } - $docComment = $method->getDocComment(); if (\is_string($docComment) && @@ -118,13 +115,13 @@ public static function fromReflection(\ReflectionMethod $method, bool $callOrigi } return new self( - $method->getDeclaringClass()->getName(), + $className, $method->getName(), $cloneArguments, $modifier, self::getMethodParameters($method), self::getMethodParameters($method, true), - $returnType, + self::deriveReturnType($method), $reference, $callOriginalMethod, $method->isStatic(), @@ -142,7 +139,7 @@ public static function fromName(string $fullClassName, string $methodName, bool 'public', '', '', - '', + new UnknownType(), '', false, false, @@ -151,20 +148,20 @@ public static function fromName(string $fullClassName, string $methodName, bool ); } - public function __construct(string $className, string $methodName, bool $cloneArguments, string $modifier, string $argumentsForDeclaration, string $argumentsForCall, string $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, bool $allowsReturnNull) { - $this->className = $className; - $this->methodName = $methodName; - $this->cloneArguments = $cloneArguments; - $this->modifier = $modifier; + $this->className = $className; + $this->methodName = $methodName; + $this->cloneArguments = $cloneArguments; + $this->modifier = $modifier; $this->argumentsForDeclaration = $argumentsForDeclaration; - $this->argumentsForCall = $argumentsForCall; - $this->returnType = $returnType; - $this->reference = $reference; - $this->callOriginalMethod = $callOriginalMethod; - $this->static = $static; - $this->deprecation = $deprecation; - $this->allowsReturnNull = $allowsReturnNull; + $this->argumentsForCall = $argumentsForCall; + $this->returnType = $returnType; + $this->reference = $reference; + $this->callOriginalMethod = $callOriginalMethod; + $this->static = $static; + $this->deprecation = $deprecation; + $this->allowsReturnNull = $allowsReturnNull; } public function getName(): string @@ -179,7 +176,7 @@ public function generateCode(): string { if ($this->static) { $templateFile = 'mocked_static_method.tpl'; - } elseif ($this->returnType === 'void') { + } elseif ($this->returnType->getReturnTypeDeclaration() === ': void') { $templateFile = \sprintf( '%s_method_void.tpl', $this->callOriginalMethod ? 'proxied' : 'mocked' @@ -191,43 +188,10 @@ public function generateCode(): string ); } - $returnType = $this->returnType; - - // @see https://bugs.php.net/bug.php?id=70722 - if ($returnType === 'self') { - $returnType = $this->className; - } - - // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/406 - if ($returnType === 'parent') { - try { - $parentClass = (new \ReflectionClass($this->className))->getParentClass(); - } catch (\ReflectionException $e) { - throw new RuntimeException( - $e->getMessage(), - (int) $e->getCode(), - $e - ); - } - - if ($parentClass === false) { - throw new RuntimeException( - \sprintf( - 'Cannot mock %s::%s because "parent" return type declaration is used but %s does not have a parent class', - $this->className, - $this->methodName, - $this->className - ) - ); - } - - $returnType = $parentClass->getName(); - } - $deprecation = $this->deprecation; if (null !== $this->deprecation) { - $deprecation = "The $this->className::$this->methodName method is deprecated ($this->deprecation)."; + $deprecation = "The $this->className::$this->methodName method is deprecated ($this->deprecation)."; $deprecationTemplate = $this->getTemplate('deprecation.tpl'); $deprecationTemplate->setVar([ @@ -241,17 +205,16 @@ public function generateCode(): string $template->setVar( [ - 'arguments_decl' => $this->argumentsForDeclaration, - 'arguments_call' => $this->argumentsForCall, - 'return_delim' => $returnType ? ': ' : '', - 'return_type' => $this->allowsReturnNull ? '?' . $returnType : $returnType, + 'arguments_decl' => $this->argumentsForDeclaration, + 'arguments_call' => $this->argumentsForCall, + 'return_declaration' => $this->returnType->getReturnTypeDeclaration(), 'arguments_count' => !empty($this->argumentsForCall) ? \substr_count($this->argumentsForCall, ',') + 1 : 0, - 'class_name' => $this->className, - 'method_name' => $this->methodName, - 'modifier' => $this->modifier, - 'reference' => $this->reference, + 'class_name' => $this->className, + 'method_name' => $this->methodName, + 'modifier' => $this->modifier, + 'reference' => $this->reference, 'clone_arguments' => $this->cloneArguments ? 'true' : 'false', - 'deprecation' => $deprecation, + 'deprecation' => $deprecation, ] ); @@ -296,9 +259,9 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for $name = '...' . $name; } - $nullable = ''; - $default = ''; - $reference = ''; + $nullable = ''; + $default = ''; + $reference = ''; $typeDeclaration = ''; if (!$forCall) { @@ -306,7 +269,7 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for $nullable = '?'; } - if ($parameter->hasType() && (string) $parameter->getType() !== 'self') { + if ($parameter->hasType() && (string)$parameter->getType() !== 'self') { $typeDeclaration = $parameter->getType() . ' '; } else { try { @@ -336,7 +299,7 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for } catch (\ReflectionException $e) { throw new RuntimeException( $e->getMessage(), - (int) $e->getCode(), + (int)$e->getCode(), $e ); } @@ -347,13 +310,13 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for } catch (\ReflectionException $e) { throw new RuntimeException( $e->getMessage(), - (int) $e->getCode(), + (int)$e->getCode(), $e ); } } elseif (!\defined($value)) { $rootValue = \preg_replace('/^.*\\\\/', '', $value); - $value = \defined($rootValue) ? $rootValue : $value; + $value = \defined($rootValue) ? $rootValue : $value; } $default = ' = ' . $value; @@ -372,4 +335,43 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for return \implode(', ', $parameters); } + + private static function deriveReturnType(\ReflectionMethod $method): ?Type + { + $returnType = $method->getReturnType(); + + if ($returnType === null) { + return new UnknownType(); + } + + // @see https://bugs.php.net/bug.php?id=70722 + if ($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') { + $parentClass = $method->getDeclaringClass()->getParentClass(); + + if ($parentClass === false) { + throw new RuntimeException( + \sprintf( + 'Cannot mock %s::%s because "parent" return type declaration is used but %s does not have a parent class', + $method->getDeclaringClass()->getName(), + $method->getName(), + $method->getDeclaringClass()->getName() + ) + ); + } + + return ObjectType::fromName($parentClass->getName(), $returnType->allowsNull()); + } + + return Type::fromName($returnType->getName(), $returnType->allowsNull()); + } + + public function getReturnType(): Type + { + return $this->returnType; + } } diff --git a/src/Framework/MockObject/MockTrait.php b/src/Framework/MockObject/MockTrait.php new file mode 100644 index 00000000000..6634ce33787 --- /dev/null +++ b/src/Framework/MockObject/MockTrait.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Framework\MockObject; + +class MockTrait implements MockBrick +{ + + /** + * @var string + */ + private $classCode; + + /** + * @var string + */ + private $mockName; + + public function __construct(string $classCode, string $mockName) + { + $this->classCode = $classCode; + $this->mockName = $mockName; + } + + public function getClassCode(): string + { + return $this->classCode; + } + + public function getMockName(): string + { + return $this->mockName; + } + + public function bringIntoExistence(): string + { + if (!\class_exists($this->getMockName(), false)) { + eval($this->getClassCode()); + } + return $this->getMockName(); + } + +} diff --git a/src/Framework/MockObject/ObjectType.php b/src/Framework/MockObject/ObjectType.php new file mode 100644 index 00000000000..0bf40b863db --- /dev/null +++ b/src/Framework/MockObject/ObjectType.php @@ -0,0 +1,52 @@ +className = $className; + $this->allowsNull = $nullable; + } + + public function isAssignable(Type $other): bool + { + if ($this->allowsNull && isNull($other)) { + return true; + } + if ($other instanceof self) { + if ($this->className->getQualifiedName() === $other->className->getQualifiedName()) { + return true; + } + + if (is_subclass_of($other->className->getQualifiedName(), $this->className->getQualifiedName(), true)) { + return true; + } + } + return false; + } + + public function getReturnTypeDeclaration(): string + { + return ': ' . ($this->allowsNull ? '?' : '') . $this->className->getQualifiedName(); + } + + public function allowsNull(): bool + { + return $this->allowsNull; + } +} diff --git a/src/Framework/MockObject/SimpleType.php b/src/Framework/MockObject/SimpleType.php new file mode 100644 index 00000000000..5c0cecc6bde --- /dev/null +++ b/src/Framework/MockObject/SimpleType.php @@ -0,0 +1,64 @@ +name = $this->normalize($name); + $this->allowsNull = $nullable; + } + + public function isAssignable(Type $other): bool + { + if ($this->allowsNull && isNull($other)) { + return true; + } + if ($other instanceof self) { + return $this->name === $other->name; + } + return false; + } + + public function getReturnTypeDeclaration(): string + { + return ': ' . ($this->allowsNull ? '?' : '') . $this->name; + } + + public function allowsNull(): bool + { + return $this->allowsNull; + } + + private function normalize(string $name): string + { + $name = mb_strtolower($name); + switch ($name) { + case 'boolean': + return 'bool'; + case 'real': + case 'double': + return 'float'; + case 'integer': + return 'int'; + case '[]': + return 'array'; + default: + return $name; + } + } +} diff --git a/src/Framework/MockObject/Type.php b/src/Framework/MockObject/Type.php new file mode 100644 index 00000000000..398cd5c69a8 --- /dev/null +++ b/src/Framework/MockObject/Type.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + + +abstract class Type +{ + + public static function fromValue($value, bool $allowsNull): Type + { + $type = gettype($value); + switch ($type) { + case 'object': + return self::fromName(get_class($value), $allowsNull); + default: + return new SimpleType($type, $allowsNull); + } + } + + public static function fromName(?string $typeName, bool $allowsNull): Type + { + switch (mb_strtolower($typeName)) { + case null: + case 'unknown type': + return new UnknownType(); + case 'object': + case 'boolean': + case 'bool': + case 'integer': + case 'int': + case 'real': + case 'double': + case 'float': + case 'string': + case 'array': + case 'resource': + case 'resource (closed)': + return new SimpleType($typeName, $allowsNull); + default: + return new ObjectType(TypeName::fromQualifiedName($typeName), $allowsNull); + } + } + + abstract public function isAssignable(Type $other): bool; + + abstract public function getReturnTypeDeclaration(): string; + + abstract public function allowsNull(): bool; + +} diff --git a/src/Framework/MockObject/TypeName.php b/src/Framework/MockObject/TypeName.php new file mode 100644 index 00000000000..b5377d923f6 --- /dev/null +++ b/src/Framework/MockObject/TypeName.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class TypeName +{ + /** + * @var ?string + */ + private $namespaceName; + + /** + * @var string + */ + private $simpleName; + + public static function fromQualifiedName(string $fullClassName): self + { + if ($fullClassName[0] === '\\') { + $fullClassName = \substr($fullClassName, 1); + } + + $classNameParts = \explode('\\', $fullClassName); + + $simpleName = \array_pop($classNameParts); + $namespaceName = \implode('\\', $classNameParts); + + return new self($namespaceName, $simpleName); + } + + public static function fromReflection(\ReflectionClass $type): self + { + return new self( + $type->getNamespaceName(), + $type->getShortName() + ); + } + + public function __construct(?string $namespaceName, string $simpleName) + { + if ($namespaceName === '') { + $namespaceName = null; + } + $this->namespaceName = $namespaceName; + $this->simpleName = $simpleName; + } + + public function getNamespaceName(): ?string + { + return $this->namespaceName; + } + + public function getSimpleName(): string + { + return $this->simpleName; + } + + public function getQualifiedName(): string + { + return $this->isNamespaced() + ? $this->namespaceName . '\\' . $this->simpleName + : $this->simpleName; + } + + public function isNamespaced(): bool + { + return $this->namespaceName !== null; + } +} diff --git a/src/Framework/MockObject/UnknownType.php b/src/Framework/MockObject/UnknownType.php new file mode 100644 index 00000000000..108dcf7eed2 --- /dev/null +++ b/src/Framework/MockObject/UnknownType.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +class ChildClass extends ParentClass +{ +} diff --git a/tests/_files/mock-object/ClassAUsingConfigurableMethods.php b/tests/_files/mock-object/ClassAUsingConfigurableMethods.php new file mode 100644 index 00000000000..2adf0ab144d --- /dev/null +++ b/tests/_files/mock-object/ClassAUsingConfigurableMethods.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Framework\MockObject; + +class ClassAUsingConfigurableMethods { + use ConfigurableMethods; + + public static function getConfigurableMethods() { + return static::$__phpunit_configurableMethods; + } +} diff --git a/tests/_files/mock-object/ClassBUsingConfigurableMethods.php b/tests/_files/mock-object/ClassBUsingConfigurableMethods.php new file mode 100644 index 00000000000..a9a4b1ad6bc --- /dev/null +++ b/tests/_files/mock-object/ClassBUsingConfigurableMethods.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Framework\MockObject; + +class ClassBUsingConfigurableMethods +{ + use ConfigurableMethods; + + public static function getConfigurableMethods() + { + return static::$__phpunit_configurableMethods; + } + +} diff --git a/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php b/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php new file mode 100644 index 00000000000..bc1b7426648 --- /dev/null +++ b/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PHPUnit\Framework\MockObject; + +class ClassWithoutParentButParentReturnType { + + + public function foo(): parent + { + } + +} diff --git a/tests/_files/mock-object/ParentClass.php b/tests/_files/mock-object/ParentClass.php new file mode 100644 index 00000000000..62a4f9bd129 --- /dev/null +++ b/tests/_files/mock-object/ParentClass.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +class ParentClass +{ + public function foo(): void + { + } +} diff --git a/tests/_files/namespace/someNamespaceA/NamespacedClass.php b/tests/_files/namespace/someNamespaceA/NamespacedClass.php new file mode 100644 index 00000000000..889fdd7592f --- /dev/null +++ b/tests/_files/namespace/someNamespaceA/NamespacedClass.php @@ -0,0 +1,7 @@ +generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['speak']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -113,7 +116,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/3154_namespaced_constant_resolving.phpt b/tests/end-to-end/mock-objects/generator/3154_namespaced_constant_resolving.phpt index be9f2a4cf18..e7af58896be 100644 --- a/tests/end-to-end/mock-objects/generator/3154_namespaced_constant_resolving.phpt +++ b/tests/end-to-end/mock-objects/generator/3154_namespaced_constant_resolving.phpt @@ -35,13 +35,16 @@ $mock = $generator->generate( true ); -print $mock['code']; ---EXPECT-- +print $mock->getClassCode(); +--EXPECTF-- +declare(strict_types=1); + class Issue3154Mock extends Is\Namespaced\Issue3154 implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['a']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -64,7 +67,7 @@ class Issue3154Mock extends Is\Namespaced\Issue3154 implements PHPUnit\Framework $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Is\Namespaced\Issue3154', 'a', $__phpunit_arguments, 'string', $this, true + 'Is\Namespaced\Issue3154', 'a', $__phpunit_arguments, ': string', $this, true ) ); @@ -97,7 +100,7 @@ class Issue3154Mock extends Is\Namespaced\Issue3154 implements PHPUnit\Framework public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/3530.phpt b/tests/end-to-end/mock-objects/generator/3530.phpt index 588a654d7c3..c279aba77c0 100644 --- a/tests/end-to-end/mock-objects/generator/3530.phpt +++ b/tests/end-to-end/mock-objects/generator/3530.phpt @@ -14,6 +14,8 @@ print $generator->generateClassFromWsdl( 'Test' ); --EXPECTF-- +declare(strict_types=1); + class Test extends \SoapClient { public function __construct($wsdl, array $options) diff --git a/tests/end-to-end/mock-objects/generator/397.phpt b/tests/end-to-end/mock-objects/generator/397.phpt index 39dc64507de..badcda28f3f 100644 --- a/tests/end-to-end/mock-objects/generator/397.phpt +++ b/tests/end-to-end/mock-objects/generator/397.phpt @@ -21,13 +21,16 @@ $mock = $generator->generate( true ); -print $mock['code']; ---EXPECT-- +print $mock->getClassCode(); +--EXPECTF-- +declare(strict_types=1); + class MockC extends C implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['m']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -50,7 +53,7 @@ class MockC extends C implements PHPUnit\Framework\MockObject\MockObject $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'C', 'm', $__phpunit_arguments, 'C', $this, true + 'C', 'm', $__phpunit_arguments, ': C', $this, true ) ); @@ -83,7 +86,7 @@ class MockC extends C implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/abstract_class.phpt b/tests/end-to-end/mock-objects/generator/abstract_class.phpt index 727e4cb026e..083949d9a12 100644 --- a/tests/end-to-end/mock-objects/generator/abstract_class.phpt +++ b/tests/end-to-end/mock-objects/generator/abstract_class.phpt @@ -25,14 +25,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['one', 'two', 'three']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -132,7 +135,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class.phpt b/tests/end-to-end/mock-objects/generator/class.phpt index b013bbcb6fc..f290198331f 100644 --- a/tests/end-to-end/mock-objects/generator/class.phpt +++ b/tests/end-to-end/mock-objects/generator/class.phpt @@ -25,14 +25,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar', 'baz']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -110,7 +113,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_call_parent_clone.phpt b/tests/end-to-end/mock-objects/generator/class_call_parent_clone.phpt index 1dbd21eb36a..99f6a86d2d2 100644 --- a/tests/end-to-end/mock-objects/generator/class_call_parent_clone.phpt +++ b/tests/end-to-end/mock-objects/generator/class_call_parent_clone.phpt @@ -20,14 +20,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -62,7 +65,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/class_call_parent_constructor.phpt index 8f0d647e585..1b17186e0d1 100644 --- a/tests/end-to-end/mock-objects/generator/class_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/class_call_parent_constructor.phpt @@ -20,14 +20,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -61,7 +64,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_dont_call_parent_clone.phpt b/tests/end-to-end/mock-objects/generator/class_dont_call_parent_clone.phpt index 51e95b51324..8c543a604f3 100644 --- a/tests/end-to-end/mock-objects/generator/class_dont_call_parent_clone.phpt +++ b/tests/end-to-end/mock-objects/generator/class_dont_call_parent_clone.phpt @@ -20,14 +20,17 @@ $mock = $generator->generate( false ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -61,7 +64,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_dont_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/class_dont_call_parent_constructor.phpt index 8f0d647e585..1b17186e0d1 100644 --- a/tests/end-to-end/mock-objects/generator/class_dont_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/class_dont_call_parent_constructor.phpt @@ -20,14 +20,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -61,7 +64,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_implementing_interface_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/class_implementing_interface_call_parent_constructor.phpt index 8aae9076e9c..91ad5323f67 100644 --- a/tests/end-to-end/mock-objects/generator/class_implementing_interface_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/class_implementing_interface_call_parent_constructor.phpt @@ -25,14 +25,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -66,7 +69,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_implementing_interface_dont_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/class_implementing_interface_dont_call_parent_constructor.phpt index 8aae9076e9c..91ad5323f67 100644 --- a/tests/end-to-end/mock-objects/generator/class_implementing_interface_dont_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/class_implementing_interface_dont_call_parent_constructor.phpt @@ -25,14 +25,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -66,7 +69,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_nonexistent_method.phpt b/tests/end-to-end/mock-objects/generator/class_nonexistent_method.phpt index ae6ad6068c6..9a8a709d4e6 100644 --- a/tests/end-to-end/mock-objects/generator/class_nonexistent_method.phpt +++ b/tests/end-to-end/mock-objects/generator/class_nonexistent_method.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_partial.phpt b/tests/end-to-end/mock-objects/generator/class_partial.phpt index 5b139da871f..b7279c554fb 100644 --- a/tests/end-to-end/mock-objects/generator/class_partial.phpt +++ b/tests/end-to-end/mock-objects/generator/class_partial.phpt @@ -25,14 +25,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -88,7 +91,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_with_deprecated_method.phpt b/tests/end-to-end/mock-objects/generator/class_with_deprecated_method.phpt index b0a5401fcea..982c98139c7 100644 --- a/tests/end-to-end/mock-objects/generator/class_with_deprecated_method.phpt +++ b/tests/end-to-end/mock-objects/generator/class_with_deprecated_method.phpt @@ -25,14 +25,17 @@ $mock = $generator->generate( TRUE ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends ClassWithDeprecatedMethod implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['deprecatedmethod']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -90,7 +93,7 @@ class MockFoo extends ClassWithDeprecatedMethod implements PHPUnit\Framework\Moc public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_with_final_method.phpt b/tests/end-to-end/mock-objects/generator/class_with_final_method.phpt index f382a37a188..76c5aff252d 100644 --- a/tests/end-to-end/mock-objects/generator/class_with_final_method.phpt +++ b/tests/end-to-end/mock-objects/generator/class_with_final_method.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends ClassWithFinalMethod implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -62,7 +65,7 @@ class MockFoo extends ClassWithFinalMethod implements PHPUnit\Framework\MockObje public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_with_method_named_method.phpt b/tests/end-to-end/mock-objects/generator/class_with_method_named_method.phpt index d06e709ff62..257f9df769c 100644 --- a/tests/end-to-end/mock-objects/generator/class_with_method_named_method.phpt +++ b/tests/end-to-end/mock-objects/generator/class_with_method_named_method.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['method']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -76,7 +79,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_with_method_with_nullable_typehinted_variadic_arguments.phpt b/tests/end-to-end/mock-objects/generator/class_with_method_with_nullable_typehinted_variadic_arguments.phpt index 44e0ecbb11a..38f116187fd 100644 --- a/tests/end-to-end/mock-objects/generator/class_with_method_with_nullable_typehinted_variadic_arguments.phpt +++ b/tests/end-to-end/mock-objects/generator/class_with_method_with_nullable_typehinted_variadic_arguments.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends ClassWithMethodWithNullableTypehintedVariadicArguments implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['methodwithnullabletypehintedvariadicarguments']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends ClassWithMethodWithNullableTypehintedVariadicArguments imp public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_with_method_with_typehinted_variadic_arguments.phpt b/tests/end-to-end/mock-objects/generator/class_with_method_with_typehinted_variadic_arguments.phpt index 246b9cbee6a..30badf4923f 100644 --- a/tests/end-to-end/mock-objects/generator/class_with_method_with_typehinted_variadic_arguments.phpt +++ b/tests/end-to-end/mock-objects/generator/class_with_method_with_typehinted_variadic_arguments.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends ClassWithMethodWithTypehintedVariadicArguments implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['methodwithtypehintedvariadicarguments']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends ClassWithMethodWithTypehintedVariadicArguments implements public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/class_with_method_with_variadic_arguments.phpt b/tests/end-to-end/mock-objects/generator/class_with_method_with_variadic_arguments.phpt index 282294ed1df..2137e8560cc 100644 --- a/tests/end-to-end/mock-objects/generator/class_with_method_with_variadic_arguments.phpt +++ b/tests/end-to-end/mock-objects/generator/class_with_method_with_variadic_arguments.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends ClassWithMethodWithVariadicArguments implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['methodwithvariadicarguments']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends ClassWithMethodWithVariadicArguments implements PHPUnit\Fr public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/constant_as_parameter_default_value.phpt b/tests/end-to-end/mock-objects/generator/constant_as_parameter_default_value.phpt index 004b17979c0..b4a995ac88b 100644 --- a/tests/end-to-end/mock-objects/generator/constant_as_parameter_default_value.phpt +++ b/tests/end-to-end/mock-objects/generator/constant_as_parameter_default_value.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/interface.phpt b/tests/end-to-end/mock-objects/generator/interface.phpt index c7f0e682fe2..6f6aa79ba55 100644 --- a/tests/end-to-end/mock-objects/generator/interface.phpt +++ b/tests/end-to-end/mock-objects/generator/interface.phpt @@ -19,14 +19,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -82,7 +85,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/invocation_object_clone_object.phpt b/tests/end-to-end/mock-objects/generator/invocation_object_clone_object.phpt index 6eedbcade62..aae1d0c6fa8 100644 --- a/tests/end-to-end/mock-objects/generator/invocation_object_clone_object.phpt +++ b/tests/end-to-end/mock-objects/generator/invocation_object_clone_object.phpt @@ -26,14 +26,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar', 'baz']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -111,7 +114,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class.phpt index b8cf0f4f576..9ca854c0ffb 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class.phpt @@ -27,14 +27,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar', 'baz']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -112,7 +115,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_clone.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_clone.phpt index 21f886e3df5..38c87d8435e 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_clone.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_clone.phpt @@ -22,14 +22,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -64,7 +67,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_constructor.phpt index c725628a4ca..11269c65fbc 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_call_parent_constructor.phpt @@ -22,14 +22,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -63,7 +66,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_clone.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_clone.phpt index 1dc6d430bca..5d5ac232a77 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_clone.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_clone.phpt @@ -22,14 +22,17 @@ $mock = $generator->generate( false ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -63,7 +66,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_constructor.phpt index c725628a4ca..11269c65fbc 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_dont_call_parent_constructor.phpt @@ -22,14 +22,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -63,7 +66,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_call_parent_constructor.phpt index 6ddfe6ee38f..e78216d841a 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_call_parent_constructor.phpt @@ -27,14 +27,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -68,7 +71,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_dont_call_parent_constructor.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_dont_call_parent_constructor.phpt index 6ddfe6ee38f..e78216d841a 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_dont_call_parent_constructor.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_implementing_interface_dont_call_parent_constructor.phpt @@ -27,14 +27,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -68,7 +71,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_class_partial.phpt b/tests/end-to-end/mock-objects/generator/namespaced_class_partial.phpt index 6610782d2b3..0a468e1deed 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_class_partial.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_class_partial.phpt @@ -27,14 +27,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -90,7 +93,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/namespaced_interface.phpt b/tests/end-to-end/mock-objects/generator/namespaced_interface.phpt index 25f7c24dc0d..2a117896053 100644 --- a/tests/end-to-end/mock-objects/generator/namespaced_interface.phpt +++ b/tests/end-to-end/mock-objects/generator/namespaced_interface.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, NS\Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, NS\Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/nonexistent_class.phpt b/tests/end-to-end/mock-objects/generator/nonexistent_class.phpt index 90c5747ebf0..5b7d306bdbe 100644 --- a/tests/end-to-end/mock-objects/generator/nonexistent_class.phpt +++ b/tests/end-to-end/mock-objects/generator/nonexistent_class.phpt @@ -14,18 +14,21 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class NonExistentClass { } class MockFoo extends NonExistentClass implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -59,7 +62,7 @@ class MockFoo extends NonExistentClass implements PHPUnit\Framework\MockObject\M public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace.phpt b/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace.phpt index 8767b086f62..bdcda1b5c9d 100644 --- a/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace.phpt +++ b/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace.phpt @@ -14,9 +14,11 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + namespace NS { class Foo @@ -29,9 +31,10 @@ namespace { class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -65,7 +68,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace_starting_with_separator.phpt b/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace_starting_with_separator.phpt index 053a443f372..b1e900060d5 100644 --- a/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace_starting_with_separator.phpt +++ b/tests/end-to-end/mock-objects/generator/nonexistent_class_with_namespace_starting_with_separator.phpt @@ -14,9 +14,11 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + namespace NS { class Foo @@ -29,9 +31,10 @@ namespace { class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = []; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -65,7 +68,7 @@ class MockFoo extends NS\Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/nullable_types.phpt b/tests/end-to-end/mock-objects/generator/nullable_types.phpt index e056427c7fa..fab59814f32 100644 --- a/tests/end-to-end/mock-objects/generator/nullable_types.phpt +++ b/tests/end-to-end/mock-objects/generator/nullable_types.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/proxy.phpt b/tests/end-to-end/mock-objects/generator/proxy.phpt index dba9e413693..fcd74553cb8 100644 --- a/tests/end-to-end/mock-objects/generator/proxy.phpt +++ b/tests/end-to-end/mock-objects/generator/proxy.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( 'Foo', [], 'ProxyFoo', true, true, true, true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class ProxyFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar', 'baz']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -106,7 +109,7 @@ class ProxyFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_closure.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_closure.phpt index e39913ceec8..667b581c37a 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_closure.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_closure.phpt @@ -19,14 +19,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -49,7 +52,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'Closure', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': Closure', $this, true ) ); @@ -82,7 +85,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_final.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_final.phpt index 36fbcdc1cf9..98beefe062d 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_final.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_final.phpt @@ -26,14 +26,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -56,7 +59,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'FinalClass', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': FinalClass', $this, true ) ); @@ -89,7 +92,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_generator.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_generator.phpt index c36f7cda802..2f4a6c55def 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_generator.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_generator.phpt @@ -19,14 +19,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -49,7 +52,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'Generator', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': Generator', $this, true ) ); @@ -82,7 +85,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_nullable.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_nullable.phpt index f3afc25799f..beb408143ef 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_nullable.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_nullable.phpt @@ -19,14 +19,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -49,7 +52,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, '?string', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': ?string', $this, true ) ); @@ -82,7 +85,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_object_method.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_object_method.phpt index 5bfe3e2b93a..fe002b51f5a 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_object_method.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_object_method.phpt @@ -22,14 +22,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -52,7 +55,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'Bar', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': Bar', $this, true ) ); @@ -85,7 +88,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_parent.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_parent.phpt index d3d881e3647..c1c25ffcca4 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_parent.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_parent.phpt @@ -26,13 +26,16 @@ $mock = $generator->generate( true ); -print $mock['code']; ---EXPECT-- +print $mock->getClassCode(); +--EXPECTF-- +declare(strict_types=1); + class MockBar extends Bar implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['baz']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -55,7 +58,7 @@ class MockBar extends Bar implements PHPUnit\Framework\MockObject\MockObject $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Bar', 'baz', $__phpunit_arguments, 'Foo', $this, true + 'Bar', 'baz', $__phpunit_arguments, ': Foo', $this, true ) ); @@ -88,7 +91,7 @@ class MockBar extends Bar implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_self.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_self.phpt index 12c4742bfe4..f2a23837dab 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_self.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_self.phpt @@ -19,14 +19,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -49,7 +52,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'Foo', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': Foo', $this, true ) ); @@ -82,7 +85,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_static_method.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_static_method.phpt index c8aa3d15743..50c194d0139 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_static_method.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_static_method.phpt @@ -22,14 +22,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -68,7 +71,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/return_type_declarations_void.phpt b/tests/end-to-end/mock-objects/generator/return_type_declarations_void.phpt index b452155544b..4f169d68c62 100644 --- a/tests/end-to-end/mock-objects/generator/return_type_declarations_void.phpt +++ b/tests/end-to-end/mock-objects/generator/return_type_declarations_void.phpt @@ -19,14 +19,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -49,7 +52,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'void', $this, true + 'Foo', 'bar', $__phpunit_arguments, ': void', $this, true ) ); } @@ -80,7 +83,7 @@ class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/scalar_type_declarations.phpt b/tests/end-to-end/mock-objects/generator/scalar_type_declarations.phpt index 6ab06c0f668..a7b8855203b 100644 --- a/tests/end-to-end/mock-objects/generator/scalar_type_declarations.phpt +++ b/tests/end-to-end/mock-objects/generator/scalar_type_declarations.phpt @@ -21,14 +21,17 @@ $mock = $generator->generate( true ); -print $mock['code']; +print $mock->getClassCode(); ?> ---EXPECT-- +--EXPECTF-- +declare(strict_types=1); + class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject { + use \PHPUnit\Framework\MockObject\ConfigurableMethods; + private $__phpunit_invocationMocker; private $__phpunit_originalObject; - private $__phpunit_configurable = ['bar']; private $__phpunit_returnValueGeneration = true; public function __clone() @@ -84,7 +87,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject public function __phpunit_getInvocationMocker(): \PHPUnit\Framework\MockObject\InvocationMocker { if ($this->__phpunit_invocationMocker === null) { - $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable, $this->__phpunit_returnValueGeneration); + $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker(static::$__phpunit_configurableMethods, $this->__phpunit_returnValueGeneration); } return $this->__phpunit_invocationMocker; diff --git a/tests/end-to-end/mock-objects/generator/wsdl_class.phpt b/tests/end-to-end/mock-objects/generator/wsdl_class.phpt index c3166a84c07..566a1976500 100644 --- a/tests/end-to-end/mock-objects/generator/wsdl_class.phpt +++ b/tests/end-to-end/mock-objects/generator/wsdl_class.phpt @@ -16,6 +16,8 @@ print $generator->generateClassFromWsdl( ); ?> --EXPECTF-- +declare(strict_types=1); + class GoogleSearch extends \SoapClient { public function __construct($wsdl, array $options) diff --git a/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt b/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt index 919cd5df7a1..89a2cf383e8 100644 --- a/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt +++ b/tests/end-to-end/mock-objects/generator/wsdl_class_namespace.phpt @@ -15,6 +15,8 @@ print $generator->generateClassFromWsdl( ); ?> --EXPECTF-- +declare(strict_types=1); + namespace My\Space; class GoogleSearch extends \SoapClient diff --git a/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt b/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt index 335296f8a58..59b31be9c2d 100644 --- a/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt +++ b/tests/end-to-end/mock-objects/generator/wsdl_class_partial.phpt @@ -17,6 +17,8 @@ print $generator->generateClassFromWsdl( ); ?> --EXPECTF-- +declare(strict_types=1); + class GoogleSearch extends \SoapClient { public function __construct($wsdl, array $options) diff --git a/tests/end-to-end/mock-objects/mock-method/call_original_with_return_type_void.phpt b/tests/end-to-end/mock-objects/mock-method/call_original_with_return_type_void.phpt index 142d6335cdb..f6e71c19675 100644 --- a/tests/end-to-end/mock-objects/mock-method/call_original_with_return_type_void.phpt +++ b/tests/end-to-end/mock-objects/mock-method/call_original_with_return_type_void.phpt @@ -20,7 +20,7 @@ $code = $mockMethod->generateCode(); print $code; ?> ---EXPECT-- +--EXPECTF-- public function bar(): void { @@ -37,7 +37,7 @@ print $code; $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'void', $this, false, true + 'Foo', 'bar', $__phpunit_arguments, ': void', $this, false, true ) ); diff --git a/tests/end-to-end/mock-objects/mock-method/return_by_reference_with_return_type.phpt b/tests/end-to-end/mock-objects/mock-method/return_by_reference_with_return_type.phpt index 327b0b12253..cb027a170d6 100644 --- a/tests/end-to-end/mock-objects/mock-method/return_by_reference_with_return_type.phpt +++ b/tests/end-to-end/mock-objects/mock-method/return_by_reference_with_return_type.phpt @@ -20,7 +20,7 @@ $code = $mockMethod->generateCode(); print $code; ?> ---EXPECT-- +--EXPECTF-- public function &bar(): string { @@ -37,7 +37,7 @@ public function &bar(): string $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'string', $this, false + 'Foo', 'bar', $__phpunit_arguments, ': string', $this, false ) ); diff --git a/tests/end-to-end/mock-objects/mock-method/return_type.phpt b/tests/end-to-end/mock-objects/mock-method/return_type.phpt index 63e4b730cb7..b9547a0c916 100644 --- a/tests/end-to-end/mock-objects/mock-method/return_type.phpt +++ b/tests/end-to-end/mock-objects/mock-method/return_type.phpt @@ -20,7 +20,7 @@ $code = $mockMethod->generateCode(); print $code; ?> ---EXPECT-- +--EXPECTF-- public function bar(): string { @@ -37,7 +37,7 @@ public function bar(): string $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'string', $this, false + 'Foo', 'bar', $__phpunit_arguments, ': string', $this, false ) ); diff --git a/tests/end-to-end/mock-objects/mock-method/return_type_parent.phpt b/tests/end-to-end/mock-objects/mock-method/return_type_parent.phpt index e6ab4920a90..3cc7b5b0282 100644 --- a/tests/end-to-end/mock-objects/mock-method/return_type_parent.phpt +++ b/tests/end-to-end/mock-objects/mock-method/return_type_parent.phpt @@ -24,7 +24,7 @@ $code = $mockMethod->generateCode(); print $code; ?> ---EXPECT-- +--EXPECTF-- public function bar(): Baz { @@ -41,7 +41,7 @@ public function bar(): Baz $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'Baz', $this, false + 'Foo', 'bar', $__phpunit_arguments, ': Baz', $this, false ) ); diff --git a/tests/end-to-end/mock-objects/mock-method/return_type_self.phpt b/tests/end-to-end/mock-objects/mock-method/return_type_self.phpt index 4cf21b85d06..e3d5ee6733c 100644 --- a/tests/end-to-end/mock-objects/mock-method/return_type_self.phpt +++ b/tests/end-to-end/mock-objects/mock-method/return_type_self.phpt @@ -20,7 +20,7 @@ $code = $mockMethod->generateCode(); print $code; ?> ---EXPECT-- +--EXPECTF-- public function bar(): Foo { @@ -37,7 +37,7 @@ public function bar(): Foo $__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke( new \PHPUnit\Framework\MockObject\Invocation( - 'Foo', 'bar', $__phpunit_arguments, 'Foo', $this, false + 'Foo', 'bar', $__phpunit_arguments, ': Foo', $this, false ) ); diff --git a/tests/end-to-end/regression/GitHub/2366/Issue2366Test.php b/tests/end-to-end/regression/GitHub/2366/Issue2366Test.php index 057df1bfbe9..303053c3fd2 100644 --- a/tests/end-to-end/regression/GitHub/2366/Issue2366Test.php +++ b/tests/end-to-end/regression/GitHub/2366/Issue2366Test.php @@ -24,14 +24,14 @@ class Issue2366Test extends TestCase */ public function testOne($o): void { - $this->assertEquals(1, $o->foo()); + $this->assertEquals(true, $o->foo()); } public function provider() { $o = $this->createMock(Issue2366::class); - $o->method('foo')->willReturn(1); + $o->method('foo')->willReturn(true); return [ [$o], diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index 36a3b63512e..9c2d4dc57f5 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -1,4 +1,5 @@ getMockBuilder(stdClass::class) - ->setMethods(['foo']) - ->getMock(); + ->setMethods(['foo']) + ->getMock(); $mock->expects($this->any()) - ->method('foo') - ->willReturn(1); + ->method('foo') + ->willReturn(1); $this->assertEquals(1, $mock->foo()); } @@ -32,12 +34,12 @@ public function testWillReturnWithOneValue(): void public function testWillReturnWithMultipleValues(): void { $mock = $this->getMockBuilder(stdClass::class) - ->setMethods(['foo']) - ->getMock(); + ->setMethods(['foo']) + ->getMock(); $mock->expects($this->any()) - ->method('foo') - ->willReturn(1, 2, 3); + ->method('foo') + ->willReturn(1, 2, 3); $this->assertEquals(1, $mock->foo()); $this->assertEquals(2, $mock->foo()); @@ -47,12 +49,12 @@ public function testWillReturnWithMultipleValues(): void public function testWillReturnOnConsecutiveCalls(): void { $mock = $this->getMockBuilder(stdClass::class) - ->setMethods(['foo']) - ->getMock(); + ->setMethods(['foo']) + ->getMock(); $mock->expects($this->any()) - ->method('foo') - ->willReturnOnConsecutiveCalls(1, 2, 3); + ->method('foo') + ->willReturnOnConsecutiveCalls(1, 2, 3); $this->assertEquals(1, $mock->foo()); $this->assertEquals(2, $mock->foo()); @@ -62,12 +64,12 @@ public function testWillReturnOnConsecutiveCalls(): void public function testWillReturnByReference(): void { $mock = $this->getMockBuilder(stdClass::class) - ->setMethods(['foo']) - ->getMock(); + ->setMethods(['foo']) + ->getMock(); $mock->expects($this->any()) - ->method('foo') - ->willReturnReference($value); + ->method('foo') + ->willReturnReference($value); $this->assertNull($mock->foo()); $value = 'foo'; @@ -80,13 +82,49 @@ public function testWillFailWhenTryingToPerformExpectationUnconfigurableMethod() { /** @var MatcherCollection|\PHPUnit\Framework\MockObject\MockObject $matcherCollection */ $matcherCollection = $this->createMock(MatcherCollection::class); - $invocationMocker = new \PHPUnit\Framework\MockObject\Builder\InvocationMocker( + $invocationMocker = new \PHPUnit\Framework\MockObject\Builder\InvocationMocker( $matcherCollection, - $this->any(), - [] + $this->any() ); $this->expectException(RuntimeException::class); $invocationMocker->method('someMethod'); } + + public function testWillReturnFailsWhenTryingToReturnSingleIncompatibleValue(): void + { + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $invocationMocker = $mock->method('methodWithBoolReturnTypeDeclaration'); + + $this->expectException(IncompatibleReturnValueException::class); + $this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer'); + $invocationMocker->willReturn(1); + } + + public function testWillReturnFailsWhenTryingToReturnIncompatibleValueByConstraint(): void + { + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $invocationMocker = $mock->method(new \PHPUnit\Framework\Constraint\IsEqual('methodWithBoolReturnTypeDeclaration')); + + $this->expectException(IncompatibleReturnValueException::class); + $this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer'); + $invocationMocker->willReturn(1); + } + + public function testWillReturnFailsWhenTryingToReturnAtLeastOneIncompatibleValue(): void + { + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $invocationMocker = $mock->method('methodWithBoolReturnTypeDeclaration'); + + $this->expectException(IncompatibleReturnValueException::class); + $this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer'); + $invocationMocker->willReturn(true, 1); + } + } diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php new file mode 100644 index 00000000000..dcfc4e63fb0 --- /dev/null +++ b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php @@ -0,0 +1,28 @@ +createMock(Type::class); + $assignableType->method('isAssignable') + ->willReturn(true); + $configurable = new ConfigurableMethod('foo', $assignableType); + $this->assertTrue($configurable->mayReturn('everything-is-valid')); + } + + public function testMethodMayNotReturnUnassignableValue() + { + $unassignableType = $this->createMock(Type::class); + $unassignableType->method('isAssignable') + ->willReturn(false); + $configurable = new ConfigurableMethod('foo', $unassignableType); + $this->assertFalse($configurable->mayReturn('everything-is-invalid')); + } + +} diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php new file mode 100644 index 00000000000..efe82196a0e --- /dev/null +++ b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php @@ -0,0 +1,20 @@ +assertSame($configurableMethodsA, ClassAUsingConfigurableMethods::getConfigurableMethods()); + $this->assertSame($configurableMethodsB, ClassBUsingConfigurableMethods::getConfigurableMethods()); + } + +} diff --git a/tests/unit/Framework/MockObject/MockMethodTest.php b/tests/unit/Framework/MockObject/MockMethodTest.php index 51ee79c2d19..73661a786bf 100644 --- a/tests/unit/Framework/MockObject/MockMethodTest.php +++ b/tests/unit/Framework/MockObject/MockMethodTest.php @@ -25,7 +25,7 @@ public function testGetNameReturnsMethodName(): void '', '', '', - '', + new UnknownType(), '', false, false, @@ -37,21 +37,9 @@ public function testGetNameReturnsMethodName(): void public function testFailWhenReturnTypeIsParentButThereIsNoParentClass(): void { - $method = new MockMethod( - \stdClass::class, - 'methodName', - false, - '', - '', - '', - 'parent', - '', - false, - false, - null, - false - ); + $class = new \ReflectionClass(ClassWithoutParentButParentReturnType::class); + $this->expectException(\RuntimeException::class); - $method->generateCode(); + MockMethod::fromReflection($class->getMethod('foo'), false, false); } } diff --git a/tests/unit/Framework/MockObject/ObjectTypeTest.php b/tests/unit/Framework/MockObject/ObjectTypeTest.php new file mode 100644 index 00000000000..01e960f8bf7 --- /dev/null +++ b/tests/unit/Framework/MockObject/ObjectTypeTest.php @@ -0,0 +1,84 @@ +childClass = new ObjectType( + TypeName::fromQualifiedName(ChildClass::class), + false + ); + $this->parentClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + false + ); + } + + public function testParentIsNotAssignableToChild(): void + { + $this->assertFalse($this->childClass->isAssignable($this->parentClass)); + } + + public function testChildIsAssignableToParent(): void + { + $this->assertTrue($this->parentClass->isAssignable($this->childClass)); + } + + public function testClassIsAssignableToSelf(): void + { + $this->assertTrue($this->parentClass->isAssignable($this->parentClass)); + } + + public function testSimpleTypeIsNotAssignableToClass(): void + { + $this->assertFalse($this->parentClass->isAssignable(new SimpleType('int', false))); + } + + public function testClassFromOneNamespaceIsNotAssignableToClassInOtherNamespace() + { + $classFromNamespaceA = new ObjectType( + TypeName::fromQualifiedName(\someNamespaceA\NamespacedClass::class), + false + ); + $classFromNamespaceB = new ObjectType( + TypeName::fromQualifiedName(\someNamespaceB\NamespacedClass::class), + false + ); + $this->assertFalse($classFromNamespaceA->isAssignable($classFromNamespaceB)); + } + + public function testNullIsAssignableToNullableType() + { + $someClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + true + ); + $this->assertTrue($someClass->isAssignable(Type::fromValue(null, true))); + } + + public function testNullIsNotAssignableToNotNullableType() + { + $someClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + false + ); + $this->assertFalse($someClass->isAssignable(Type::fromValue(null, true))); + } + + +} diff --git a/tests/unit/Framework/MockObject/TypeNameTest.php b/tests/unit/Framework/MockObject/TypeNameTest.php new file mode 100644 index 00000000000..d9df038f0de --- /dev/null +++ b/tests/unit/Framework/MockObject/TypeNameTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +class TypeNameTest extends TestCase +{ + public function testFromReflection(): void + { + $class = new \ReflectionClass(TypeName::class); + $typeName = TypeName::fromReflection($class); + + $this->assertTrue($typeName->isNamespaced()); + $this->assertEquals('PHPUnit\\Framework\\MockObject', $typeName->getNamespaceName()); + $this->assertEquals(TypeName::class, $typeName->getQualifiedName()); + $this->assertEquals('TypeName', $typeName->getSimpleName()); + } + + public function testFromQualifiedName(): void + { + $typeName = TypeName::fromQualifiedName('PHPUnit\\Framework\\MockObject\\TypeName'); + + $this->assertTrue($typeName->isNamespaced()); + $this->assertEquals('PHPUnit\\Framework\\MockObject', $typeName->getNamespaceName()); + $this->assertEquals('PHPUnit\\Framework\\MockObject\\TypeName', $typeName->getQualifiedName()); + $this->assertEquals('TypeName', $typeName->getSimpleName()); + } + + public function testFromQualifiedNameWithLeadingSeparator(): void + { + $typeName = TypeName::fromQualifiedName('\\Foo\\Bar'); + + $this->assertTrue($typeName->isNamespaced()); + $this->assertEquals('Foo', $typeName->getNamespaceName()); + $this->assertEquals('Foo\\Bar', $typeName->getQualifiedName()); + $this->assertEquals('Bar', $typeName->getSimpleName()); + } + + public function testFromQualifiedNameWithoutNamespace(): void + { + $typeName = TypeName::fromQualifiedName('Bar'); + + $this->assertFalse($typeName->isNamespaced()); + $this->assertNull($typeName->getNamespaceName()); + $this->assertEquals('Bar', $typeName->getQualifiedName()); + $this->assertEquals('Bar', $typeName->getSimpleName()); + } +} From 0970f735d4c1201f92cebbdf25c3e8493856f625 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Sun, 28 Apr 2019 22:06:08 +0200 Subject: [PATCH 02/11] CS-fixed, naming and some more tests --- .../MockObject/Builder/InvocationMocker.php | 18 +++-- .../MockObject/ConfigurableMethod.php | 21 ++++-- .../MockObject/ConfigurableMethods.php | 7 +- src/Framework/MockObject/Generator.php | 6 +- src/Framework/MockObject/Invocation.php | 2 +- .../MockObject/Matcher/MethodName.php | 3 +- src/Framework/MockObject/MockBrick.php | 8 --- src/Framework/MockObject/MockClass.php | 30 ++++----- src/Framework/MockObject/MockMethod.php | 67 +++++++++---------- src/Framework/MockObject/MockTrait.php | 26 +++---- src/Framework/MockObject/MockType.php | 18 +++++ src/Framework/MockObject/ObjectType.php | 23 +++++-- src/Framework/MockObject/SimpleType.php | 24 +++++-- src/Framework/MockObject/Type.php | 19 +++--- src/Framework/MockObject/UnknownType.php | 16 +++-- tests/_files/mock-object/ChildClass.php | 2 +- .../ClassAUsingConfigurableMethods.php | 9 ++- .../ClassBUsingConfigurableMethods.php | 6 +- .../ClassWithoutParentButParentReturnType.php | 9 +-- .../mock-object/MockClassGenerated.tpl.dist | 13 ++++ .../MockClassWithConfigurableMethods.php | 22 ++++++ .../mock-object/MockTraitGenerated.tpl.dist | 5 ++ tests/_files/mock-object/ParentClass.php | 2 +- .../someNamespaceA/NamespacedClass.php | 15 +++-- .../someNamespaceB/NamespacedClass.php | 15 +++-- .../Builder/InvocationMockerTest.php | 3 +- .../MockObject/ConfigurableMethodTest.php | 17 +++-- .../MockObject/ConfigurableMethodsTest.php | 16 +++-- .../Framework/MockObject/MockClassTest.php | 46 +++++++++++++ .../Framework/MockObject/MockMethodTest.php | 1 + .../Framework/MockObject/MockTraitTest.php | 36 ++++++++++ .../Framework/MockObject/ObjectTypeTest.php | 23 ++++--- 32 files changed, 363 insertions(+), 165 deletions(-) delete mode 100644 src/Framework/MockObject/MockBrick.php create mode 100644 src/Framework/MockObject/MockType.php create mode 100644 tests/_files/mock-object/MockClassGenerated.tpl.dist create mode 100644 tests/_files/mock-object/MockClassWithConfigurableMethods.php create mode 100644 tests/_files/mock-object/MockTraitGenerated.tpl.dist create mode 100644 tests/unit/Framework/MockObject/MockClassTest.php create mode 100644 tests/unit/Framework/MockObject/MockTraitTest.php diff --git a/src/Framework/MockObject/Builder/InvocationMocker.php b/src/Framework/MockObject/Builder/InvocationMocker.php index d3b568688e2..52c1c4cde23 100644 --- a/src/Framework/MockObject/Builder/InvocationMocker.php +++ b/src/Framework/MockObject/Builder/InvocationMocker.php @@ -38,7 +38,7 @@ final class InvocationMocker implements MethodNameMatch */ private $configurableMethods; - public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, ConfigurableMethod... $configurableMethods) + public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, ConfigurableMethod...$configurableMethods) { $this->collection = $collection; $this->matcher = new Matcher($invocationMatcher); @@ -196,12 +196,13 @@ public function method($constraint): self ); } - $configurableMethodNames = array_map( + $configurableMethodNames = \array_map( function (ConfigurableMethod $configurable) { - return strtolower($configurable->getName()); + return \strtolower($configurable->getName()); }, $this->configurableMethods ); + if (\is_string($constraint) && !\in_array(\strtolower($constraint), $configurableMethodNames, true)) { throw new RuntimeException( \sprintf( @@ -240,32 +241,35 @@ private function canDefineParameters(): void private function getConfiguredMethod(): ?ConfigurableMethod { $configuredMethod = null; + foreach ($this->configurableMethods as $configurableMethod) { - if ($this->matcher->getMethodNameMatcher()->matchesMethodName($configurableMethod->getName())) { + if ($this->matcher->getMethodNameMatcher()->matchesName($configurableMethod->getName())) { if ($configuredMethod !== null) { return null; } $configuredMethod = $configurableMethod; } } + return $configuredMethod; } private function ensureTypeOfReturnValues(array $values): void { $configuredMethod = $this->getConfiguredMethod(); + if ($configuredMethod === null) { return; } + foreach ($values as $value) { if (!$configuredMethod->mayReturn($value)) { - throw new IncompatibleReturnValueException(sprintf( + throw new IncompatibleReturnValueException(\sprintf( 'Method %s may not return value of type %s', $configuredMethod->getName(), - gettype($value) + \gettype($value) )); } } } - } diff --git a/src/Framework/MockObject/ConfigurableMethod.php b/src/Framework/MockObject/ConfigurableMethod.php index 0bf094fcdb6..6e5de00ac05 100644 --- a/src/Framework/MockObject/ConfigurableMethod.php +++ b/src/Framework/MockObject/ConfigurableMethod.php @@ -1,12 +1,19 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; - +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ class ConfigurableMethod { - /** * @var string */ @@ -19,7 +26,7 @@ class ConfigurableMethod public function __construct(string $name, Type $returnType) { - $this->name = $name; + $this->name = $name; $this->returnType = $returnType; } @@ -33,7 +40,7 @@ public function mayReturn($value): bool if (isNull($value) && $this->returnType->allowsNull()) { return true; } + return $this->returnType->isAssignable(Type::fromValue($value, false)); } - } diff --git a/src/Framework/MockObject/ConfigurableMethods.php b/src/Framework/MockObject/ConfigurableMethods.php index 5eb2e5e9de3..9c7e69020a1 100644 --- a/src/Framework/MockObject/ConfigurableMethods.php +++ b/src/Framework/MockObject/ConfigurableMethods.php @@ -7,18 +7,19 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace PHPUnit\Framework\MockObject; +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ trait ConfigurableMethods { - /** * @var ConfigurableMethods[] */ private static $__phpunit_configurableMethods; - public static function __phpunit_initConfigurableMethods(\PHPUnit\Framework\MockObject\ConfigurableMethod... $configurable) + public static function __phpunit_initConfigurableMethods(\PHPUnit\Framework\MockObject\ConfigurableMethod...$configurable): void { if (isset(static::$__phpunit_configurableMethods)) { // TODO: improve exception diff --git a/src/Framework/MockObject/Generator.php b/src/Framework/MockObject/Generator.php index cf9d53e6afb..982a708783e 100644 --- a/src/Framework/MockObject/Generator.php +++ b/src/Framework/MockObject/Generator.php @@ -263,7 +263,7 @@ public function getMockForTrait(string $traitName, array $arguments = [], string ); $mockClass = new MockTrait($classTemplate->render(), $className['className']); - $mockClass->bringIntoExistence(); + $mockClass->generate(); return $this->getMockForAbstractClass($className['className'], $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $mockedMethods, $cloneArguments); } @@ -514,9 +514,9 @@ private function getInterfaceOwnMethods(string $interfaceName): array return $methods; } - private function getObject(MockBrick $mockClass, $type = '', bool $callOriginalConstructor = false, bool $callAutoload = false, array $arguments = [], bool $callOriginalMethods = false, object $proxyTarget = null, bool $returnValueGeneration = true) + private function getObject(MockType $mockClass, $type = '', bool $callOriginalConstructor = false, bool $callAutoload = false, array $arguments = [], bool $callOriginalMethods = false, object $proxyTarget = null, bool $returnValueGeneration = true) { - $className = $mockClass->bringIntoExistence(); + $className = $mockClass->generate(); if ($callOriginalConstructor && \is_string($type) && diff --git a/src/Framework/MockObject/Invocation.php b/src/Framework/MockObject/Invocation.php index dada8047526..b7691d4c880 100644 --- a/src/Framework/MockObject/Invocation.php +++ b/src/Framework/MockObject/Invocation.php @@ -61,7 +61,7 @@ public function __construct(string $className, string $methodName, array $parame $this->object = $object; $this->proxiedCall = $proxiedCall; - $returnType = ltrim($returnType, ': '); + $returnType = \ltrim($returnType, ': '); if (\strtolower($methodName) === '__tostring') { $returnType = 'string'; diff --git a/src/Framework/MockObject/Matcher/MethodName.php b/src/Framework/MockObject/Matcher/MethodName.php index cfb55357530..8d066e3e7bf 100644 --- a/src/Framework/MockObject/Matcher/MethodName.php +++ b/src/Framework/MockObject/Matcher/MethodName.php @@ -63,7 +63,8 @@ public function matches(BaseInvocation $invocation): bool return $this->constraint->evaluate($invocation->getMethodName(), '', true); } - public function matchesMethodName(string $methodName) : bool { + public function matchesName(string $methodName): bool + { return $this->constraint->evaluate($methodName, '', true); } } diff --git a/src/Framework/MockObject/MockBrick.php b/src/Framework/MockObject/MockBrick.php deleted file mode 100644 index 2869e2f62f2..00000000000 --- a/src/Framework/MockObject/MockBrick.php +++ /dev/null @@ -1,8 +0,0 @@ -classCode = $classCode; - $this->mockName = $mockName; + $this->classCode = $classCode; + $this->mockName = $mockName; $this->configurableMethods = $configurableMethods; } - public function getClassCode(): string + public function generate(): string { - return $this->classCode; - } + if (!\class_exists($this->mockName, false)) { + eval($this->classCode); + \call_user_func([$this->mockName, '__phpunit_initConfigurableMethods'], ...$this->configurableMethods); + } - private function getMockName(): string - { return $this->mockName; } - public function bringIntoExistence(): string + public function getClassCode(): string { - if (!\class_exists($this->getMockName(), false)) { - eval($this->getClassCode()); - call_user_func(array($this->getMockName(), '__phpunit_initConfigurableMethods'), ...$this->configurableMethods); - } - return $this->getMockName(); + return $this->classCode; } - } diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 7e2a66663c6..22cde1438e1 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -7,7 +7,6 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace PHPUnit\Framework\MockObject; /** @@ -150,18 +149,18 @@ public static function fromName(string $fullClassName, string $methodName, bool 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) { - $this->className = $className; - $this->methodName = $methodName; - $this->cloneArguments = $cloneArguments; - $this->modifier = $modifier; + $this->className = $className; + $this->methodName = $methodName; + $this->cloneArguments = $cloneArguments; + $this->modifier = $modifier; $this->argumentsForDeclaration = $argumentsForDeclaration; - $this->argumentsForCall = $argumentsForCall; - $this->returnType = $returnType; - $this->reference = $reference; - $this->callOriginalMethod = $callOriginalMethod; - $this->static = $static; - $this->deprecation = $deprecation; - $this->allowsReturnNull = $allowsReturnNull; + $this->argumentsForCall = $argumentsForCall; + $this->returnType = $returnType; + $this->reference = $reference; + $this->callOriginalMethod = $callOriginalMethod; + $this->static = $static; + $this->deprecation = $deprecation; + $this->allowsReturnNull = $allowsReturnNull; } public function getName(): string @@ -191,7 +190,7 @@ public function generateCode(): string $deprecation = $this->deprecation; if (null !== $this->deprecation) { - $deprecation = "The $this->className::$this->methodName method is deprecated ($this->deprecation)."; + $deprecation = "The $this->className::$this->methodName method is deprecated ($this->deprecation)."; $deprecationTemplate = $this->getTemplate('deprecation.tpl'); $deprecationTemplate->setVar([ @@ -205,22 +204,27 @@ public function generateCode(): string $template->setVar( [ - 'arguments_decl' => $this->argumentsForDeclaration, - 'arguments_call' => $this->argumentsForCall, + 'arguments_decl' => $this->argumentsForDeclaration, + 'arguments_call' => $this->argumentsForCall, 'return_declaration' => $this->returnType->getReturnTypeDeclaration(), - 'arguments_count' => !empty($this->argumentsForCall) ? \substr_count($this->argumentsForCall, ',') + 1 : 0, - 'class_name' => $this->className, - 'method_name' => $this->methodName, - 'modifier' => $this->modifier, - 'reference' => $this->reference, - 'clone_arguments' => $this->cloneArguments ? 'true' : 'false', - 'deprecation' => $deprecation, + 'arguments_count' => !empty($this->argumentsForCall) ? \substr_count($this->argumentsForCall, ',') + 1 : 0, + 'class_name' => $this->className, + 'method_name' => $this->methodName, + 'modifier' => $this->modifier, + 'reference' => $this->reference, + 'clone_arguments' => $this->cloneArguments ? 'true' : 'false', + 'deprecation' => $deprecation, ] ); return $template->render(); } + public function getReturnType(): Type + { + return $this->returnType; + } + private function getTemplate(string $template): \Text_Template { $filename = __DIR__ . \DIRECTORY_SEPARATOR . 'Generator' . \DIRECTORY_SEPARATOR . $template; @@ -259,9 +263,9 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for $name = '...' . $name; } - $nullable = ''; - $default = ''; - $reference = ''; + $nullable = ''; + $default = ''; + $reference = ''; $typeDeclaration = ''; if (!$forCall) { @@ -269,7 +273,7 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for $nullable = '?'; } - if ($parameter->hasType() && (string)$parameter->getType() !== 'self') { + if ($parameter->hasType() && (string) $parameter->getType() !== 'self') { $typeDeclaration = $parameter->getType() . ' '; } else { try { @@ -299,7 +303,7 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for } catch (\ReflectionException $e) { throw new RuntimeException( $e->getMessage(), - (int)$e->getCode(), + (int) $e->getCode(), $e ); } @@ -310,13 +314,13 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for } catch (\ReflectionException $e) { throw new RuntimeException( $e->getMessage(), - (int)$e->getCode(), + (int) $e->getCode(), $e ); } } elseif (!\defined($value)) { $rootValue = \preg_replace('/^.*\\\\/', '', $value); - $value = \defined($rootValue) ? $rootValue : $value; + $value = \defined($rootValue) ? $rootValue : $value; } $default = ' = ' . $value; @@ -369,9 +373,4 @@ private static function deriveReturnType(\ReflectionMethod $method): ?Type return Type::fromName($returnType->getName(), $returnType->allowsNull()); } - - public function getReturnType(): Type - { - return $this->returnType; - } } diff --git a/src/Framework/MockObject/MockTrait.php b/src/Framework/MockObject/MockTrait.php index 6634ce33787..d9d58e0ba89 100644 --- a/src/Framework/MockObject/MockTrait.php +++ b/src/Framework/MockObject/MockTrait.php @@ -7,12 +7,13 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace PHPUnit\Framework\MockObject; -class MockTrait implements MockBrick +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +class MockTrait implements MockType { - /** * @var string */ @@ -26,25 +27,20 @@ class MockTrait implements MockBrick public function __construct(string $classCode, string $mockName) { $this->classCode = $classCode; - $this->mockName = $mockName; + $this->mockName = $mockName; } - public function getClassCode(): string + public function generate(): string { - return $this->classCode; - } + if (!\class_exists($this->mockName, false)) { + eval($this->classCode); + } - public function getMockName(): string - { return $this->mockName; } - public function bringIntoExistence(): string + public function getClassCode(): string { - if (!\class_exists($this->getMockName(), false)) { - eval($this->getClassCode()); - } - return $this->getMockName(); + return $this->classCode; } - } diff --git a/src/Framework/MockObject/MockType.php b/src/Framework/MockObject/MockType.php new file mode 100644 index 00000000000..b35ac306d20 --- /dev/null +++ b/src/Framework/MockObject/MockType.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +interface MockType +{ + public function generate(): string; +} diff --git a/src/Framework/MockObject/ObjectType.php b/src/Framework/MockObject/ObjectType.php index 0bf40b863db..778b54ba2d9 100644 --- a/src/Framework/MockObject/ObjectType.php +++ b/src/Framework/MockObject/ObjectType.php @@ -1,12 +1,19 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; - +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ class ObjectType extends Type { - /** * @var TypeName */ @@ -19,7 +26,7 @@ class ObjectType extends Type public function __construct(TypeName $className, bool $nullable) { - $this->className = $className; + $this->className = $className; $this->allowsNull = $nullable; } @@ -28,15 +35,17 @@ public function isAssignable(Type $other): bool if ($this->allowsNull && isNull($other)) { return true; } + if ($other instanceof self) { if ($this->className->getQualifiedName() === $other->className->getQualifiedName()) { return true; } - if (is_subclass_of($other->className->getQualifiedName(), $this->className->getQualifiedName(), true)) { + if (\is_subclass_of($other->className->getQualifiedName(), $this->className->getQualifiedName(), true)) { return true; } } + return false; } diff --git a/src/Framework/MockObject/SimpleType.php b/src/Framework/MockObject/SimpleType.php index 5c0cecc6bde..c938ce188eb 100644 --- a/src/Framework/MockObject/SimpleType.php +++ b/src/Framework/MockObject/SimpleType.php @@ -1,12 +1,19 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; - +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ class SimpleType extends Type { - /** * @var string */ @@ -19,7 +26,7 @@ class SimpleType extends Type public function __construct(string $name, bool $nullable) { - $this->name = $this->normalize($name); + $this->name = $this->normalize($name); $this->allowsNull = $nullable; } @@ -28,9 +35,11 @@ public function isAssignable(Type $other): bool if ($this->allowsNull && isNull($other)) { return true; } + if ($other instanceof self) { return $this->name === $other->name; } + return false; } @@ -46,7 +55,8 @@ public function allowsNull(): bool private function normalize(string $name): string { - $name = mb_strtolower($name); + $name = \mb_strtolower($name); + switch ($name) { case 'boolean': return 'bool'; diff --git a/src/Framework/MockObject/Type.php b/src/Framework/MockObject/Type.php index 398cd5c69a8..0a5619585b1 100644 --- a/src/Framework/MockObject/Type.php +++ b/src/Framework/MockObject/Type.php @@ -9,24 +9,26 @@ */ namespace PHPUnit\Framework\MockObject; - +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ abstract class Type { - - public static function fromValue($value, bool $allowsNull): Type + public static function fromValue($value, bool $allowsNull): self { - $type = gettype($value); + $type = \gettype($value); + switch ($type) { case 'object': - return self::fromName(get_class($value), $allowsNull); + return self::fromName(\get_class($value), $allowsNull); default: return new SimpleType($type, $allowsNull); } } - public static function fromName(?string $typeName, bool $allowsNull): Type + public static function fromName(?string $typeName, bool $allowsNull): self { - switch (mb_strtolower($typeName)) { + switch (\mb_strtolower($typeName)) { case null: case 'unknown type': return new UnknownType(); @@ -48,10 +50,9 @@ public static function fromName(?string $typeName, bool $allowsNull): Type } } - abstract public function isAssignable(Type $other): bool; + abstract public function isAssignable(self $other): bool; abstract public function getReturnTypeDeclaration(): string; abstract public function allowsNull(): bool; - } diff --git a/src/Framework/MockObject/UnknownType.php b/src/Framework/MockObject/UnknownType.php index 108dcf7eed2..0e09de56010 100644 --- a/src/Framework/MockObject/UnknownType.php +++ b/src/Framework/MockObject/UnknownType.php @@ -1,9 +1,17 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; - +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ class UnknownType extends Type { public function isAssignable(Type $other): bool diff --git a/tests/_files/mock-object/ChildClass.php b/tests/_files/mock-object/ChildClass.php index 18d080992cb..ac981e0ec12 100644 --- a/tests/_files/mock-object/ChildClass.php +++ b/tests/_files/mock-object/ChildClass.php @@ -7,7 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace PHPUnit\Framework\MockObject; +namespace PHPUnit\TestFixture\MockObject; class ChildClass extends ParentClass { diff --git a/tests/_files/mock-object/ClassAUsingConfigurableMethods.php b/tests/_files/mock-object/ClassAUsingConfigurableMethods.php index 2adf0ab144d..ac9e1aae0c2 100644 --- a/tests/_files/mock-object/ClassAUsingConfigurableMethods.php +++ b/tests/_files/mock-object/ClassAUsingConfigurableMethods.php @@ -7,13 +7,16 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +namespace PHPUnit\TestFixture\MockObject; -namespace PHPUnit\Framework\MockObject; +use PHPUnit\Framework\MockObject\ConfigurableMethods; -class ClassAUsingConfigurableMethods { +class ClassAUsingConfigurableMethods +{ use ConfigurableMethods; - public static function getConfigurableMethods() { + public static function getConfigurableMethods(): array + { return static::$__phpunit_configurableMethods; } } diff --git a/tests/_files/mock-object/ClassBUsingConfigurableMethods.php b/tests/_files/mock-object/ClassBUsingConfigurableMethods.php index a9a4b1ad6bc..4f38044bd78 100644 --- a/tests/_files/mock-object/ClassBUsingConfigurableMethods.php +++ b/tests/_files/mock-object/ClassBUsingConfigurableMethods.php @@ -7,16 +7,16 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +namespace PHPUnit\TestFixture\MockObject; -namespace PHPUnit\Framework\MockObject; +use PHPUnit\Framework\MockObject\ConfigurableMethods; class ClassBUsingConfigurableMethods { use ConfigurableMethods; - public static function getConfigurableMethods() + public static function getConfigurableMethods(): array { return static::$__phpunit_configurableMethods; } - } diff --git a/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php b/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php index bc1b7426648..c3aefaa9e6e 100644 --- a/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php +++ b/tests/_files/mock-object/ClassWithoutParentButParentReturnType.php @@ -7,14 +7,11 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +namespace PHPUnit\TestFixture\MockObject; -namespace PHPUnit\Framework\MockObject; - -class ClassWithoutParentButParentReturnType { - - +class ClassWithoutParentButParentReturnType +{ public function foo(): parent { } - } diff --git a/tests/_files/mock-object/MockClassGenerated.tpl.dist b/tests/_files/mock-object/MockClassGenerated.tpl.dist new file mode 100644 index 00000000000..01372e8b8f1 --- /dev/null +++ b/tests/_files/mock-object/MockClassGenerated.tpl.dist @@ -0,0 +1,13 @@ +namespace PHPUnit\TestFixture\MockObject; + +use PHPUnit\Framework\MockObject\ConfigurableMethods; + +class MockClassGenerated +{ + use ConfigurableMethods; + + public static function getConfigurableMethods(): array + { + return static::$__phpunit_configurableMethods; + } +} diff --git a/tests/_files/mock-object/MockClassWithConfigurableMethods.php b/tests/_files/mock-object/MockClassWithConfigurableMethods.php new file mode 100644 index 00000000000..cc3745576fd --- /dev/null +++ b/tests/_files/mock-object/MockClassWithConfigurableMethods.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\MockObject; + +use PHPUnit\Framework\MockObject\ConfigurableMethods; + +class MockClassWithConfigurableMethods +{ + use ConfigurableMethods; + + public static function getConfigurableMethods(): array + { + return static::$__phpunit_configurableMethods; + } +} diff --git a/tests/_files/mock-object/MockTraitGenerated.tpl.dist b/tests/_files/mock-object/MockTraitGenerated.tpl.dist new file mode 100644 index 00000000000..05092777a61 --- /dev/null +++ b/tests/_files/mock-object/MockTraitGenerated.tpl.dist @@ -0,0 +1,5 @@ +namespace PHPUnit\TestFixture\MockObject; + +trait MockTraitGenerated +{ +} diff --git a/tests/_files/mock-object/ParentClass.php b/tests/_files/mock-object/ParentClass.php index 62a4f9bd129..1168c69eff5 100644 --- a/tests/_files/mock-object/ParentClass.php +++ b/tests/_files/mock-object/ParentClass.php @@ -7,7 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace PHPUnit\Framework\MockObject; +namespace PHPUnit\TestFixture\MockObject; class ParentClass { diff --git a/tests/_files/namespace/someNamespaceA/NamespacedClass.php b/tests/_files/namespace/someNamespaceA/NamespacedClass.php index 889fdd7592f..b035c17b905 100644 --- a/tests/_files/namespace/someNamespaceA/NamespacedClass.php +++ b/tests/_files/namespace/someNamespaceA/NamespacedClass.php @@ -1,7 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace someNamespaceA; -class NamespacedClass { - +class NamespacedClass +{ } diff --git a/tests/_files/namespace/someNamespaceB/NamespacedClass.php b/tests/_files/namespace/someNamespaceB/NamespacedClass.php index 89b864c8d57..e2eb0f22d0b 100644 --- a/tests/_files/namespace/someNamespaceB/NamespacedClass.php +++ b/tests/_files/namespace/someNamespaceB/NamespacedClass.php @@ -1,7 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace someNamespaceB; -class NamespacedClass { - +class NamespacedClass +{ } diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index 9c2d4dc57f5..4c12d89aa2d 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -82,7 +82,7 @@ public function testWillFailWhenTryingToPerformExpectationUnconfigurableMethod() { /** @var MatcherCollection|\PHPUnit\Framework\MockObject\MockObject $matcherCollection */ $matcherCollection = $this->createMock(MatcherCollection::class); - $invocationMocker = new \PHPUnit\Framework\MockObject\Builder\InvocationMocker( + $invocationMocker = new \PHPUnit\Framework\MockObject\Builder\InvocationMocker( $matcherCollection, $this->any() ); @@ -126,5 +126,4 @@ public function testWillReturnFailsWhenTryingToReturnAtLeastOneIncompatibleValue $this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer'); $invocationMocker->willReturn(true, 1); } - } diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php index dcfc4e63fb0..39c9db62e5d 100644 --- a/tests/unit/Framework/MockObject/ConfigurableMethodTest.php +++ b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php @@ -1,13 +1,19 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; use PHPUnit\Framework\TestCase; class ConfigurableMethodTest extends TestCase { - - public function testMethodMayReturnAssignableValue() + public function testMethodMayReturnAssignableValue(): void { $assignableType = $this->createMock(Type::class); $assignableType->method('isAssignable') @@ -16,7 +22,7 @@ public function testMethodMayReturnAssignableValue() $this->assertTrue($configurable->mayReturn('everything-is-valid')); } - public function testMethodMayNotReturnUnassignableValue() + public function testMethodMayNotReturnUnassignableValue(): void { $unassignableType = $this->createMock(Type::class); $unassignableType->method('isAssignable') @@ -24,5 +30,4 @@ public function testMethodMayNotReturnUnassignableValue() $configurable = new ConfigurableMethod('foo', $unassignableType); $this->assertFalse($configurable->mayReturn('everything-is-invalid')); } - } diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php index efe82196a0e..391f85510f8 100644 --- a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php +++ b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php @@ -1,12 +1,21 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; use PHPUnit\Framework\TestCase; +use PHPUnit\TestFixture\MockObject\ClassAUsingConfigurableMethods; +use PHPUnit\TestFixture\MockObject\ClassBUsingConfigurableMethods; class ConfigurableMethodsTest extends TestCase { - public function testTwoClassesUsingConfigurableMethodsDontInterfere() + public function testTwoClassesUsingConfigurableMethodsDontInterfere(): void { $configurableMethodsA = [new ConfigurableMethod('foo', SimpleType::fromValue('boolean', false))]; $configurableMethodsB = []; @@ -16,5 +25,4 @@ public function testTwoClassesUsingConfigurableMethodsDontInterfere() $this->assertSame($configurableMethodsA, ClassAUsingConfigurableMethods::getConfigurableMethods()); $this->assertSame($configurableMethodsB, ClassBUsingConfigurableMethods::getConfigurableMethods()); } - } diff --git a/tests/unit/Framework/MockObject/MockClassTest.php b/tests/unit/Framework/MockObject/MockClassTest.php new file mode 100644 index 00000000000..a1ec41f8bae --- /dev/null +++ b/tests/unit/Framework/MockObject/MockClassTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; +use PHPUnit\TestFixture\MockObject\MockClassWithConfigurableMethods; + +class MockClassTest extends TestCase +{ + public function testGenerateClassFromSource(): void + { + $mockName = 'PHPUnit\TestFixture\MockObject\MockClassGenerated'; + + $file = __DIR__ . '/../../../_files/mock-object/MockClassGenerated.tpl.dist'; + + $mockClass = new MockClass(\file_get_contents($file), $mockName, []); + $mockClass->generate(); + + $this->assertTrue(\class_exists($mockName)); + } + + public function testGenerateReturnsNameOfGeneratedClass(): void + { + $mockName = 'PHPUnit\TestFixture\MockObject\MockClassGenerated'; + + $mockClass = new MockClass('', $mockName, []); + + $this->assertEquals($mockName, $mockClass->generate()); + } + + public function testConfigurableMethodsAreInitalized(): void + { + $configurableMethods = [new ConfigurableMethod('foo', Type::fromName('void', false))]; + $mockClass = new MockClass('', MockClassWithConfigurableMethods::class, $configurableMethods); + $mockClass->generate(); + + $this->assertSame($configurableMethods, MockClassWithConfigurableMethods::getConfigurableMethods()); + } +} diff --git a/tests/unit/Framework/MockObject/MockMethodTest.php b/tests/unit/Framework/MockObject/MockMethodTest.php index 73661a786bf..e4d9cb53079 100644 --- a/tests/unit/Framework/MockObject/MockMethodTest.php +++ b/tests/unit/Framework/MockObject/MockMethodTest.php @@ -10,6 +10,7 @@ namespace PHPUnit\Framework\MockObject; use PHPUnit\Framework\TestCase; +use PHPUnit\TestFixture\MockObject\ClassWithoutParentButParentReturnType; /** * @small diff --git a/tests/unit/Framework/MockObject/MockTraitTest.php b/tests/unit/Framework/MockObject/MockTraitTest.php new file mode 100644 index 00000000000..a0efc1aaff8 --- /dev/null +++ b/tests/unit/Framework/MockObject/MockTraitTest.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +class MockTraitTest extends TestCase +{ + public function testGenerateClassFromSource(): void + { + $mockName = 'PHPUnit\TestFixture\MockObject\MockTraitGenerated'; + + $file = __DIR__ . '/../../../_files/mock-object/MockTraitGenerated.tpl.dist'; + + $mockTrait = new MockTrait(\file_get_contents($file), $mockName); + $mockTrait->generate(); + + $this->assertTrue(\trait_exists($mockName)); + } + + public function testGenerateReturnsNameOfGeneratedClass(): void + { + $mockName = 'PHPUnit\TestFixture\MockObject\MockTraitGenerated'; + + $mockTrait = new MockTrait('', $mockName); + + $this->assertEquals($mockName, $mockTrait->generate()); + } +} diff --git a/tests/unit/Framework/MockObject/ObjectTypeTest.php b/tests/unit/Framework/MockObject/ObjectTypeTest.php index 01e960f8bf7..05152ecd59c 100644 --- a/tests/unit/Framework/MockObject/ObjectTypeTest.php +++ b/tests/unit/Framework/MockObject/ObjectTypeTest.php @@ -1,16 +1,25 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace PHPUnit\Framework\MockObject; use PHPUnit\Framework\TestCase; +use PHPUnit\TestFixture\MockObject\ChildClass; +use PHPUnit\TestFixture\MockObject\ParentClass; class ObjectTypeTest extends TestCase { - /** * @var ObjectType */ private $childClass; + /** * @var ObjectType */ @@ -49,7 +58,7 @@ public function testSimpleTypeIsNotAssignableToClass(): void $this->assertFalse($this->parentClass->isAssignable(new SimpleType('int', false))); } - public function testClassFromOneNamespaceIsNotAssignableToClassInOtherNamespace() + public function testClassFromOneNamespaceIsNotAssignableToClassInOtherNamespace(): void { $classFromNamespaceA = new ObjectType( TypeName::fromQualifiedName(\someNamespaceA\NamespacedClass::class), @@ -62,7 +71,7 @@ public function testClassFromOneNamespaceIsNotAssignableToClassInOtherNamespace( $this->assertFalse($classFromNamespaceA->isAssignable($classFromNamespaceB)); } - public function testNullIsAssignableToNullableType() + public function testNullIsAssignableToNullableType(): void { $someClass = new ObjectType( TypeName::fromQualifiedName(ParentClass::class), @@ -71,7 +80,7 @@ public function testNullIsAssignableToNullableType() $this->assertTrue($someClass->isAssignable(Type::fromValue(null, true))); } - public function testNullIsNotAssignableToNotNullableType() + public function testNullIsNotAssignableToNotNullableType(): void { $someClass = new ObjectType( TypeName::fromQualifiedName(ParentClass::class), @@ -79,6 +88,4 @@ public function testNullIsNotAssignableToNotNullableType() ); $this->assertFalse($someClass->isAssignable(Type::fromValue(null, true))); } - - } From fdcd69b327f7da20636fe47fed9cb8e83a3252c8 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 15:51:03 +0200 Subject: [PATCH 03/11] moved exception to mock-object classes --- src/Framework/MockObject/Builder/InvocationMocker.php | 2 +- .../Exception/IncompatibleReturnValueException.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/Framework/{ => MockObject}/Exception/IncompatibleReturnValueException.php (71%) diff --git a/src/Framework/MockObject/Builder/InvocationMocker.php b/src/Framework/MockObject/Builder/InvocationMocker.php index 52c1c4cde23..983b08d5491 100644 --- a/src/Framework/MockObject/Builder/InvocationMocker.php +++ b/src/Framework/MockObject/Builder/InvocationMocker.php @@ -10,8 +10,8 @@ namespace PHPUnit\Framework\MockObject\Builder; use PHPUnit\Framework\Constraint\Constraint; -use PHPUnit\Framework\IncompatibleReturnValueException; use PHPUnit\Framework\MockObject\ConfigurableMethod; +use PHPUnit\Framework\MockObject\IncompatibleReturnValueException; use PHPUnit\Framework\MockObject\Matcher; use PHPUnit\Framework\MockObject\Matcher\Invocation; use PHPUnit\Framework\MockObject\RuntimeException; diff --git a/src/Framework/Exception/IncompatibleReturnValueException.php b/src/Framework/MockObject/Exception/IncompatibleReturnValueException.php similarity index 71% rename from src/Framework/Exception/IncompatibleReturnValueException.php rename to src/Framework/MockObject/Exception/IncompatibleReturnValueException.php index 9e1c2147cc9..f1ceb1debfa 100644 --- a/src/Framework/Exception/IncompatibleReturnValueException.php +++ b/src/Framework/MockObject/Exception/IncompatibleReturnValueException.php @@ -7,11 +7,11 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace PHPUnit\Framework; +namespace PHPUnit\Framework\MockObject; /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -final class IncompatibleReturnValueException extends Exception +final class IncompatibleReturnValueException extends \PHPUnit\Framework\Exception implements Exception { } From c6e946f33bf69dbee031d282d8b9146f0d16cdbd Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 15:52:44 +0200 Subject: [PATCH 04/11] Cleaned handling of invalid re-initialization of ConfigurableMethods --- .../MockObject/ConfigurableMethods.php | 3 +-- ...urableMethodsAlreadyInitializedException.php | 17 +++++++++++++++++ .../ReinitializeConfigurableMethods.php | 17 +++++++++++++++++ .../MockObject/ConfigurableMethodsTest.php | 8 ++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/Framework/MockObject/Exception/ConfigurableMethodsAlreadyInitializedException.php create mode 100644 tests/_files/mock-object/ReinitializeConfigurableMethods.php diff --git a/src/Framework/MockObject/ConfigurableMethods.php b/src/Framework/MockObject/ConfigurableMethods.php index 9c7e69020a1..ac286ec5856 100644 --- a/src/Framework/MockObject/ConfigurableMethods.php +++ b/src/Framework/MockObject/ConfigurableMethods.php @@ -22,8 +22,7 @@ trait ConfigurableMethods public static function __phpunit_initConfigurableMethods(\PHPUnit\Framework\MockObject\ConfigurableMethod...$configurable): void { if (isset(static::$__phpunit_configurableMethods)) { - // TODO: improve exception - throw new \Exception('nogo!'); + throw new ConfigurableMethodsAlreadyInitializedException('Configurable methods is already initialized and can not be reinitialized.'); } static::$__phpunit_configurableMethods = $configurable; } diff --git a/src/Framework/MockObject/Exception/ConfigurableMethodsAlreadyInitializedException.php b/src/Framework/MockObject/Exception/ConfigurableMethodsAlreadyInitializedException.php new file mode 100644 index 00000000000..d12ac997382 --- /dev/null +++ b/src/Framework/MockObject/Exception/ConfigurableMethodsAlreadyInitializedException.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class ConfigurableMethodsAlreadyInitializedException extends \PHPUnit\Framework\Exception implements Exception +{ +} diff --git a/tests/_files/mock-object/ReinitializeConfigurableMethods.php b/tests/_files/mock-object/ReinitializeConfigurableMethods.php new file mode 100644 index 00000000000..9af0d4a6aa4 --- /dev/null +++ b/tests/_files/mock-object/ReinitializeConfigurableMethods.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\MockObject; + +use PHPUnit\Framework\MockObject\ConfigurableMethods; + +class ReinitializeConfigurableMethods +{ + use ConfigurableMethods; +} diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php index 391f85510f8..e87458653a3 100644 --- a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php +++ b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\TestCase; use PHPUnit\TestFixture\MockObject\ClassAUsingConfigurableMethods; use PHPUnit\TestFixture\MockObject\ClassBUsingConfigurableMethods; +use PHPUnit\TestFixture\MockObject\ReinitializeConfigurableMethods; class ConfigurableMethodsTest extends TestCase { @@ -25,4 +26,11 @@ public function testTwoClassesUsingConfigurableMethodsDontInterfere(): void $this->assertSame($configurableMethodsA, ClassAUsingConfigurableMethods::getConfigurableMethods()); $this->assertSame($configurableMethodsB, ClassBUsingConfigurableMethods::getConfigurableMethods()); } + + public function testConfigurableMethodsAreImmutable(): void + { + ReinitializeConfigurableMethods::__phpunit_initConfigurableMethods(); + $this->expectException(ConfigurableMethodsAlreadyInitializedException::class); + ReinitializeConfigurableMethods::__phpunit_initConfigurableMethods(); + } } From 49646919cc3462b30aa79249ef5e90aeb6aa69d2 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 15:54:02 +0200 Subject: [PATCH 05/11] fixes to the handling of return types --- src/Framework/MockObject/NullType.php | 31 ++++++++ src/Framework/MockObject/ObjectType.php | 6 +- src/Framework/MockObject/SimpleType.php | 2 +- src/Framework/MockObject/Type.php | 9 ++- src/Framework/MockObject/VoidType.php | 31 ++++++++ .../Builder/InvocationMockerTest.php | 16 +++- .../Framework/MockObject/NullTypeTest.php | 63 +++++++++++++++ .../Framework/MockObject/ObjectTypeTest.php | 21 +++++ .../Framework/MockObject/SimpleTypeTest.php | 77 +++++++++++++++++++ tests/unit/Framework/MockObject/TypeTest.php | 74 ++++++++++++++++++ .../Framework/MockObject/UnknownTypeTest.php | 51 ++++++++++++ .../Framework/MockObject/VoidTypeTest.php | 66 ++++++++++++++++ 12 files changed, 439 insertions(+), 8 deletions(-) create mode 100644 src/Framework/MockObject/NullType.php create mode 100644 src/Framework/MockObject/VoidType.php create mode 100644 tests/unit/Framework/MockObject/NullTypeTest.php create mode 100644 tests/unit/Framework/MockObject/SimpleTypeTest.php create mode 100644 tests/unit/Framework/MockObject/TypeTest.php create mode 100644 tests/unit/Framework/MockObject/UnknownTypeTest.php create mode 100644 tests/unit/Framework/MockObject/VoidTypeTest.php diff --git a/src/Framework/MockObject/NullType.php b/src/Framework/MockObject/NullType.php new file mode 100644 index 00000000000..c3490b80d66 --- /dev/null +++ b/src/Framework/MockObject/NullType.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +class NullType extends Type +{ + public function isAssignable(Type $other): bool + { + return !($other instanceof VoidType); + } + + public function getReturnTypeDeclaration(): string + { + return ''; + } + + public function allowsNull(): bool + { + return true; + } +} diff --git a/src/Framework/MockObject/ObjectType.php b/src/Framework/MockObject/ObjectType.php index 778b54ba2d9..80446efa314 100644 --- a/src/Framework/MockObject/ObjectType.php +++ b/src/Framework/MockObject/ObjectType.php @@ -24,15 +24,15 @@ class ObjectType extends Type */ private $allowsNull; - public function __construct(TypeName $className, bool $nullable) + public function __construct(TypeName $className, bool $allowsNull) { $this->className = $className; - $this->allowsNull = $nullable; + $this->allowsNull = $allowsNull; } public function isAssignable(Type $other): bool { - if ($this->allowsNull && isNull($other)) { + if ($this->allowsNull && $other instanceof NullType) { return true; } diff --git a/src/Framework/MockObject/SimpleType.php b/src/Framework/MockObject/SimpleType.php index c938ce188eb..76e438cc6ce 100644 --- a/src/Framework/MockObject/SimpleType.php +++ b/src/Framework/MockObject/SimpleType.php @@ -32,7 +32,7 @@ public function __construct(string $name, bool $nullable) public function isAssignable(Type $other): bool { - if ($this->allowsNull && isNull($other)) { + if ($this->allowsNull && $other instanceof NullType) { return true; } diff --git a/src/Framework/MockObject/Type.php b/src/Framework/MockObject/Type.php index 0a5619585b1..71e8b8863fc 100644 --- a/src/Framework/MockObject/Type.php +++ b/src/Framework/MockObject/Type.php @@ -20,18 +20,21 @@ public static function fromValue($value, bool $allowsNull): self switch ($type) { case 'object': - return self::fromName(\get_class($value), $allowsNull); + return new ObjectType(TypeName::fromQualifiedName(\get_class($value)), $allowsNull); default: - return new SimpleType($type, $allowsNull); + return self::fromName($type, $allowsNull); } } public static function fromName(?string $typeName, bool $allowsNull): self { switch (\mb_strtolower($typeName)) { - case null: + case 'null': + return new NullType(); case 'unknown type': return new UnknownType(); + case 'void': + return new VoidType(); case 'object': case 'boolean': case 'bool': diff --git a/src/Framework/MockObject/VoidType.php b/src/Framework/MockObject/VoidType.php new file mode 100644 index 00000000000..599029b2d9a --- /dev/null +++ b/src/Framework/MockObject/VoidType.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +class VoidType extends Type +{ + public function isAssignable(Type $other): bool + { + return $other instanceof self; + } + + public function getReturnTypeDeclaration(): string + { + return ': void'; + } + + public function allowsNull(): bool + { + return false; + } +} diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index 4c12d89aa2d..d51a53846c6 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -9,11 +9,12 @@ * file that was distributed with this source code. */ -use PHPUnit\Framework\IncompatibleReturnValueException; +use PHPUnit\Framework\MockObject\IncompatibleReturnValueException; use PHPUnit\Framework\MockObject\Stub\MatcherCollection; use PHPUnit\Framework\TestCase; /** + * @covers \PHPUnit\Framework\MockObject\Builder\InvocationMocker * @small */ final class InvocationMockerTest extends TestCase @@ -126,4 +127,17 @@ public function testWillReturnFailsWhenTryingToReturnAtLeastOneIncompatibleValue $this->expectExceptionMessage('Method methodWithBoolReturnTypeDeclaration may not return value of type integer'); $invocationMocker->willReturn(true, 1); } + + public function testWillReturnAllowsMatchersForMultipleMethodsWithDifferentReturnTypes(): void + { + /** @var ClassWithAllPossibleReturnTypes|\PHPUnit\Framework\MockObject\MockObject $mock */ + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $invocationMocker = $mock->method(new \PHPUnit\Framework\Constraint\IsAnything()); + $invocationMocker->willReturn(true, 1); + + $this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration()); + $this->assertEquals(1, $mock->methodWithIntReturnTypeDeclaration()); + } } diff --git a/tests/unit/Framework/MockObject/NullTypeTest.php b/tests/unit/Framework/MockObject/NullTypeTest.php new file mode 100644 index 00000000000..b742110c7a4 --- /dev/null +++ b/tests/unit/Framework/MockObject/NullTypeTest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +class NullTypeTest extends TestCase +{ + /** + * @dataProvider assignableTypes + */ + public function testIsAssignable(Type $assignableType): void + { + $type = new NullType(); + $this->assertTrue($type->isAssignable($assignableType)); + } + + public function assignableTypes(): array + { + return [ + [new SimpleType('int', false)], + [new SimpleType('int', true)], + [new ObjectType(TypeName::fromQualifiedName(self::class), false)], + [new ObjectType(TypeName::fromQualifiedName(self::class), true)], + [new UnknownType()], + ]; + } + + /** + * @dataProvider notAssignable + */ + public function testIsNotAssignable(Type $assignedType): void + { + $type = new NullType(); + $this->assertFalse($type->isAssignable($assignedType)); + } + + public function notAssignable(): array + { + return [ + 'void' => [new VoidType()], + ]; + } + + public function testAllowsNull(): void + { + $type = new NullType(); + $this->assertTrue($type->allowsNull()); + } + + public function testReturnTypeDeclaration(): void + { + $type = new NullType(); + $this->assertEquals('', $type->getReturnTypeDeclaration()); + } +} diff --git a/tests/unit/Framework/MockObject/ObjectTypeTest.php b/tests/unit/Framework/MockObject/ObjectTypeTest.php index 05152ecd59c..0215c02bb4e 100644 --- a/tests/unit/Framework/MockObject/ObjectTypeTest.php +++ b/tests/unit/Framework/MockObject/ObjectTypeTest.php @@ -13,6 +13,9 @@ use PHPUnit\TestFixture\MockObject\ChildClass; use PHPUnit\TestFixture\MockObject\ParentClass; +/** + * @covers \PHPUnit\Framework\MockObject\ObjectType + */ class ObjectTypeTest extends TestCase { /** @@ -88,4 +91,22 @@ public function testNullIsNotAssignableToNotNullableType(): void ); $this->assertFalse($someClass->isAssignable(Type::fromValue(null, true))); } + + public function testPreservesNullNotAllowed(): void + { + $someClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + false + ); + $this->assertFalse($someClass->allowsNull()); + } + + public function testPreservesNullAllowed(): void + { + $someClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + true + ); + $this->assertTrue($someClass->allowsNull()); + } } diff --git a/tests/unit/Framework/MockObject/SimpleTypeTest.php b/tests/unit/Framework/MockObject/SimpleTypeTest.php new file mode 100644 index 00000000000..5927a2b97b5 --- /dev/null +++ b/tests/unit/Framework/MockObject/SimpleTypeTest.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +/** + * @covers \PHPUnit\Framework\MockObject\SimpleType + */ +class SimpleTypeTest extends TestCase +{ + /** + * @dataProvider assignablePairs + */ + public function testIsAssignable(Type $assignTo, Type $assignedType): void + { + $this->assertTrue($assignTo->isAssignable($assignedType)); + } + + public function assignablePairs(): array + { + return [ + 'nullable to notNullable' => [new SimpleType('int', false), new SimpleType('int', true)], + 'notNullable to nullable' => [new SimpleType('int', true), new SimpleType('int', false)], + 'nullable to nullable' => [new SimpleType('int', true), new SimpleType('int', true)], + 'notNullable to notNullable' => [new SimpleType('int', false), new SimpleType('int', false)], + 'null to notNullable' => [new SimpleType('int', true), new NullType()], + ]; + } + + /** + * @dataProvider notAssignablePairs + */ + public function testIsNotAssignable(Type $assignTo, Type $assignedType): void + { + $this->assertFalse($assignTo->isAssignable($assignedType)); + } + + public function notAssignablePairs(): array + { + return [ + 'null to notNullable' => [new SimpleType('int', false), new NullType()], + 'int to boolean' => [new SimpleType('boolean', false), new SimpleType('int', false)], + 'object' => [new SimpleType('boolean', false), new ObjectType(TypeName::fromQualifiedName(\stdClass::class), true)], + 'unknown type' => [new SimpleType('boolean', false), new UnknownType()], + 'void' => [new SimpleType('boolean', false), new VoidType()], + ]; + } + + /** + * @dataProvider returnTypes + */ + public function testReturnTypeDeclaration(Type $type, string $returnType): void + { + $this->assertEquals($type->getReturnTypeDeclaration(), $returnType); + } + + public function returnTypes(): array + { + return [ + '[]' => [new SimpleType('[]', false), ': array'], + 'array' => [new SimpleType('array', false), ': array'], + '?array' => [new SimpleType('array', true), ': ?array'], + 'boolean' => [new SimpleType('boolean', false), ': bool'], + 'real' => [new SimpleType('real', false), ': float'], + 'double' => [new SimpleType('double', false), ': float'], + 'integer' => [new SimpleType('integer', false), ': int'], + ]; + } +} diff --git a/tests/unit/Framework/MockObject/TypeTest.php b/tests/unit/Framework/MockObject/TypeTest.php new file mode 100644 index 00000000000..b0f1348e780 --- /dev/null +++ b/tests/unit/Framework/MockObject/TypeTest.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +/** + * @covers \PHPUnit\Framework\MockObject\Type + */ +class TypeTest extends TestCase +{ + /** + * @dataProvider valuesToNullableType + */ + public function testTypeMappingFromValue($value, bool $allowsNull, Type $expectedType): void + { + $this->assertEquals($expectedType, Type::fromValue($value, $allowsNull)); + } + + public function valuesToNullableType(): array + { + return [ + '?null' => [null, true, new NullType()], + 'null' => [null, false, new NullType()], + '?integer' => [1, true, new SimpleType('int', true)], + 'integer' => [1, false, new SimpleType('int', false)], + '?boolean' => [true, true, new SimpleType('bool', true)], + 'boolean' => [true, false, new SimpleType('bool', false)], + '?object' => [new \stdClass(), true, new ObjectType(TypeName::fromQualifiedName(\stdClass::class), true)], + 'object' => [new \stdClass(), false, new ObjectType(TypeName::fromQualifiedName(\stdClass::class), false)], + ]; + } + + /** + * @dataProvider namesToTypes + */ + public function testTypeMappingFromName(string $typeName, bool $allowsNull, $expectedType): void + { + $this->assertEquals($expectedType, Type::fromName($typeName, $allowsNull)); + } + + public function namesToTypes(): array + { + return [ + '?void' => ['void', true, new VoidType()], + 'void' => ['void', false, new VoidType()], + '?null' => ['null', true, new NullType()], + 'null' => ['null', true, new NullType()], + '?int' => ['int', true, new SimpleType('int', true)], + '?integer' => ['integer', true, new SimpleType('int', true)], + 'int' => ['int', false, new SimpleType('int', false)], + 'bool' => ['bool', false, new SimpleType('bool', false)], + 'boolean' => ['boolean', false, new SimpleType('bool', false)], + 'object' => ['object', false, new SimpleType('object', false)], + 'real' => ['real', false, new SimpleType('float', false)], + 'double' => ['double', false, new SimpleType('float', false)], + 'float' => ['float', false, new SimpleType('float', false)], + 'string' => ['string', false, new SimpleType('string', false)], + 'array' => ['array', false, new SimpleType('array', false)], + 'resource' => ['resource', false, new SimpleType('resource', false)], + 'resource (closed)' => ['resource (closed)', false, new SimpleType('resource (closed)', false)], + 'unknown type' => ['unknown type', false, new UnknownType()], + '?object' => [\stdClass::class, true, new ObjectType(TypeName::fromQualifiedName(\stdClass::class), true)], + 'classname' => [\stdClass::class, false, new ObjectType(TypeName::fromQualifiedName(\stdClass::class), false)], + ]; + } +} diff --git a/tests/unit/Framework/MockObject/UnknownTypeTest.php b/tests/unit/Framework/MockObject/UnknownTypeTest.php new file mode 100644 index 00000000000..e893b83589b --- /dev/null +++ b/tests/unit/Framework/MockObject/UnknownTypeTest.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +/** + * @covers \PHPUnit\Framework\MockObject\UnknownType + */ +class UnknownTypeTest extends TestCase +{ + /** + * @dataProvider assignableTypes + */ + public function testIsAssignable(Type $assignableType): void + { + $unknownType = new UnknownType(); + $this->assertTrue($unknownType->isAssignable($assignableType)); + } + + public function assignableTypes(): array + { + return [ + [new SimpleType('int', false)], + [new SimpleType('int', true)], + [new VoidType()], + [new ObjectType(TypeName::fromQualifiedName(self::class), false)], + [new ObjectType(TypeName::fromQualifiedName(self::class), true)], + [new UnknownType()], + ]; + } + + public function testAllowsNull(): void + { + $type = new UnknownType(); + $this->assertTrue($type->allowsNull()); + } + + public function testReturnTypeDeclaration(): void + { + $type = new UnknownType(); + $this->assertEquals('', $type->getReturnTypeDeclaration()); + } +} diff --git a/tests/unit/Framework/MockObject/VoidTypeTest.php b/tests/unit/Framework/MockObject/VoidTypeTest.php new file mode 100644 index 00000000000..10e2bac540c --- /dev/null +++ b/tests/unit/Framework/MockObject/VoidTypeTest.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework\MockObject; + +use PHPUnit\Framework\TestCase; + +/** + * @covers \PHPUnit\Framework\MockObject\VoidType + */ +class VoidTypeTest extends TestCase +{ + /** + * @dataProvider assignableTypes + */ + public function testIsAssignable(Type $assignableType): void + { + $void = new VoidType(); + $this->assertTrue($void->isAssignable($assignableType)); + } + + public function assignableTypes(): array + { + return [ + [new VoidType()], + ]; + } + + /** + * @dataProvider notAssignableTypes + */ + public function testIsNotAssignable(Type $assignableType): void + { + $void = new VoidType(); + $this->assertFalse($void->isAssignable($assignableType)); + } + + public function notAssignableTypes(): array + { + return [ + [new SimpleType('int', false)], + [new SimpleType('int', true)], + [new ObjectType(TypeName::fromQualifiedName(self::class), false)], + [new ObjectType(TypeName::fromQualifiedName(self::class), true)], + [new UnknownType()], + ]; + } + + public function testNotAllowNull(): void + { + $type = new VoidType(); + $this->assertFalse($type->allowsNull()); + } + + public function testReturnTypeDeclaration(): void + { + $type = new VoidType(); + $this->assertEquals(': void', $type->getReturnTypeDeclaration()); + } +} From af960abcd73d215ad917ca4a4a5c7219bef1a0b8 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 16:18:07 +0200 Subject: [PATCH 06/11] Added tests for method calls with different letter case --- .../Builder/InvocationMockerTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index d51a53846c6..eaaf4dafec7 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -140,4 +140,42 @@ public function testWillReturnAllowsMatchersForMultipleMethodsWithDifferentRetur $this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration()); $this->assertEquals(1, $mock->methodWithIntReturnTypeDeclaration()); } + + public function testWillReturnValidType(): void + { + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $mock->expects($this->any()) + ->method('methodWithBoolReturnTypeDeclaration') + ->willReturn(true); + + $this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration()); + } + + public function testWillReturnValidTypeForLowercaseCall(): void + { + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $mock->expects($this->any()) + ->method('methodWithBoolReturnTypeDeclaration') + ->willReturn(true); + + $this->assertEquals(true, $mock->methodwithboolreturntypedeclaration()); + } + + public function testWillReturnValidTypeForLowercaseMethod(): void + { + $mock = $this->getMockBuilder(ClassWithAllPossibleReturnTypes::class) + ->getMock(); + + $mock->expects($this->any()) + ->method('methodwithboolreturntypedeclaration') + ->willReturn(true); + + $this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration()); + } + + } From f427c037e97c57eb17399afe36b1d2bda444a490 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 16:30:56 +0200 Subject: [PATCH 07/11] cleanup --- src/Framework/MockObject/Generator.php | 4 ++-- src/Framework/MockObject/Matcher/MethodName.php | 2 +- src/Framework/MockObject/MockMethod.php | 6 ++---- .../Framework/MockObject/Builder/InvocationMockerTest.php | 2 -- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Framework/MockObject/Generator.php b/src/Framework/MockObject/Generator.php index 982a708783e..427f4dfcfb6 100644 --- a/src/Framework/MockObject/Generator.php +++ b/src/Framework/MockObject/Generator.php @@ -262,8 +262,8 @@ public function getMockForTrait(string $traitName, array $arguments = [], string ] ); - $mockClass = new MockTrait($classTemplate->render(), $className['className']); - $mockClass->generate(); + $mockTrait = new MockTrait($classTemplate->render(), $className['className']); + $mockTrait->generate(); return $this->getMockForAbstractClass($className['className'], $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $mockedMethods, $cloneArguments); } diff --git a/src/Framework/MockObject/Matcher/MethodName.php b/src/Framework/MockObject/Matcher/MethodName.php index 8d066e3e7bf..5c87d448fde 100644 --- a/src/Framework/MockObject/Matcher/MethodName.php +++ b/src/Framework/MockObject/Matcher/MethodName.php @@ -60,7 +60,7 @@ public function toString(): string */ public function matches(BaseInvocation $invocation): bool { - return $this->constraint->evaluate($invocation->getMethodName(), '', true); + return $this->matchesName($invocation->getMethodName()); } public function matchesName(string $methodName): bool diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 22cde1438e1..362e6d0a05a 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -84,8 +84,6 @@ final class MockMethod */ public static function fromReflection(\ReflectionMethod $method, bool $callOriginalMethod, bool $cloneArguments): self { - $className = $method->getDeclaringClass()->getName(); - if ($method->isPrivate()) { $modifier = 'private'; } elseif ($method->isProtected()) { @@ -114,7 +112,7 @@ public static function fromReflection(\ReflectionMethod $method, bool $callOrigi } return new self( - $className, + $method->getDeclaringClass()->getName(), $method->getName(), $cloneArguments, $modifier, @@ -175,7 +173,7 @@ public function generateCode(): string { if ($this->static) { $templateFile = 'mocked_static_method.tpl'; - } elseif ($this->returnType->getReturnTypeDeclaration() === ': void') { + } elseif ($this->returnType instanceof VoidType) { $templateFile = \sprintf( '%s_method_void.tpl', $this->callOriginalMethod ? 'proxied' : 'mocked' diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index eaaf4dafec7..89816346887 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -176,6 +176,4 @@ public function testWillReturnValidTypeForLowercaseMethod(): void $this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration()); } - - } From 5ba91cb744c5483f036e1a998a3abee5fe0a0bcc Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 16:37:31 +0200 Subject: [PATCH 08/11] Added regression test for #3602 --- .../MockObject/Builder/InvocationMockerTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index 89816346887..62ece6f66d3 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -176,4 +176,18 @@ public function testWillReturnValidTypeForLowercaseMethod(): void $this->assertEquals(true, $mock->methodWithBoolReturnTypeDeclaration()); } + + /** + * @see https://github.com/sebastianbergmann/phpunit/issues/3602 + */ + public function testWillReturnFailsWhenTryingToReturnValueFromVoidMethod() : void + { + /** @var ClassWithAllPossibleReturnTypes|\PHPUnit\Framework\MockObject\MockObject $out */ + $out = $this->createMock(ClassWithAllPossibleReturnTypes::class); + $method = $out->method('methodWithVoidReturnTypeDeclaration'); + + $this->expectException(IncompatibleReturnValueException::class); + $this->expectExceptionMessage('Method methodWithVoidReturnTypeDeclaration may not return value of type boolean'); + $method->willReturn(true); + } } From 229077e7acdec1c6373359cd6ad447ddd28338d2 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 16:51:31 +0200 Subject: [PATCH 09/11] CS-fixed --- .../Framework/MockObject/Builder/InvocationMockerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php index 62ece6f66d3..0e8f31e212f 100644 --- a/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php +++ b/tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php @@ -180,10 +180,10 @@ public function testWillReturnValidTypeForLowercaseMethod(): void /** * @see https://github.com/sebastianbergmann/phpunit/issues/3602 */ - public function testWillReturnFailsWhenTryingToReturnValueFromVoidMethod() : void + public function testWillReturnFailsWhenTryingToReturnValueFromVoidMethod(): void { /** @var ClassWithAllPossibleReturnTypes|\PHPUnit\Framework\MockObject\MockObject $out */ - $out = $this->createMock(ClassWithAllPossibleReturnTypes::class); + $out = $this->createMock(ClassWithAllPossibleReturnTypes::class); $method = $out->method('methodWithVoidReturnTypeDeclaration'); $this->expectException(IncompatibleReturnValueException::class); From abcc77526e0a82898cf8582f5bd4981a4c716b39 Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 17:03:50 +0200 Subject: [PATCH 10/11] fixed errors discovered my psalm shepard --- src/Framework/MockObject/ConfigurableMethod.php | 2 +- src/Framework/MockObject/InvocationMocker.php | 2 +- src/Framework/MockObject/MockMethod.php | 2 +- src/Framework/MockObject/Type.php | 2 +- src/Framework/MockObject/TypeName.php | 6 +++--- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Framework/MockObject/ConfigurableMethod.php b/src/Framework/MockObject/ConfigurableMethod.php index 6e5de00ac05..7e7e21a4c6e 100644 --- a/src/Framework/MockObject/ConfigurableMethod.php +++ b/src/Framework/MockObject/ConfigurableMethod.php @@ -37,7 +37,7 @@ public function getName(): string public function mayReturn($value): bool { - if (isNull($value) && $this->returnType->allowsNull()) { + if ($value === null && $this->returnType->allowsNull()) { return true; } diff --git a/src/Framework/MockObject/InvocationMocker.php b/src/Framework/MockObject/InvocationMocker.php index b623bd3e15e..9b40c5a839a 100644 --- a/src/Framework/MockObject/InvocationMocker.php +++ b/src/Framework/MockObject/InvocationMocker.php @@ -34,7 +34,7 @@ final class InvocationMocker implements MatcherCollection, Invokable, NamespaceM private $builderMap = []; /** - * @var string[] + * @var ConfigurableMethod[] */ private $configurableMethods; diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 362e6d0a05a..45086ba48ef 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -338,7 +338,7 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for return \implode(', ', $parameters); } - private static function deriveReturnType(\ReflectionMethod $method): ?Type + private static function deriveReturnType(\ReflectionMethod $method): Type { $returnType = $method->getReturnType(); diff --git a/src/Framework/MockObject/Type.php b/src/Framework/MockObject/Type.php index 71e8b8863fc..6c52a84787a 100644 --- a/src/Framework/MockObject/Type.php +++ b/src/Framework/MockObject/Type.php @@ -26,7 +26,7 @@ public static function fromValue($value, bool $allowsNull): self } } - public static function fromName(?string $typeName, bool $allowsNull): self + public static function fromName(string $typeName, bool $allowsNull): self { switch (\mb_strtolower($typeName)) { case 'null': diff --git a/src/Framework/MockObject/TypeName.php b/src/Framework/MockObject/TypeName.php index b5377d923f6..e2a1337c362 100644 --- a/src/Framework/MockObject/TypeName.php +++ b/src/Framework/MockObject/TypeName.php @@ -67,9 +67,9 @@ public function getSimpleName(): string public function getQualifiedName(): string { - return $this->isNamespaced() - ? $this->namespaceName . '\\' . $this->simpleName - : $this->simpleName; + return $this->namespaceName === null + ? $this->simpleName + : $this->namespaceName . '\\' . $this->simpleName; } public function isNamespaced(): bool From 7a0a7a9938d0c93c1f2cd4fe659d422244ead70b Mon Sep 17 00:00:00 2001 From: Michel Hartmann Date: Tue, 30 Apr 2019 17:31:59 +0200 Subject: [PATCH 11/11] mark new classes as final --- src/Framework/MockObject/ConfigurableMethod.php | 2 +- src/Framework/MockObject/MockClass.php | 2 +- src/Framework/MockObject/MockTrait.php | 2 +- src/Framework/MockObject/NullType.php | 2 +- src/Framework/MockObject/ObjectType.php | 2 +- src/Framework/MockObject/SimpleType.php | 2 +- src/Framework/MockObject/UnknownType.php | 2 +- src/Framework/MockObject/VoidType.php | 2 +- tests/unit/Framework/MockObject/ConfigurableMethodTest.php | 2 +- tests/unit/Framework/MockObject/ConfigurableMethodsTest.php | 2 +- tests/unit/Framework/MockObject/MockClassTest.php | 2 +- tests/unit/Framework/MockObject/MockTraitTest.php | 2 +- tests/unit/Framework/MockObject/NullTypeTest.php | 2 +- tests/unit/Framework/MockObject/ObjectTypeTest.php | 2 +- tests/unit/Framework/MockObject/SimpleTypeTest.php | 2 +- tests/unit/Framework/MockObject/TypeNameTest.php | 2 +- tests/unit/Framework/MockObject/TypeTest.php | 2 +- tests/unit/Framework/MockObject/UnknownTypeTest.php | 2 +- tests/unit/Framework/MockObject/VoidTypeTest.php | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Framework/MockObject/ConfigurableMethod.php b/src/Framework/MockObject/ConfigurableMethod.php index 7e7e21a4c6e..9e979452ad0 100644 --- a/src/Framework/MockObject/ConfigurableMethod.php +++ b/src/Framework/MockObject/ConfigurableMethod.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class ConfigurableMethod +final class ConfigurableMethod { /** * @var string diff --git a/src/Framework/MockObject/MockClass.php b/src/Framework/MockObject/MockClass.php index a3606fcdf8c..92f29afe622 100644 --- a/src/Framework/MockObject/MockClass.php +++ b/src/Framework/MockObject/MockClass.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class MockClass implements MockType +final class MockClass implements MockType { /** * @var string diff --git a/src/Framework/MockObject/MockTrait.php b/src/Framework/MockObject/MockTrait.php index d9d58e0ba89..3ced88991f6 100644 --- a/src/Framework/MockObject/MockTrait.php +++ b/src/Framework/MockObject/MockTrait.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class MockTrait implements MockType +final class MockTrait implements MockType { /** * @var string diff --git a/src/Framework/MockObject/NullType.php b/src/Framework/MockObject/NullType.php index c3490b80d66..649d799ec6c 100644 --- a/src/Framework/MockObject/NullType.php +++ b/src/Framework/MockObject/NullType.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class NullType extends Type +final class NullType extends Type { public function isAssignable(Type $other): bool { diff --git a/src/Framework/MockObject/ObjectType.php b/src/Framework/MockObject/ObjectType.php index 80446efa314..7e006bf87cd 100644 --- a/src/Framework/MockObject/ObjectType.php +++ b/src/Framework/MockObject/ObjectType.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class ObjectType extends Type +final class ObjectType extends Type { /** * @var TypeName diff --git a/src/Framework/MockObject/SimpleType.php b/src/Framework/MockObject/SimpleType.php index 76e438cc6ce..4ec0a8ba03f 100644 --- a/src/Framework/MockObject/SimpleType.php +++ b/src/Framework/MockObject/SimpleType.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class SimpleType extends Type +final class SimpleType extends Type { /** * @var string diff --git a/src/Framework/MockObject/UnknownType.php b/src/Framework/MockObject/UnknownType.php index 0e09de56010..533f45fc893 100644 --- a/src/Framework/MockObject/UnknownType.php +++ b/src/Framework/MockObject/UnknownType.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class UnknownType extends Type +final class UnknownType extends Type { public function isAssignable(Type $other): bool { diff --git a/src/Framework/MockObject/VoidType.php b/src/Framework/MockObject/VoidType.php index 599029b2d9a..969bd5e3321 100644 --- a/src/Framework/MockObject/VoidType.php +++ b/src/Framework/MockObject/VoidType.php @@ -12,7 +12,7 @@ /** * @internal This class is not covered by the backward compatibility promise for PHPUnit */ -class VoidType extends Type +final class VoidType extends Type { public function isAssignable(Type $other): bool { diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php index 39c9db62e5d..1663c8f7b20 100644 --- a/tests/unit/Framework/MockObject/ConfigurableMethodTest.php +++ b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php @@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase; -class ConfigurableMethodTest extends TestCase +final class ConfigurableMethodTest extends TestCase { public function testMethodMayReturnAssignableValue(): void { diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php index e87458653a3..7b34e5aa7c8 100644 --- a/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php +++ b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.php @@ -14,7 +14,7 @@ use PHPUnit\TestFixture\MockObject\ClassBUsingConfigurableMethods; use PHPUnit\TestFixture\MockObject\ReinitializeConfigurableMethods; -class ConfigurableMethodsTest extends TestCase +final class ConfigurableMethodsTest extends TestCase { public function testTwoClassesUsingConfigurableMethodsDontInterfere(): void { diff --git a/tests/unit/Framework/MockObject/MockClassTest.php b/tests/unit/Framework/MockObject/MockClassTest.php index a1ec41f8bae..4f7c6ec8825 100644 --- a/tests/unit/Framework/MockObject/MockClassTest.php +++ b/tests/unit/Framework/MockObject/MockClassTest.php @@ -12,7 +12,7 @@ use PHPUnit\Framework\TestCase; use PHPUnit\TestFixture\MockObject\MockClassWithConfigurableMethods; -class MockClassTest extends TestCase +final class MockClassTest extends TestCase { public function testGenerateClassFromSource(): void { diff --git a/tests/unit/Framework/MockObject/MockTraitTest.php b/tests/unit/Framework/MockObject/MockTraitTest.php index a0efc1aaff8..a314678eb01 100644 --- a/tests/unit/Framework/MockObject/MockTraitTest.php +++ b/tests/unit/Framework/MockObject/MockTraitTest.php @@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase; -class MockTraitTest extends TestCase +final class MockTraitTest extends TestCase { public function testGenerateClassFromSource(): void { diff --git a/tests/unit/Framework/MockObject/NullTypeTest.php b/tests/unit/Framework/MockObject/NullTypeTest.php index b742110c7a4..43498db0638 100644 --- a/tests/unit/Framework/MockObject/NullTypeTest.php +++ b/tests/unit/Framework/MockObject/NullTypeTest.php @@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase; -class NullTypeTest extends TestCase +final class NullTypeTest extends TestCase { /** * @dataProvider assignableTypes diff --git a/tests/unit/Framework/MockObject/ObjectTypeTest.php b/tests/unit/Framework/MockObject/ObjectTypeTest.php index 0215c02bb4e..9ca8a025b70 100644 --- a/tests/unit/Framework/MockObject/ObjectTypeTest.php +++ b/tests/unit/Framework/MockObject/ObjectTypeTest.php @@ -16,7 +16,7 @@ /** * @covers \PHPUnit\Framework\MockObject\ObjectType */ -class ObjectTypeTest extends TestCase +final class ObjectTypeTest extends TestCase { /** * @var ObjectType diff --git a/tests/unit/Framework/MockObject/SimpleTypeTest.php b/tests/unit/Framework/MockObject/SimpleTypeTest.php index 5927a2b97b5..3033dac9290 100644 --- a/tests/unit/Framework/MockObject/SimpleTypeTest.php +++ b/tests/unit/Framework/MockObject/SimpleTypeTest.php @@ -14,7 +14,7 @@ /** * @covers \PHPUnit\Framework\MockObject\SimpleType */ -class SimpleTypeTest extends TestCase +final class SimpleTypeTest extends TestCase { /** * @dataProvider assignablePairs diff --git a/tests/unit/Framework/MockObject/TypeNameTest.php b/tests/unit/Framework/MockObject/TypeNameTest.php index d9df038f0de..fff980dab9b 100644 --- a/tests/unit/Framework/MockObject/TypeNameTest.php +++ b/tests/unit/Framework/MockObject/TypeNameTest.php @@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase; -class TypeNameTest extends TestCase +final class TypeNameTest extends TestCase { public function testFromReflection(): void { diff --git a/tests/unit/Framework/MockObject/TypeTest.php b/tests/unit/Framework/MockObject/TypeTest.php index b0f1348e780..fac089f73dd 100644 --- a/tests/unit/Framework/MockObject/TypeTest.php +++ b/tests/unit/Framework/MockObject/TypeTest.php @@ -14,7 +14,7 @@ /** * @covers \PHPUnit\Framework\MockObject\Type */ -class TypeTest extends TestCase +final class TypeTest extends TestCase { /** * @dataProvider valuesToNullableType diff --git a/tests/unit/Framework/MockObject/UnknownTypeTest.php b/tests/unit/Framework/MockObject/UnknownTypeTest.php index e893b83589b..96c94e3801d 100644 --- a/tests/unit/Framework/MockObject/UnknownTypeTest.php +++ b/tests/unit/Framework/MockObject/UnknownTypeTest.php @@ -14,7 +14,7 @@ /** * @covers \PHPUnit\Framework\MockObject\UnknownType */ -class UnknownTypeTest extends TestCase +final class UnknownTypeTest extends TestCase { /** * @dataProvider assignableTypes diff --git a/tests/unit/Framework/MockObject/VoidTypeTest.php b/tests/unit/Framework/MockObject/VoidTypeTest.php index 10e2bac540c..5e4479010fe 100644 --- a/tests/unit/Framework/MockObject/VoidTypeTest.php +++ b/tests/unit/Framework/MockObject/VoidTypeTest.php @@ -14,7 +14,7 @@ /** * @covers \PHPUnit\Framework\MockObject\VoidType */ -class VoidTypeTest extends TestCase +final class VoidTypeTest extends TestCase { /** * @dataProvider assignableTypes