Skip to content

Commit 63e8a13

Browse files
committed
doc(src) fix some doc comments, add changelog entry
1 parent 5249be4 commit 63e8a13

4 files changed

Lines changed: 46 additions & 28 deletions

File tree

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ public fuction testReplacedFunctionReturnsCallback(){
153153
}
154154
```
155155

156+
If you need to replace a function and make it return a sequence of values, one at each call, then you can use the `FunctionMocker::replaceInOrder` method:
157+
158+
```php
159+
public function testReplacedFunctionReturnsValue(){
160+
FunctionMocker::replaceInOrder('myFunction', [23, 89, 2389]);
161+
162+
$this->assertEquals(23, myFunction());
163+
$this->assertEquals(89, myFunction());
164+
$this->assertEquals(2389, myFunction());
165+
}
166+
```
167+
156168
#### Spying on functions
157169
If the value returned by the `FunctionMocker::replace` method is stored in a variable than checks for calls can be made on that function:
158170

@@ -644,4 +656,4 @@ $do_action->wasCalledWithOnce(['before_switch_to_theme_foo', ])
644656

645657
// restore state
646658
$GLOBALS['switchingToTheme'] = $prev;
647-
```
659+
```

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This projec
33

44
## [Unreleased][unreleased]
55

6+
## [1.3.9]
7+
### Added
8+
- the `FunctionMocker::replaceInOrder` method to return values from a set from a replaced function or static method, thanks @wppunk, fixes #11
9+
610
## [1.3.8]
711
### Fixed
812
- compatibility with PHPUnit 6.0+

src/tad/FunctionMocker/FunctionMocker.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,42 +120,44 @@ public static function replace($functionName, $returnValue = null) {
120120
}
121121

122122
/**
123-
* Replaces a function with few results, a static method or an instance method.
123+
* Replaces a function, a static method or an instance method with a set of results, that will be returned in order.
124124
*
125-
* The function or methods to be replaced must be specified with fully
126-
* qualified names like
125+
* The function or methods to be replaced must be specified with fully qualified names like:
127126
*
128-
* FunctionMocker::replace('my\name\space\aFunction');
129-
* FunctionMocker::replace('my\name\space\SomeClass::someMethod');
127+
* FunctionMocker::replaceInOrder('my\name\space\aFunction', [1, 2, 3]);
128+
* FunctionMocker::replaceInOrder('my\name\space\SomeClass::someMethod', [1, 2, 3]);
130129
*
131-
* not specifying a return value will make the replaced function or value
132-
* return `null`.
130+
* @param string $functionName The fully-qualified name of the function, or static method, to replace.
131+
* @param array<mixed> $returnValues The set of values to return when the function is called, in order.
133132
*
134-
* @param $functionName
135-
* @param null $returnValue
136-
*
137-
* @return mixed|Call\Verifier\InstanceMethodCallVerifier|static
133+
* @return mixed A return value from the set, depending on the current index.
138134
*/
139-
public static function replaceInOrder($functionName, $returnValue = null) {
140-
if ( ! $returnValue || ! is_array( $returnValue ) ) {
141-
return self::replace( $functionName, $returnValue );
135+
public static function replaceInOrder($functionName, array $returnValues) {
136+
if ( ! $returnValues || ! is_array($returnValues) ) {
137+
return self::replace($functionName, $returnValues);
142138
}
143-
$returnValue = array_values( $returnValue );
144139

145-
return self::replace( $functionName, function() use ( $returnValue ) {
146-
static $i = 0;
140+
$returnValues = array_values($returnValues);
141+
142+
return self::replace($functionName, static function () use ($returnValues) {
143+
static $i = 0;
147144

148-
return $returnValue[ $i ++ ];
149-
} );
145+
return $returnValues[$i ++];
146+
});
150147
}
151148

152-
/**
153-
* @param $functionName
154-
* @param $returnValue
155-
*
156-
* @return mixed|null|Call\Verifier\InstanceMethodCallVerifier|static|Step
157-
* @throws \Exception
158-
*/
149+
/**
150+
* Internal method to replace the functions or static methods.
151+
*
152+
* @param string $functionName The fully-qualified name of the function or static method or instance method to
153+
* replace.
154+
* @param mixed $returnValue The return value for the replaced function or static method.
155+
*
156+
* @return mixed|null|Call\Verifier\InstanceMethodCallVerifier|static|Step The replaced function handle, for spying,
157+
* or the head of a replacement chain.
158+
*
159+
* @throws \Exception
160+
*/
159161
private static function _replace($functionName, $returnValue) {
160162
$request = ReplacementRequest::on($functionName);
161163
$returnValue = ReturnValue::from($returnValue);

src/tad/FunctionMocker/ReturnValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function isNull()
4545

4646
public function call(array $args = array())
4747
{
48-
return call_user_func_array($this->value, $args);
48+
return call_user_func_array( $this->value, $args );
4949
}
5050

5151
public function isSelf()

0 commit comments

Comments
 (0)