Skip to content

Commit abe61d7

Browse files
Fix regression - partial construction with trait methods (#1403)
Signed-off-by: Nathanael Esayeas <[email protected]> Co-authored-by: Ant Weedon <[email protected]>
1 parent 0d6ae93 commit abe61d7

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

library/Mockery/Mock.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,10 @@ protected function _mockery_handleMethodCall($method, array $args)
910910
// noop - there is no hasPrototype method
911911
}
912912

913+
if (null === $this->_mockery_parentClass) {
914+
$this->_mockery_parentClass = get_parent_class($this);
915+
}
916+
913917
return call_user_func_array($this->_mockery_parentClass . '::' . $method, $args);
914918
}
915919

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fixture\PHP74\Regression\Issue1402;
6+
7+
/**
8+
* This trait does something, but we need to initialise a thing on construction.
9+
*/
10+
trait InitTrait {
11+
12+
protected function init(): void {}
13+
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fixture\PHP74\Regression\Issue1402;
6+
7+
class Service {
8+
9+
use InitTrait;
10+
11+
private int $arg;
12+
13+
public function __construct(int $arg)
14+
{
15+
$this->arg = $arg;
16+
17+
$this->init();
18+
}
19+
20+
public function test(): int
21+
{
22+
return $this->arg;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mockery\Tests\Unit\Regression;
6+
7+
use Fixture\PHP74\Regression\Issue1402\Service;
8+
use Mockery;
9+
use Mockery\Adapter\Phpunit\MockeryTestCase;
10+
11+
/**
12+
* @requires PHP 7.4
13+
*/
14+
final class Issue1402Test extends MockeryTestCase
15+
{
16+
public function testMethod(): void {
17+
$banana = Mockery::mock(Service::class, [1])
18+
->makePartial();
19+
20+
$banana->allows('test')->andReturns(2);
21+
22+
self::assertEquals(2, $banana->test());
23+
}
24+
}

0 commit comments

Comments
 (0)