diff --git a/ChangeLog-7.5.md b/ChangeLog-7.5.md index 10f6e32d746..0a67557a5fc 100644 --- a/ChangeLog-7.5.md +++ b/ChangeLog-7.5.md @@ -6,6 +6,7 @@ All notable changes of the PHPUnit 7.5 release series are documented in this fil ### Fixed +* Fixed [#3722](https://github.com/sebastianbergmann/phpunit/issues/3722): `getObjectForTrait()` does not work for traits that declare a constructor * Fixed [#3723](https://github.com/sebastianbergmann/phpunit/pull/3723): Unescaped dash in character group in regular expression ## [7.5.12] - 2019-05-28 diff --git a/src/Framework/MockObject/Generator.php b/src/Framework/MockObject/Generator.php index 7d7d67cb774..c7300f761b1 100644 --- a/src/Framework/MockObject/Generator.php +++ b/src/Framework/MockObject/Generator.php @@ -374,7 +374,14 @@ public function getObjectForTrait($traitName, array $arguments = [], $traitClass ] ); - return $this->getObject($classTemplate->render(), $className['className']); + return $this->getObject( + $classTemplate->render(), + $className['className'], + '', + $callOriginalConstructor, + $callAutoload, + $arguments + ); } /** diff --git a/tests/_files/TraitWithConstructor.php b/tests/_files/TraitWithConstructor.php new file mode 100644 index 00000000000..ac9c55993ba --- /dev/null +++ b/tests/_files/TraitWithConstructor.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +trait TraitWithConstructor +{ + private $value; + + public function __construct(string $value) + { + $this->value = $value; + } + + public function value(): string + { + return $this->value; + } +} diff --git a/tests/unit/Framework/MockObject/MockObjectTest.php b/tests/unit/Framework/MockObject/MockObjectTest.php index 46f12e943eb..d58d7b21966 100644 --- a/tests/unit/Framework/MockObject/MockObjectTest.php +++ b/tests/unit/Framework/MockObject/MockObjectTest.php @@ -1110,13 +1110,20 @@ public function testObjectReturnTypeIsMockedCorrectly(): void $this->assertInstanceOf(stdClass::class, $stub->methodWithObjectReturnTypeDeclaration()); } - public function testGetObjectForTrait(): void + public function testTraitCanBeDoubled(): void { $object = $this->getObjectForTrait(ExampleTrait::class); $this->assertSame('ohHai', $object->ohHai()); } + public function testTraitWithConstructorCanBeDoubled(): void + { + $object = $this->getObjectForTrait(TraitWithConstructor::class, ['value']); + + $this->assertSame('value', $object->value()); + } + private function resetMockObjects(): void { $refl = new ReflectionObject($this);