Skip to content

Commit

Permalink
Fix regression - partial construction with trait methods (#1403)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Co-authored-by: Ant Weedon <81413869+adnweedon@users.noreply.github.com>
  • Loading branch information
ghostwriter and adnweedon committed Mar 21, 2024
1 parent 0d6ae93 commit abe61d7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/Mockery/Mock.php
Expand Up @@ -910,6 +910,10 @@ protected function _mockery_handleMethodCall($method, array $args)
// noop - there is no hasPrototype method
}

if (null === $this->_mockery_parentClass) {
$this->_mockery_parentClass = get_parent_class($this);
}

return call_user_func_array($this->_mockery_parentClass . '::' . $method, $args);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Fixture/PHP74/Regression/Issue1402/InitTrait.php
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Fixture\PHP74\Regression\Issue1402;

/**
* This trait does something, but we need to initialise a thing on construction.
*/
trait InitTrait {

protected function init(): void {}

}
24 changes: 24 additions & 0 deletions tests/Fixture/PHP74/Regression/Issue1402/Service.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Fixture\PHP74\Regression\Issue1402;

class Service {

use InitTrait;

private int $arg;

public function __construct(int $arg)
{
$this->arg = $arg;

$this->init();
}

public function test(): int
{
return $this->arg;
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Regression/Issue1402Test.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Mockery\Tests\Unit\Regression;

use Fixture\PHP74\Regression\Issue1402\Service;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;

/**
* @requires PHP 7.4
*/
final class Issue1402Test extends MockeryTestCase
{
public function testMethod(): void {
$banana = Mockery::mock(Service::class, [1])
->makePartial();

$banana->allows('test')->andReturns(2);

self::assertEquals(2, $banana->test());
}
}

0 comments on commit abe61d7

Please sign in to comment.