forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCase.php
More file actions
64 lines (53 loc) · 1.88 KB
/
TestCase.php
File metadata and controls
64 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Clue\Tests\React\Redis;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase as BaseTestCase;
use React\Promise\PromiseInterface;
class TestCase extends BaseTestCase
{
protected function expectCallableOnce(): callable
{
$mock = $this->createCallableMock();
$mock->expects($this->once())->method('__invoke');
assert(is_callable($mock));
return $mock;
}
/** @param mixed $argument */
protected function expectCallableOnceWith($argument): callable
{
$mock = $this->createCallableMock();
$mock->expects($this->once())->method('__invoke')->with($argument);
assert(is_callable($mock));
return $mock;
}
protected function expectCallableNever(): callable
{
$mock = $this->createCallableMock();
$mock->expects($this->never())->method('__invoke');
assert(is_callable($mock));
return $mock;
}
protected function createCallableMock(): MockObject
{
if (method_exists(MockBuilder::class, 'addMethods')) {
// @phpstan-ignore-next-line requires PHPUnit 9+
return $this->getMockBuilder(\stdClass::class)->addMethods(['__invoke'])->getMock();
} else {
// legacy PHPUnit < 9
return $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
}
}
/**
* @param PromiseInterface<mixed> $promise
*/
protected function expectPromiseResolve(PromiseInterface $promise): void
{
$this->assertInstanceOf(PromiseInterface::class, $promise);
$promise->then(null, function(\Throwable $error) {
$this->assertNull($error);
$this->fail('promise rejected');
});
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
}
}