Skip to content

Commit

Permalink
Closes #5857
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 10, 2024
1 parent 4c37cbd commit a5a2676
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
1 change: 1 addition & 0 deletions ChangeLog-11.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes of the PHPUnit 11.2 release series are documented in this fi

### Fixed

* [#5857](https://github.com/sebastianbergmann/phpunit/issues/5857): Mocked method called in constructor and a additional method throws an unexpected exception
* [#5859](https://github.com/sebastianbergmann/phpunit/issues/5859): XML Configuration File Migrator does not remove `cacheDirectory` attribute from `<coverage>` element when migrating from PHPUnit 11.1 to PHPUnit 11.2

## [11.2.0] - 2024-06-07
Expand Down
69 changes: 29 additions & 40 deletions src/Framework/MockObject/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,28 @@ private function userDefinedInterfaceMethods(string $interfaceName): array
private function getObject(MockType $mockClass, string $type = '', bool $callOriginalConstructor = false, array $arguments = [], bool $callOriginalMethods = false, ?object $proxyTarget = null, bool $returnValueGeneration = true): object
{
$className = $mockClass->generate();
$object = $this->instantiate($className, $callOriginalConstructor, $arguments);

try {
$object = (new ReflectionClass($className))->newInstanceWithoutConstructor();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e,
);
// @codeCoverageIgnoreEnd
}

$reflector = new ReflectionObject($object);

if ($object instanceof StubInternal && $mockClass instanceof MockClass) {
/**
* @psalm-suppress MissingThrowsDocblock
*
* @noinspection PhpUnhandledExceptionInspection
*/
(new ReflectionObject($object))->getProperty('__phpunit_state')->setValue(
$reflector->getProperty('__phpunit_state')->setValue(
$object,
new TestDoubleState($mockClass->configurableMethods(), $returnValueGeneration),
);
Expand All @@ -596,6 +609,20 @@ private function getObject(MockType $mockClass, string $type = '', bool $callOri
}
}

if ($callOriginalConstructor && $reflector->getConstructor() !== null) {
try {
$reflector->getConstructor()->invokeArgs($object, $arguments);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e,
);
// @codeCoverageIgnoreEnd
}
}

return $object;
}

Expand Down Expand Up @@ -1003,44 +1030,6 @@ trait_exists($className, false)) {
}
}

/**
* @psalm-param class-string $className
*
* @throws ReflectionException
*/
private function instantiate(string $className, bool $callOriginalConstructor, array $arguments): object
{
if ($callOriginalConstructor) {
if (count($arguments) === 0) {
return new $className;
}

try {
return (new ReflectionClass($className))->newInstanceArgs($arguments);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e,
);
}
// @codeCoverageIgnoreEnd
}

try {
return (new ReflectionClass($className))->newInstanceWithoutConstructor();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e,
);
// @codeCoverageIgnoreEnd
}
}

/**
* @psalm-param class-string $type
*
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/Framework/MockObject/Creation/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ public function testCreatesMockObjectForUnknownType(): void
#[TestDox('Mocked methods can be called from the original constructor of a partially mocked class')]
public function testOnlyMethodCalledInConstructorWorks(): void
{
$this->markTestIncomplete('https://github.com/sebastianbergmann/phpunit/issues/5857');

$double = $this->getMockBuilder(ClassCallingMethodInConstructor::class)
->onlyMethods(['reset'])
->getMock();
Expand Down

0 comments on commit a5a2676

Please sign in to comment.