diff --git a/src/Framework/MockObject/Builder/InvocationMocker.php b/src/Framework/MockObject/Builder/InvocationMocker.php index 9186c26faa6..983b08d5491 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\MockObject\ConfigurableMethod; +use PHPUnit\Framework\MockObject\IncompatibleReturnValueException; 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,14 @@ 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 +237,39 @@ private function canDefineParameters(): void ); } } + + private function getConfiguredMethod(): ?ConfigurableMethod + { + $configuredMethod = null; + + foreach ($this->configurableMethods as $configurableMethod) { + 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( + '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..9e979452ad0 --- /dev/null +++ b/src/Framework/MockObject/ConfigurableMethod.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; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class ConfigurableMethod +{ + /** + * @var string + */ + private $name; + + /** + * @var Type + */ + private $returnType; + + public function __construct(string $name, Type $returnType) + { + $this->name = $name; + $this->returnType = $returnType; + } + + public function getName(): string + { + return $this->name; + } + + public function mayReturn($value): bool + { + if ($value === null && $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..ac286ec5856 --- /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; + +/** + * @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): void + { + if (isset(static::$__phpunit_configurableMethods)) { + 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/src/Framework/MockObject/Exception/IncompatibleReturnValueException.php b/src/Framework/MockObject/Exception/IncompatibleReturnValueException.php new file mode 100644 index 00000000000..f1ceb1debfa --- /dev/null +++ b/src/Framework/MockObject/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\MockObject; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class IncompatibleReturnValueException extends \PHPUnit\Framework\Exception implements Exception +{ +} diff --git a/src/Framework/MockObject/Generator.php b/src/Framework/MockObject/Generator.php index 20fee252bb1..427f4dfcfb6 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'] - ); + $mockTrait = new MockTrait($classTemplate->render(), $className['className']); + $mockTrait->generate(); 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(MockType $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->generate(); 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..b7691d4c880 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..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; @@ -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..5c87d448fde 100644 --- a/src/Framework/MockObject/Matcher/MethodName.php +++ b/src/Framework/MockObject/Matcher/MethodName.php @@ -60,6 +60,11 @@ 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 + { + return $this->constraint->evaluate($methodName, '', true); } } diff --git a/src/Framework/MockObject/MockClass.php b/src/Framework/MockObject/MockClass.php new file mode 100644 index 00000000000..92f29afe622 --- /dev/null +++ b/src/Framework/MockObject/MockClass.php @@ -0,0 +1,53 @@ + + * + * 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 MockClass implements MockType +{ + /** + * @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 generate(): string + { + if (!\class_exists($this->mockName, false)) { + eval($this->classCode); + \call_user_func([$this->mockName, '__phpunit_initConfigurableMethods'], ...$this->configurableMethods); + } + + return $this->mockName; + } + + public function getClassCode(): string + { + return $this->classCode; + } +} diff --git a/src/Framework/MockObject/MockMethod.php b/src/Framework/MockObject/MockMethod.php index 793cbe8d2ff..45086ba48ef 100644 --- a/src/Framework/MockObject/MockMethod.php +++ b/src/Framework/MockObject/MockMethod.php @@ -50,7 +50,7 @@ final class MockMethod private $argumentsForCall; /** - * @var string + * @var Type */ private $returnType; @@ -102,12 +102,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) && @@ -124,7 +118,7 @@ public static function fromReflection(\ReflectionMethod $method, bool $callOrigi $modifier, self::getMethodParameters($method), self::getMethodParameters($method, true), - $returnType, + self::deriveReturnType($method), $reference, $callOriginalMethod, $method->isStatic(), @@ -142,7 +136,7 @@ public static function fromName(string $fullClassName, string $methodName, bool 'public', '', '', - '', + new UnknownType(), '', false, false, @@ -151,7 +145,7 @@ 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; @@ -179,7 +173,7 @@ public function generateCode(): string { if ($this->static) { $templateFile = 'mocked_static_method.tpl'; - } elseif ($this->returnType === 'void') { + } elseif ($this->returnType instanceof VoidType) { $templateFile = \sprintf( '%s_method_void.tpl', $this->callOriginalMethod ? 'proxied' : 'mocked' @@ -191,39 +185,6 @@ 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) { @@ -241,23 +202,27 @@ public function generateCode(): string $template->setVar( [ - 'arguments_decl' => $this->argumentsForDeclaration, - 'arguments_call' => $this->argumentsForCall, - 'return_delim' => $returnType ? ': ' : '', - 'return_type' => $this->allowsReturnNull ? '?' . $returnType : $returnType, - '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_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, ] ); 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; @@ -372,4 +337,38 @@ 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()); + } } diff --git a/src/Framework/MockObject/MockTrait.php b/src/Framework/MockObject/MockTrait.php new file mode 100644 index 00000000000..3ced88991f6 --- /dev/null +++ b/src/Framework/MockObject/MockTrait.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; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class MockTrait implements MockType +{ + /** + * @var string + */ + private $classCode; + + /** + * @var string + */ + private $mockName; + + public function __construct(string $classCode, string $mockName) + { + $this->classCode = $classCode; + $this->mockName = $mockName; + } + + public function generate(): string + { + if (!\class_exists($this->mockName, false)) { + eval($this->classCode); + } + + return $this->mockName; + } + + public function getClassCode(): string + { + 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/NullType.php b/src/Framework/MockObject/NullType.php new file mode 100644 index 00000000000..649d799ec6c --- /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 + */ +final 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 new file mode 100644 index 00000000000..7e006bf87cd --- /dev/null +++ b/src/Framework/MockObject/ObjectType.php @@ -0,0 +1,61 @@ + + * + * 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 ObjectType extends Type +{ + /** + * @var TypeName + */ + private $className; + + /** + * @var bool + */ + private $allowsNull; + + public function __construct(TypeName $className, bool $allowsNull) + { + $this->className = $className; + $this->allowsNull = $allowsNull; + } + + public function isAssignable(Type $other): bool + { + if ($this->allowsNull && $other instanceof NullType) { + 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..4ec0a8ba03f --- /dev/null +++ b/src/Framework/MockObject/SimpleType.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; + +/** + * @internal This class is not covered by the backward compatibility promise for PHPUnit + */ +final class SimpleType extends Type +{ + /** + * @var string + */ + private $name; + + /** + * @var bool + */ + private $allowsNull; + + public function __construct(string $name, bool $nullable) + { + $this->name = $this->normalize($name); + $this->allowsNull = $nullable; + } + + public function isAssignable(Type $other): bool + { + if ($this->allowsNull && $other instanceof NullType) { + 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..6c52a84787a --- /dev/null +++ b/src/Framework/MockObject/Type.php @@ -0,0 +1,61 @@ + + * + * 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 + */ +abstract class Type +{ + public static function fromValue($value, bool $allowsNull): self + { + $type = \gettype($value); + + switch ($type) { + case 'object': + return new ObjectType(TypeName::fromQualifiedName(\get_class($value)), $allowsNull); + default: + return self::fromName($type, $allowsNull); + } + } + + public static function fromName(string $typeName, bool $allowsNull): self + { + switch (\mb_strtolower($typeName)) { + case 'null': + return new NullType(); + case 'unknown type': + return new UnknownType(); + case 'void': + return new VoidType(); + 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(self $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..e2a1337c362 --- /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->namespaceName === null + ? $this->simpleName + : $this->namespaceName . '\\' . $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..533f45fc893 --- /dev/null +++ b/src/Framework/MockObject/UnknownType.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 + */ +final class UnknownType extends Type +{ + public function isAssignable(Type $other): bool + { + return true; + } + + public function getReturnTypeDeclaration(): string + { + return ''; + } + + public function allowsNull(): bool + { + return true; + } +} diff --git a/src/Framework/MockObject/VoidType.php b/src/Framework/MockObject/VoidType.php new file mode 100644 index 00000000000..969bd5e3321 --- /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 + */ +final 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/_files/mock-object/ChildClass.php b/tests/_files/mock-object/ChildClass.php new file mode 100644 index 00000000000..ac981e0ec12 --- /dev/null +++ b/tests/_files/mock-object/ChildClass.php @@ -0,0 +1,14 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\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..ac9e1aae0c2 --- /dev/null +++ b/tests/_files/mock-object/ClassAUsingConfigurableMethods.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 ClassAUsingConfigurableMethods +{ + use ConfigurableMethods; + + 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 new file mode 100644 index 00000000000..4f38044bd78 --- /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\TestFixture\MockObject; + +use PHPUnit\Framework\MockObject\ConfigurableMethods; + +class ClassBUsingConfigurableMethods +{ + use ConfigurableMethods; + + 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 new file mode 100644 index 00000000000..c3aefaa9e6e --- /dev/null +++ b/tests/_files/mock-object/ClassWithoutParentButParentReturnType.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; + +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 new file mode 100644 index 00000000000..1168c69eff5 --- /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\TestFixture\MockObject; + +class ParentClass +{ + public function foo(): void + { + } +} 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/_files/namespace/someNamespaceA/NamespacedClass.php b/tests/_files/namespace/someNamespaceA/NamespacedClass.php new file mode 100644 index 00000000000..b035c17b905 --- /dev/null +++ b/tests/_files/namespace/someNamespaceA/NamespacedClass.php @@ -0,0 +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 +{ +} diff --git a/tests/_files/namespace/someNamespaceB/NamespacedClass.php b/tests/_files/namespace/someNamespaceB/NamespacedClass.php new file mode 100644 index 00000000000..e2eb0f22d0b --- /dev/null +++ b/tests/_files/namespace/someNamespaceB/NamespacedClass.php @@ -0,0 +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 +{ +} diff --git a/tests/end-to-end/mock-objects/generator/232.phpt b/tests/end-to-end/mock-objects/generator/232.phpt index 34a13c4e6bc..27664298d47 100644 --- a/tests/end-to-end/mock-objects/generator/232.phpt +++ b/tests/end-to-end/mock-objects/generator/232.phpt @@ -50,14 +50,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 = ['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..0e8f31e212f 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 +35,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 +50,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 +65,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'; @@ -82,11 +85,109 @@ public function testWillFailWhenTryingToPerformExpectationUnconfigurableMethod() $matcherCollection = $this->createMock(MatcherCollection::class); $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); + } + + 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()); + } + + 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()); + } + + /** + * @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); + } } diff --git a/tests/unit/Framework/MockObject/ConfigurableMethodTest.php b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php new file mode 100644 index 00000000000..1663c8f7b20 --- /dev/null +++ b/tests/unit/Framework/MockObject/ConfigurableMethodTest.php @@ -0,0 +1,33 @@ + + * + * 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; + +final class ConfigurableMethodTest extends TestCase +{ + public function testMethodMayReturnAssignableValue(): void + { + $assignableType = $this->createMock(Type::class); + $assignableType->method('isAssignable') + ->willReturn(true); + $configurable = new ConfigurableMethod('foo', $assignableType); + $this->assertTrue($configurable->mayReturn('everything-is-valid')); + } + + public function testMethodMayNotReturnUnassignableValue(): void + { + $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..7b34e5aa7c8 --- /dev/null +++ b/tests/unit/Framework/MockObject/ConfigurableMethodsTest.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; +use PHPUnit\TestFixture\MockObject\ClassAUsingConfigurableMethods; +use PHPUnit\TestFixture\MockObject\ClassBUsingConfigurableMethods; +use PHPUnit\TestFixture\MockObject\ReinitializeConfigurableMethods; + +final class ConfigurableMethodsTest extends TestCase +{ + public function testTwoClassesUsingConfigurableMethodsDontInterfere(): void + { + $configurableMethodsA = [new ConfigurableMethod('foo', SimpleType::fromValue('boolean', false))]; + $configurableMethodsB = []; + ClassAUsingConfigurableMethods::__phpunit_initConfigurableMethods(...$configurableMethodsA); + ClassBUsingConfigurableMethods::__phpunit_initConfigurableMethods(...$configurableMethodsB); + + $this->assertSame($configurableMethodsA, ClassAUsingConfigurableMethods::getConfigurableMethods()); + $this->assertSame($configurableMethodsB, ClassBUsingConfigurableMethods::getConfigurableMethods()); + } + + public function testConfigurableMethodsAreImmutable(): void + { + ReinitializeConfigurableMethods::__phpunit_initConfigurableMethods(); + $this->expectException(ConfigurableMethodsAlreadyInitializedException::class); + ReinitializeConfigurableMethods::__phpunit_initConfigurableMethods(); + } +} diff --git a/tests/unit/Framework/MockObject/MockClassTest.php b/tests/unit/Framework/MockObject/MockClassTest.php new file mode 100644 index 00000000000..4f7c6ec8825 --- /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; + +final 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 51ee79c2d19..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 @@ -25,7 +26,7 @@ public function testGetNameReturnsMethodName(): void '', '', '', - '', + new UnknownType(), '', false, false, @@ -37,21 +38,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/MockTraitTest.php b/tests/unit/Framework/MockObject/MockTraitTest.php new file mode 100644 index 00000000000..a314678eb01 --- /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; + +final 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/NullTypeTest.php b/tests/unit/Framework/MockObject/NullTypeTest.php new file mode 100644 index 00000000000..43498db0638 --- /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; + +final 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 new file mode 100644 index 00000000000..9ca8a025b70 --- /dev/null +++ b/tests/unit/Framework/MockObject/ObjectTypeTest.php @@ -0,0 +1,112 @@ + + * + * 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; + +/** + * @covers \PHPUnit\Framework\MockObject\ObjectType + */ +final class ObjectTypeTest extends TestCase +{ + /** + * @var ObjectType + */ + private $childClass; + + /** + * @var ObjectType + */ + private $parentClass; + + protected function setUp(): void + { + parent::setUp(); + $this->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(): void + { + $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(): void + { + $someClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + true + ); + $this->assertTrue($someClass->isAssignable(Type::fromValue(null, true))); + } + + public function testNullIsNotAssignableToNotNullableType(): void + { + $someClass = new ObjectType( + TypeName::fromQualifiedName(ParentClass::class), + false + ); + $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..3033dac9290 --- /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 + */ +final 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/TypeNameTest.php b/tests/unit/Framework/MockObject/TypeNameTest.php new file mode 100644 index 00000000000..fff980dab9b --- /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; + +final 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()); + } +} diff --git a/tests/unit/Framework/MockObject/TypeTest.php b/tests/unit/Framework/MockObject/TypeTest.php new file mode 100644 index 00000000000..fac089f73dd --- /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 + */ +final 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..96c94e3801d --- /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 + */ +final 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..5e4479010fe --- /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 + */ +final 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()); + } +}