| layout | page |
|---|---|
| title | Codeception Stubs |
Class for easily create stubs in your unit tests. You can use this class even inside PHPUnit tests too. It's a useful wrapper on top of PHPUnit mock builder.
make- creates new class bypassing constructormakeEmpty- creates new class bypassing constructor with all methods return null.construct- creates new class throgh constructor.constructEmpty- creates new empty class throgh constructor with all methods return null.copy- copies object and redefines it's properties.
{% highlight php %}
'davert', 'save' => function () { return true; })); // create class instance with all empty methods (will return NULL) $user = Stub::makeEmpty('User', array('getName' => function () { return 'davert'; })); $user->save(); // is empty and returns NULL $user->getName(); // return 'davert' // create class with empty methods except one $user = Stub::makeEmptyExcept('User', 'getName', array('name' => 'davert')); $user->save(); // is empty and returns NULL $user->getName(); // returns 'davert' // create class instance through constructor // second parameter is array, it's values will be passed to constructor. // similar to $user = new User($con, $is_new); $user = Stub::construct('User', array($con, $is_new), array('name' => 'davert')); // similar is constructEmpty $user = Stub::constructEmpty('User', array($con, $is_new), array('getName' => function () { return 'davert'; })); // and constructEmptyExcept $user = Stub::constructEmptyExcept('User', 'getName', array($con, $is_new), array('name' => 'davert')); // copy and redefine class instance // can act with regular objects, not only stubs $user->getName(); // returns 'davert' $user2 = Stub::copy($user, array('name' => 'davert2')); $user->getName(); // returns 'davert2' ?>{% endhighlight %}