Skip to content

Commit

Permalink
Closes #4750
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Aug 1, 2021
1 parent 191768c commit 2f2d90a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 45 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-9.5.md
Original file line number Diff line number Diff line change
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.9] - 2021-MM-DD

### Fixed

* [#4750](https://github.com/sebastianbergmann/phpunit/issues/4750): Automatic return value generation leads to invalid (and superfluous) test double code generation when a stubbed method returns `*|false`

## [9.5.8] - 2021-07-31

### Fixed
Expand Down Expand Up @@ -73,6 +79,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.9]: https://github.com/sebastianbergmann/phpunit/compare/9.5.8...9.5
[9.5.8]: https://github.com/sebastianbergmann/phpunit/compare/9.5.7...9.5.8
[9.5.7]: https://github.com/sebastianbergmann/phpunit/compare/9.5.6...9.5.7
[9.5.6]: https://github.com/sebastianbergmann/phpunit/compare/9.5.5...9.5.6
Expand Down
120 changes: 75 additions & 45 deletions src/Framework/MockObject/Invocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use function array_map;
use function explode;
use function get_class;
use function implode;
use function is_object;
use function sprintf;
Expand All @@ -23,6 +22,7 @@
use PHPUnit\Util\Type;
use SebastianBergmann\Exporter\Exporter;
use stdClass;
use Throwable;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
Expand Down Expand Up @@ -117,69 +117,99 @@ public function getParameters(): array
public function generateReturnValue()
{
if ($this->isReturnTypeNullable || $this->proxiedCall) {
return;
return null;
}

$returnType = $this->returnType;

if (strpos($returnType, '|') !== false) {
$types = explode('|', $returnType);
$returnType = $types[0];
$union = false;

foreach ($types as $type) {
if ($type === 'null') {
return;
}
}
if (strpos($this->returnType, '|') !== false) {
$types = explode('|', $this->returnType);
$union = true;
} else {
$types = [$this->returnType];
}

switch (strtolower($returnType)) {
case '':
case 'void':
return;
$types = array_map('strtolower', $types);

case 'string':
return '';
if (in_array('', $types, true) ||
in_array('null', $types, true) ||
in_array('mixed', $types, true) ||
in_array('void', $types, true)) {
return null;
}

case 'float':
return 0.0;
if (in_array('false', $types, true) ||
in_array('bool', $types, true)) {
return false;
}

case 'int':
return 0;
if (in_array('float', $types, true)) {
return 0.0;
}

case 'bool':
case 'false':
return false;
if (in_array('int', $types, true)) {
return 0;
}

case 'array':
return [];
if (in_array('string', $types, true)) {
return '';
}

case 'static':
return (new Instantiator)->instantiate(get_class($this->object));
if (in_array('array', $types, true)) {
return [];
}

case 'object':
return new stdClass;
if (in_array('static', $types, true)) {
try {
return (new Instantiator)->instantiate($this->object::class);
} catch (Throwable $t) {
throw new RuntimeException(
$t->getMessage(),
(int) $t->getCode(),
$t
);
}
}

case 'callable':
case 'closure':
return static function (): void {
};
if (in_array('object', $types, true)) {
return new stdClass;
}

case 'traversable':
case 'generator':
case 'iterable':
$generator = static function (): \Generator {
yield from [];
};
if (in_array('callable', $types, true) ||
in_array('closure', $types, true)) {
return static function (): void {
};
}

return $generator();
if (in_array('traversable', $types, true) ||
in_array('generator', $types, true) ||
in_array('iterable', $types, true)) {
$generator = static function (): \Generator {
yield from [];
};

case 'mixed':
return null;
return $generator();
}

default:
if (!$union) {
try {
return (new Generator)->getMock($this->returnType, [], [], '', false);
} catch (Throwable $t) {
throw new RuntimeException(
$t->getMessage(),
(int) $t->getCode(),
$t
);
}
}

throw new RuntimeException(
sprintf(
'Return value for %s::%s() cannot be generated because the declared return type is a union, please configure a return value for this method',
$this->className,
$this->methodName
)
);
}

public function toString(): string
Expand Down

0 comments on commit 2f2d90a

Please sign in to comment.