/
FunctionalTestCase.php
102 lines (81 loc) · 2.85 KB
/
FunctionalTestCase.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Rx\Functional;
use PHPUnit_Framework_ExpectationFailedException;
use Rx\Scheduler\VirtualTimeScheduler;
use Rx\TestCase;
use Rx\Testing\ColdObservable;
use Rx\Testing\HotObservable;
use Rx\Testing\Subscription;
use Rx\Testing\TestScheduler;
abstract class FunctionalTestCase extends TestCase
{
/** @var TestScheduler */
protected $scheduler;
public function setup()
{
$this->scheduler = $this->createTestScheduler();
}
public function assertMessages(array $expected, array $recorded)
{
if (count($expected) !== count($recorded)) {
$this->fail(sprintf('Expected message count %d does not match actual count %d.', count($expected), count($recorded)));
}
for ($i = 0, $count = count($expected); $i < $count; $i++) {
if (!$expected[$i]->equals($recorded[$i])) {
$this->fail($expected[$i] . ' does not equal ' . $recorded[$i]);
}
}
$this->assertTrue(true); // success
}
public function assertSubscription(HotObservable $observable, Subscription $expected)
{
$subscriptionCount = count($observable->getSubscriptions());
if ($subscriptionCount === 0) {
$this->fail('Observable has no subscriptions.');
}
if ($subscriptionCount > 1) {
$this->fail('Observable has more than 1 subscription.');
}
list($actual) = $observable->getSubscriptions();
if (!$expected->equals($actual)) {
$this->fail(sprintf("Expected subscription '%s' does not match actual subscription '%s'", $expected, $actual));
}
$this->assertTrue(true); // success
}
public function assertSubscriptions(array $expected, array $recorded)
{
if (count($expected) !== count($recorded)) {
$this->fail(sprintf('Expected subscription count %d does not match actual count %d.', count($expected), count($recorded)));
}
for ($i = 0, $count = count($expected); $i < $count; $i++) {
if (!$expected[$i]->equals($recorded[$i])) {
$this->fail($expected[$i] . ' does not equal ' . $recorded[$i]);
}
}
$this->assertTrue(true); // success
}
/**
* @param callable $callback
*/
protected function assertException(callable $callback)
{
try {
$callback();
} catch (\Exception $e) {
return;
}
$this->fail('Expected the callback to throw an exception.');
}
protected function createColdObservable(array $events)
{
return new ColdObservable($this->scheduler, $events);
}
protected function createHotObservable(array $events)
{
return new HotObservable($this->scheduler, $events);
}
protected function createTestScheduler()
{
return new TestScheduler();
}
}