Skip to content

Commit

Permalink
Closes #4929
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 12, 2022
1 parent d62a25e commit 658d8de
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ChangeLog-9.5.md
Expand Up @@ -2,6 +2,12 @@

All notable changes of the PHPUnit 9.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [9.5.19] - 2022-MM-DD

### Fixed

* [#4929](https://github.com/sebastianbergmann/phpunit/issues/4929): Test Double code generator does not handle new expressions inside parameter default values

## [9.5.18] - 2022-03-08

### Fixed
Expand Down Expand Up @@ -142,6 +148,7 @@ All notable changes of the PHPUnit 9.5 release series are documented in this fil

* [#4535](https://github.com/sebastianbergmann/phpunit/issues/4535): `getMockFromWsdl()` does not handle methods that do not have parameters correctly

[9.5.19]: https://github.com/sebastianbergmann/phpunit/compare/9.5.18...9.5
[9.5.18]: https://github.com/sebastianbergmann/phpunit/compare/9.5.17...9.5.18
[9.5.17]: https://github.com/sebastianbergmann/phpunit/compare/9.5.16...9.5.17
[9.5.16]: https://github.com/sebastianbergmann/phpunit/compare/dc738383c519243b0a967f63943a848d3fd861aa...9.5.16
Expand Down
25 changes: 24 additions & 1 deletion src/Framework/MockObject/MockMethod.php
Expand Up @@ -10,11 +10,16 @@
namespace PHPUnit\Framework\MockObject;

use const DIRECTORY_SEPARATOR;
use function explode;
use function implode;
use function is_object;
use function is_string;
use function preg_match;
use function preg_replace;
use function sprintf;
use function strlen;
use function strpos;
use function substr;
use function substr_count;
use function trim;
use function var_export;
Expand Down Expand Up @@ -369,7 +374,25 @@ private static function getMethodParametersForCall(ReflectionMethod $method): st
private static function exportDefaultValue(ReflectionParameter $parameter): string
{
try {
return (string) var_export($parameter->getDefaultValue(), true);
$defaultValue = $parameter->getDefaultValue();

if (!is_object($defaultValue)) {
return (string) var_export($defaultValue, true);
}

$parameterAsString = $parameter->__toString();

return (string) explode(
' = ',
substr(
substr(
$parameterAsString,
strpos($parameterAsString, '<optional> ') + strlen('<optional> ')
),
0,
-2
)
)[1];
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
Expand Down
@@ -0,0 +1,51 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/4929
--FILE--
<?php declare(strict_types=1);
class Foo
{
}

class Bar
{
public function method(Foo $foo = new Foo(1, 2, 3))
{
}
}

require_once __DIR__ . '/../../../bootstrap.php';

$class = new ReflectionClass(Bar::class);

$mockMethod = \PHPUnit\Framework\MockObject\MockMethod::fromReflection(
$class->getMethod('method'),
false,
false
);

$code = $mockMethod->generateCode();

print $code;
--EXPECT--

public function method(Foo $foo = new \Foo(1, 2, 3))
{
$__phpunit_arguments = [$foo];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
}
}

$__phpunit_result = $this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'Bar', 'method', $__phpunit_arguments, '', $this, false
)
);

return $__phpunit_result;
}

0 comments on commit 658d8de

Please sign in to comment.