AspectMock
AspectMock copied to clipboard
Best way to return multiple values from mocked methods
One of the classes I'm mocking is called few times along the process and from what I read in the docs, I can only return a single value when defining the mock. Is there a way to change the returning values on each call?
How about this?
test::double('Fuel\Core\Config', ['get' => function ($arg) {
if ($arg === 'foo.bar') {
return 'foo.bar';
} else {
return 'baz';
}
}]);
wonderful thank you @kenjis
Reopening to see if there are other methods when no arguments are passed to the method.
This should be the same:
test::double(Some::class, ['get' => function () {
static $index = 0;
$mockValues = ['foo', 'bar', 'baz'];
return $mockValues[$index++];
}]);
$arg = 'foo';
test::double(Some::class, ['hoge' => function () use ($arg) {
if ($arg == 'foo' ) {
return 'foo' ;
}
return 'bar';
}]);
$i =1;
test::double(Some::class, ['hoge' => function () use (&$i) {
if ($i == 1 ) {
$i++;
return '1 time' ;
} else if ($i == 2) {
$i++;
return '2 times';
}
return __AM_CONTINUE__;
}]);