Skip to content

Commit

Permalink
Closes #3722
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 16, 2019
1 parent 7ff96e2 commit 8f01ac3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog-7.5.md
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/Framework/MockObject/Generator.php
Expand Up @@ -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
);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/_files/TraitWithConstructor.php
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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;
}
}
9 changes: 8 additions & 1 deletion tests/unit/Framework/MockObject/MockObjectTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit 8f01ac3

Please sign in to comment.