using CodeCeption Stub helper #5210

Closed
ThePeach opened this Issue Sep 27, 2014 · 6 comments

Projects

None yet

5 participants

@ThePeach

I've already opened a post in the forums but didn't have a single reply, so I'm posting it here hoping someone could enlighten me.

tl;dr; I've used PHPUnit getMock() method without problems, but I cannot replicate the same functionality with CodeCeption Stub() class as it throws exceptions.

long version

As an example: I've implemented some tests for the User class (which I've also converted it in an ActiveRecord) and I've got to the point of testing validatePassword() which in turn is using the validatePassword() method of the security component of Yii.

I've then tried to create a stub of the security component using Composer Stub class as follows:

$security = Stub::construct(
    'yii\base\Security',
    [
        'validatePassword' => true,
        'generatePasswordHash' => $expectedPassword
    ]
);
Yii::$app->set('security', $security);

but if I run the tests containing such call I get the following exception

[snip]
4) tests\codeception\unit\models\UserTest::testValidatePasswordReturnsTrueIfPasswordIsCorrect
yii\base\ErrorException: Invalid argument supplied for foreach()

(almost) the same logic done with PHPUnit getMock() method works just fine:

$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);

I'm quite new to CodeCeption and I've been following its documentation and guides to use it fully as I do appreciate some of the syntactic sugar they've added to testing.

I also have to add that CodeCeption documentation on this topic is not exceptionally clear :)

Thanks.

@samdark
Member
samdark commented Sep 27, 2014

Probably worth reporting / asking in Codeception repo since it doesn't look Yii-special.

@nkovacs
Contributor
nkovacs commented Sep 27, 2014

The second parameter to Stub::construct should be the parameters to be passed to the constructor.

The correct code is:

    $security = Stub::construct(
        'yii\base\Security',
        [[]], // __construct expects one parameter, an array
        [
            'validatePassword' => true,
            'generatePasswordHash' => 'aaaaa',
        ]
    );

However, you will then run into #1694

@nkovacs
Contributor
nkovacs commented Sep 27, 2014
@ThePeach

thanks @nkovacs I completely missed the second parameter... and good to know about #1694.
Yes, you're right, if I get the second parameter I fall into the same error:

1) tests\codeception\unit\models\UserTest::testValidatePasswordReturnsTrueIfPasswordIsCorrect
yii\base\UnknownPropertyException: Setting unknown property: Mock_Security_f83a8fc0::__mocked

So it's not fixable because it's not an issue as it stands now?

Not a problem at all as long as PHPUnit works fine, but a pity as it would have been interesting to begin with :)

@Ragazzo
Contributor
Ragazzo commented Oct 2, 2014

@ThePeach yes , it is known bug see my comment for Yii1 , same here for Yii2 as you can see by Component and Object classes

@samdark can be closed i think , was also discussed with @DavertMik long time ago and decided that it is better for Yii to use phpunit or other mocking solutions

Also @ThePeach just FYI afaik @DavertMik was about to remove Stub functionality and to suggest to use other things like phpunit mocks or mockery to keep Codeception lightweight and simple

@ThePeach
ThePeach commented Oct 2, 2014

Thanks @Ragazzo very useful to know.

Also @ThePeach just FYI afaik @DavertMik was about to remove Stub functionality and to suggest to use other things like phpunit mocks or mockery to keep Codeception lightweight and simple

If that's correct, I can only second what @DavertMik said. Although the Stub class addresses some complexity issues on the use of PHPUnit, on the other hand it might introduce some undesired side-effects both on the use and on scope of CodeCeption itself (e.g. keeping it lightweight, simple and modular).

@qiangxue qiangxue closed this Oct 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment