You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since 2.0 you can register multiple functions inside stib which will be executed in chain.
93
-
You can pass control to next function in a stack by returning `__AM_CONTINUE__` constant:
94
-
95
-
```php
96
-
class User {
97
-
public function getGroup() { return 'guest'; }
98
-
}
99
-
$user = new User;
100
-
```
101
-
102
-
So, it is for conflicted stubs, when a test double (e.g., getGroup) has been redefined.
103
-
104
-
```php
105
-
$stub1 = function () {
106
-
return 'user';
107
-
};
108
-
$stub2 = function ($supersede = false) {
109
-
return $supersede ? 'root' : __AM_CONTINUE__;
110
-
};
111
-
test::double('User', ['getGroup' => $stub1]);
112
-
test::double('User', ['getGroup' => $stub2]);
113
-
114
-
```
115
-
116
-
The idea basically is to allow a chain of responsibility passing through every conflicted stubs until a result returns. Use stack structure so that the latest defined stub will gain highest priority.
117
-
So, the result will look like this:
118
-
119
-
```php
120
-
$user->getGroup(true) // => root (handled by $stub2)
121
-
$user->getGroup() // => user (handled by $stub2 and then $stub1)
122
-
```
123
-
The $user->getGroup() // => user first handled by $stub2 and it gives up control by returning __AM_CONTINUE__, $stub1 then take place and return "user". If $stub1 return __AM_CONTINUE__, it will return control to the real object, as every stub has returned __AM_CONTINUE__.
0 commit comments