|
1 | | ---- |
2 | | -layout: page |
3 | | -title: Codeception Stubs |
4 | | ---- |
5 | | - |
6 | | -# Stub |
7 | | - |
8 | | -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. |
9 | | - |
10 | | -### Usage |
11 | | - |
12 | | -* `make` - creates new class bypassing constructor |
13 | | -* `makeEmpty` - creates new class bypassing constructor with all methods return null. |
14 | | -* `construct` - creates new class throgh constructor. |
15 | | -* `constructEmpty` - creates new empty class throgh constructor with all methods return null. |
16 | | -* `copy` - copies object and redefines it's properties. |
17 | | - |
18 | | -### Example |
19 | | - |
20 | | -{% highlight php %} |
21 | | -use \Codeception\Util\Stub as Stub; |
22 | | - |
23 | | -// create class instance with name set method 'save' redefined. |
24 | | -$user = Stub::make('User', array('name' => 'davert', 'save' => function () { return true; })); |
25 | | - |
26 | | -// create class instance with all empty methods (will return NULL) |
27 | | -$user = Stub::makeEmpty('User', array('getName' => function () { return 'davert'; })); |
28 | | -$user->save(); // is empty and returns NULL |
29 | | -$user->getName(); // return 'davert' |
30 | | - |
31 | | -// create class with empty methods except one |
32 | | -$user = Stub::makeEmptyExcept('User', 'getName', array('name' => 'davert')); |
33 | | -$user->save(); // is empty and returns NULL |
34 | | -$user->getName(); // returns 'davert' |
35 | | - |
36 | | -// create class instance through constructor |
37 | | -// second parameter is array, it's values will be passed to constructor. |
38 | | - |
39 | | -// similar to $user = new User($con, $is_new); |
40 | | -$user = Stub::construct('User', array($con, $is_new), array('name' => 'davert')); |
41 | | - |
42 | | -// similar is constructEmpty |
43 | | -$user = Stub::constructEmpty('User', array($con, $is_new), array('getName' => function () { return 'davert'; })); |
44 | | - |
45 | | -// and constructEmptyExcept |
46 | | -$user = Stub::constructEmptyExcept('User', 'getName', array($con, $is_new), array('name' => 'davert')); |
47 | | - |
48 | | -// copy and redefine class instance |
49 | | -// can act with regular objects, not only stubs |
50 | | -$user->getName(); // returns 'davert' |
51 | | -$user2 = Stub::copy($user, array('name' => 'davert2')); |
52 | | -$user->getName(); // returns 'davert2' |
53 | | -{% endhighlight %} |
54 | | - |
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Codeception Stubs |
| 4 | +--- |
| 5 | + |
| 6 | +# Stub |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +### Usage |
| 11 | + |
| 12 | +* `make` - creates new class bypassing constructor |
| 13 | +* `makeEmpty` - creates new class bypassing constructor with all methods return null. |
| 14 | +* `construct` - creates new class throgh constructor. |
| 15 | +* `constructEmpty` - creates new empty class throgh constructor with all methods return null. |
| 16 | +* `copy` - copies object and redefines it's properties. |
| 17 | + |
| 18 | +### Example |
| 19 | + |
| 20 | +{% highlight php %} |
| 21 | +<?php |
| 22 | +use \Codeception\Util\Stub as Stub; |
| 23 | +
|
| 24 | +// create class instance with name set method 'save' redefined. |
| 25 | +$user = Stub::make('User', array('name' => 'davert', 'save' => function () { return true; })); |
| 26 | + |
| 27 | +// create class instance with all empty methods (will return NULL) |
| 28 | +$user = Stub::makeEmpty('User', array('getName' => function () { return 'davert'; })); |
| 29 | +$user->save(); // is empty and returns NULL |
| 30 | +$user->getName(); // return 'davert' |
| 31 | + |
| 32 | +// create class with empty methods except one |
| 33 | +$user = Stub::makeEmptyExcept('User', 'getName', array('name' => 'davert')); |
| 34 | +$user->save(); // is empty and returns NULL |
| 35 | +$user->getName(); // returns 'davert' |
| 36 | + |
| 37 | +// create class instance through constructor |
| 38 | +// second parameter is array, it's values will be passed to constructor. |
| 39 | + |
| 40 | +// similar to $user = new User($con, $is_new); |
| 41 | +$user = Stub::construct('User', array($con, $is_new), array('name' => 'davert')); |
| 42 | + |
| 43 | +// similar is constructEmpty |
| 44 | +$user = Stub::constructEmpty('User', array($con, $is_new), array('getName' => function () { return 'davert'; })); |
| 45 | + |
| 46 | +// and constructEmptyExcept |
| 47 | +$user = Stub::constructEmptyExcept('User', 'getName', array($con, $is_new), array('name' => 'davert')); |
| 48 | + |
| 49 | +// copy and redefine class instance |
| 50 | +// can act with regular objects, not only stubs |
| 51 | +$user->getName(); // returns 'davert' |
| 52 | +$user2 = Stub::copy($user, array('name' => 'davert2')); |
| 53 | +$user->getName(); // returns 'davert2' |
| 54 | +?> |
| 55 | +{% endhighlight %} |
| 56 | + |
0 commit comments