Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression - partial construction with trait methods #1403

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/Mockery/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@
// noop - there is no hasPrototype method
}

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

Check warning on line 914 in library/Mockery/Mock.php

View check run for this annotation

Codecov / codecov/patch

library/Mockery/Mock.php#L913-L914

Added lines #L913 - L914 were not covered by tests
}

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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());
}
}