Skip to content

Commit 6390216

Browse files
committed
updated docs for stacked stubs support
1 parent 7d9252b commit 6390216

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#### 2.0.0
44

5-
* **Updated to Go AOP Framework 1.0**
5+
* **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.
89

910
#### 1.0.0
1011

src/AspectMock/Test.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,41 @@ class Test {
8484
* ?>
8585
* ```
8686
*
87+
* #### Stacked Calls Support
88+
*
89+
* Since 2.0 you can register multiple functions inside stib which will be executed in chain.
90+
* You can pass control to next function in a stack by returning `__AM_CONTINUE__` constant:
91+
*
92+
* ```php
93+
* class User {
94+
* public function getGroup() { return 'guest'; }
95+
* }
96+
* $user = new User;
97+
* ```
98+
*
99+
* So, it is for conflicted stubs, when a test double (e.g., getGroup) has been redefined.
100+
*
101+
* ```php
102+
* $stub1 = function () {
103+
* return 'user';
104+
* };
105+
* $stub2 = function ($supersede = false) {
106+
* return $supersede ? 'root' : __AM_CONTINUE__;
107+
* };
108+
* test::double('User', ['getGroup' => $stub1]);
109+
* test::double('User', ['getGroup' => $stub2]);
110+
*
111+
* ```
112+
*
113+
* 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.
114+
* So, the result will look like this:
115+
*
116+
* ```php
117+
* $user->getGroup(true) // => root (handled by $stub2)
118+
* $user->getGroup() // => user (handled by $stub2 and then $stub1)
119+
* ```
120+
* 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__.
121+
*
87122
* @api
88123
* @param string|object $classOrObject
89124
* @param array $params [ 'methodName' => 'returnValue' ]

0 commit comments

Comments
 (0)