Using Codeception Stub Class For Unit Tests

Hi all,

back again on testing.

I’m still stuck on a couple of things regarding unit testing.

I’m trying to see if it’s possible to use CodeCeption Stub static class.

Let’s take as an example Stubbing the security component.

What I need to do is just have the two methods validatePassword and generatePasswordHash to return what I need.

With CodeCeption it seems I should be able to do the following:




$security = Stub::construct(

    'yii\base\Security',

    [

        'validatePassword' => true,

        'generatePasswordHash' => $expectedPassword

    ]

);

Yii::$app->set('security', $security);



which seems to be really neat and clean.

Unfortunately this doesn’t work and throws all kind of errors.

Instead the PHPUnit classic method works flawlessly:


$security = $this->getMock(

    'yii\base\Security',

    ['validatePassword', 'generatePasswordHash']

);

$security->expects($this->any())

    ->method('validatePassword')

    ->with($expectedPassword)

    ->willReturn(true);

$security->expects($this->any())

    ->method('generatePasswordHash')

    ->with($expectedPassword)

    ->willReturn($expectedPassword);



Any ideas here?

There’s no solution for this, e.g. use PHPUnit mocking functionality.

For more in depth discussion and links head over to:

Does it mean, it’s not possible to mock data for Yii2 codeception test?

Currently I try to get an Yii2/codeception unittest running and i like to load different data into an model (from an array / mock /…) in order to test the $model->verify() function.