Roman Vitruk Roman Vitruk - 6 months ago 50
PHP Question

What is the difference between Codeception\Util\Stub::construct('SomeClass') and new SomeClass?

There are two ways to create new class writing unit tests in Codeception.

use Codeception\Util\Stub as Stub;
$SomeClass = new SomeClass();
$SomeClass = Stub::construct('SomeClass');

Can someone explain what is the difference between this two approaches?


new SomeClass creates a regural instance of the class,
Stub::construct('SomeClass') creates a test double of the class with some methods or properties replaced.

As documented in

Properties and methods can be set in third argument. Even protected and private properties can be set.

There is no difference between the two (and no reason to use Stub), if you are not overriding any properties or methods.