Skip to content

Commit dd71d32

Browse files
Merge branch '11.2'
2 parents 559d0c9 + a5a2676 commit dd71d32

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

src/Framework/MockObject/Generator/Generator.php

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -578,15 +578,28 @@ private function userDefinedInterfaceMethods(string $interfaceName): array
578578
private function getObject(MockType $mockClass, string $type = '', bool $callOriginalConstructor = false, array $arguments = [], bool $callOriginalMethods = false, ?object $proxyTarget = null, bool $returnValueGeneration = true): object
579579
{
580580
$className = $mockClass->generate();
581-
$object = $this->instantiate($className, $callOriginalConstructor, $arguments);
581+
582+
try {
583+
$object = (new ReflectionClass($className))->newInstanceWithoutConstructor();
584+
// @codeCoverageIgnoreStart
585+
} catch (\ReflectionException $e) {
586+
throw new ReflectionException(
587+
$e->getMessage(),
588+
$e->getCode(),
589+
$e,
590+
);
591+
// @codeCoverageIgnoreEnd
592+
}
593+
594+
$reflector = new ReflectionObject($object);
582595

583596
if ($object instanceof StubInternal && $mockClass instanceof MockClass) {
584597
/**
585598
* @psalm-suppress MissingThrowsDocblock
586599
*
587600
* @noinspection PhpUnhandledExceptionInspection
588601
*/
589-
(new ReflectionObject($object))->getProperty('__phpunit_state')->setValue(
602+
$reflector->getProperty('__phpunit_state')->setValue(
590603
$object,
591604
new TestDoubleState($mockClass->configurableMethods(), $returnValueGeneration),
592605
);
@@ -596,6 +609,20 @@ private function getObject(MockType $mockClass, string $type = '', bool $callOri
596609
}
597610
}
598611

612+
if ($callOriginalConstructor && $reflector->getConstructor() !== null) {
613+
try {
614+
$reflector->getConstructor()->invokeArgs($object, $arguments);
615+
// @codeCoverageIgnoreStart
616+
} catch (\ReflectionException $e) {
617+
throw new ReflectionException(
618+
$e->getMessage(),
619+
$e->getCode(),
620+
$e,
621+
);
622+
// @codeCoverageIgnoreEnd
623+
}
624+
}
625+
599626
return $object;
600627
}
601628

@@ -1003,44 +1030,6 @@ trait_exists($className, false)) {
10031030
}
10041031
}
10051032

1006-
/**
1007-
* @psalm-param class-string $className
1008-
*
1009-
* @throws ReflectionException
1010-
*/
1011-
private function instantiate(string $className, bool $callOriginalConstructor, array $arguments): object
1012-
{
1013-
if ($callOriginalConstructor) {
1014-
if (count($arguments) === 0) {
1015-
return new $className;
1016-
}
1017-
1018-
try {
1019-
return (new ReflectionClass($className))->newInstanceArgs($arguments);
1020-
// @codeCoverageIgnoreStart
1021-
} catch (\ReflectionException $e) {
1022-
throw new ReflectionException(
1023-
$e->getMessage(),
1024-
$e->getCode(),
1025-
$e,
1026-
);
1027-
}
1028-
// @codeCoverageIgnoreEnd
1029-
}
1030-
1031-
try {
1032-
return (new ReflectionClass($className))->newInstanceWithoutConstructor();
1033-
// @codeCoverageIgnoreStart
1034-
} catch (\ReflectionException $e) {
1035-
throw new ReflectionException(
1036-
$e->getMessage(),
1037-
$e->getCode(),
1038-
$e,
1039-
);
1040-
// @codeCoverageIgnoreEnd
1041-
}
1042-
}
1043-
10441033
/**
10451034
* @psalm-param class-string $type
10461035
*

tests/unit/Framework/MockObject/Creation/MockBuilderTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ public function testCreatesMockObjectForUnknownType(): void
170170
#[TestDox('Mocked methods can be called from the original constructor of a partially mocked class')]
171171
public function testOnlyMethodCalledInConstructorWorks(): void
172172
{
173-
$this->markTestIncomplete('https://github.com/sebastianbergmann/phpunit/issues/5857');
174-
175173
$double = $this->getMockBuilder(ClassCallingMethodInConstructor::class)
176174
->onlyMethods(['reset'])
177175
->getMock();

0 commit comments

Comments
 (0)