AspectMock icon indicating copy to clipboard operation
AspectMock copied to clipboard

Test::double doesn't work properly for static methods in parent

Open pawelkania opened this issue 7 years ago • 1 comments

I'm using Yii2, Codeception and AspectMock, when I'm trying to mock/double static method from parent class then original method is used instad of mocked one.

Below example:

class Yii extends \yii\BaseYii class \yii\BaseYii has static method error()

public function testStaticMethodForParentIsMocked()
{
        $appVerifier = AspectMock\Test::double(\yii\BaseYii::class, [
            'error' => function () {
                return 2;
            }
        ]);

        $test = \Yii::error('test'); // Original method were used
        $this->assertEquals(2, $test); // this assert will fail - not correct
        $appVerifier->verifyInvokedOnce('error'); // this will pass - correct
}
public function testStaticMethodForClassIsMocked()
{
        $appVerifier = AspectMock\Test::double(\Yii::class, [
            'error' => function () {
                return 2;
            }
        ]);

        $test = \Yii::error('test'); // Mocked method were used
        $this->assertEquals(2, $test); // this assert will pass - correct
        $appVerifier->verifyInvokedOnce('error'); // this will pass - correct
}

pawelkania avatar Jan 18 '17 12:01 pawelkania

Same problem.

I'm using Yii2 too and trying to mock ::findOne() method. If I write Test::double(User::class, ['findOne' => new User()]);. then nothing happened. But if I override this method in the User model. then everything works fine.

I saw https://github.com/Codeception/AspectMock/pull/155 and already use 3.0.1, but it's not actually fixed. There is also no tests to add my case.

erickskrauch avatar Sep 01 '18 17:09 erickskrauch