Skip to content

Commit d29eefb

Browse files
committed
Add expected arguments param to createCallback()
1 parent e1dd1dd commit d29eefb

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/AsyncTestCase.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,15 @@ final protected function setTimeout(float $seconds): void
191191
/**
192192
* @param int $invocationCount Number of times the callback must be invoked or the test will fail.
193193
* @param callable|null $returnCallback Callable providing a return value for the callback.
194+
* @param array $expectArgs Arguments expected to be passed to the callback.
194195
*
195196
* @return \Closure
196197
*/
197-
final protected function createCallback(int $invocationCount, ?callable $returnCallback = null): \Closure
198+
final protected function createCallback(
199+
int $invocationCount,
200+
?callable $returnCallback = null,
201+
array $expectArgs = [],
202+
): \Closure
198203
{
199204
$mock = $this->createMock(CallbackStub::class);
200205
$invocationMocker = $mock->expects(self::exactly($invocationCount))
@@ -204,6 +209,10 @@ final protected function createCallback(int $invocationCount, ?callable $returnC
204209
$invocationMocker->willReturnCallback($returnCallback);
205210
}
206211

212+
if ($expectArgs) {
213+
$invocationMocker->with(...$expectArgs);
214+
}
215+
207216
return \Closure::fromCallable($mock);
208217
}
209218
}

test/AsyncTestCaseTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Amp\PHPUnit\TestException;
99
use Amp\PHPUnit\UnhandledException;
1010
use PHPUnit\Framework\AssertionFailedError;
11+
use PHPUnit\Framework\ExpectationFailedException;
1112
use Revolt\EventLoop;
1213
use function Amp\async;
1314
use function Amp\delay;
@@ -175,10 +176,7 @@ public function testSetMinimumRunTime(): void
175176

176177
public function testCreateCallback(): void
177178
{
178-
$mock = $this->createCallback(1, function (int $value): int {
179-
return $value + 1;
180-
});
181-
179+
$mock = $this->createCallback(1, fn (int $value) => $value + 1, [1]);
182180
self::assertSame(2, $mock(1));
183181
}
184182

0 commit comments

Comments
 (0)