Skip to content

Commit 0a0077a

Browse files
committed
updated docs
1 parent e3a5279 commit 0a0077a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* **Updated to Go AOP Framework 2.0**
66
* PHP7 (with typehints) supported
77
* Minimal PHP version is PHP 5.6
8-
* Stacked support for mocked methods. You can register a chain of methods as a stub, and to control their execution.
8+
* [Stacked functions support](https://github.com/Codeception/AspectMock/blob/master/docs/Test.md#Stacked-Calls-Support) for mocked methods. You can register a chain of methods as a stub, and to control their execution. By @torreytsui See https://github.com/Codeception/AspectMock/pull/91
99

1010
#### 1.0.0
1111

docs/Test.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,54 @@ test::double('ActiveRecord', ['save' => false]);
7474
$user = new User(['name' => 'davert']);
7575
$user->save(); // false
7676

77+
# on conflicted stubs
78+
test::double('User', ['getGroup' => function () { return $this->group; }]);
79+
$user = new User;
80+
$user->group = 'guest';
81+
$user->getGroup(); // => guest
82+
test::double('User', ['getGroup' => function ($supersede = false) { return $supersede ? 'root' : __AM_CONTINUE__; }]);
83+
$user->group = 'user';
84+
$user->getGroup(true); // => root
85+
$user->getGroup(); // => user
86+
7787
?>
7888
```
7989

90+
#### Stacked Calls Support
91+
92+
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__.
124+
80125
* api
81126
* `param string|object` $classOrObject
82127
* `param array` $params [ 'methodName' => 'returnValue' ]

0 commit comments

Comments
 (0)