Skip to content

Commit c1d2d4a

Browse files
committed
Cherry picked higher-order observable test from v1
1 parent 14b7fd3 commit c1d2d4a

File tree

7 files changed

+327
-32
lines changed

7 files changed

+327
-32
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Rx\Notification;
4+
5+
use Rx\Observable;
6+
use Rx\Testing\MockHigherOrderObserver;
7+
use Rx\Testing\TestScheduler;
8+
9+
class OnNextObservableNotification extends OnNextNotification
10+
{
11+
/** @var MockHigherOrderObserver */
12+
private $observer;
13+
14+
public function __construct(Observable $value, TestScheduler $scheduler = null)
15+
{
16+
parent::__construct($value);
17+
if (!$scheduler) {
18+
$scheduler = new TestScheduler();
19+
}
20+
21+
$this->observer = new MockHigherOrderObserver($scheduler, $scheduler->getClock());
22+
23+
$value->subscribe($this->observer);
24+
25+
$scheduler->start();
26+
}
27+
28+
public function equals($other): bool
29+
{
30+
$messages1 = $this->getMessages();
31+
/** @var OnNextObservableNotification $other */
32+
$messages2 = $other->getMessages();
33+
34+
if (count($messages1) != count($messages2)) {
35+
return false;
36+
}
37+
38+
for ($i = 0; $i < count($messages1); $i++) {
39+
if (!$messages1[$i]->equals($messages2[$i])) {
40+
return false;
41+
}
42+
}
43+
44+
return true;
45+
}
46+
47+
public function getMessages()
48+
{
49+
return $this->observer->getMessages();
50+
}
51+
52+
public function __toString(): string
53+
{
54+
return '[' . implode(', ', $this->getMessages()) . ']';
55+
}
56+
}

src/Testing/ColdObservable.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ public function __construct(TestScheduler $scheduler, array $messages = [])
2626

2727
protected function _subscribe(ObserverInterface $observer): DisposableInterface
2828
{
29-
$this->subscriptions[] = new Subscription($this->scheduler->getClock());
30-
$index = count($this->subscriptions) - 1;
31-
3229
$currentObservable = $this;
3330
$disposable = new CompositeDisposable();
3431
$scheduler = $this->scheduler;
3532
$isDisposed = false;
33+
$index = null;
34+
35+
if (!($observer instanceof MockHigherOrderObserver)) {
36+
$this->subscriptions[] = new Subscription($this->scheduler->getClock());
37+
$index = count($this->subscriptions) - 1;
38+
}
3639

3740
foreach ($this->messages as $message) {
3841
$notification = $message->getValue();
@@ -53,8 +56,10 @@ protected function _subscribe(ObserverInterface $observer): DisposableInterface
5356
$subscriptions = &$this->subscriptions;
5457

5558
return new CallbackDisposable(function () use (&$currentObservable, $index, $observer, $scheduler, &$subscriptions, &$isDisposed) {
56-
$isDisposed = true;
57-
$subscriptions[$index] = new Subscription($subscriptions[$index]->getSubscribed(), $scheduler->getClock());
59+
$isDisposed = true;
60+
if (!($observer instanceof MockHigherOrderObserver)) {
61+
$subscriptions[$index] = new Subscription($subscriptions[$index]->getSubscribed(), $scheduler->getClock());
62+
}
5863
});
5964

6065
}

src/Testing/HotObservable.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,22 @@ protected function _subscribe(ObserverInterface $observer): DisposableInterface
4848
{
4949
$currentObservable = $this;
5050

51-
$this->observers[] = $observer;
52-
$this->subscriptions[] = new Subscription($this->scheduler->getClock());
51+
$this->observers[] = $observer;
52+
$subscriptions = &$this->subscriptions;
53+
$index = null;
5354

54-
$subscriptions = &$this->subscriptions;
55+
if (!($observer instanceof MockHigherOrderObserver)) {
56+
$this->subscriptions[] = new Subscription($this->scheduler->getClock());
57+
$index = count($this->subscriptions) - 1;
58+
}
5559

56-
$index = count($this->subscriptions) - 1;
5760
$scheduler = $this->scheduler;
5861

5962
return new CallbackDisposable(function () use (&$currentObservable, $index, $observer, $scheduler, &$subscriptions) {
6063
$currentObservable->removeObserver($observer);
61-
$subscriptions[$index] = new Subscription($subscriptions[$index]->getSubscribed(), $scheduler->getClock());
64+
if (!($observer instanceof MockHigherOrderObserver)) {
65+
$subscriptions[$index] = new Subscription($subscriptions[$index]->getSubscribed(), $scheduler->getClock());
66+
}
6267
});
6368
}
6469

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rx\Testing;
4+
5+
class MockHigherOrderObserver extends MockObserver
6+
{
7+
}

src/Testing/MockObserver.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Rx\Notification\OnCompletedNotification;
88
use Rx\Notification\OnErrorNotification;
99
use Rx\Notification\OnNextNotification;
10+
use Rx\Notification\OnNextObservableNotification;
11+
use Rx\Observable;
1012
use Rx\ObserverInterface;
1113

1214
/**
@@ -16,32 +18,40 @@ class MockObserver implements ObserverInterface
1618
{
1719
private $scheduler;
1820
private $messages = [];
21+
private $startTime = 0;
1922

20-
public function __construct(TestScheduler $scheduler)
23+
public function __construct(TestScheduler $scheduler, int $startTime = 0)
2124
{
2225
$this->scheduler = $scheduler;
26+
$this->startTime = $startTime;
2327
}
2428

2529
public function onNext($value)
2630
{
31+
if ($value instanceof Observable) {
32+
$notification = new OnNextObservableNotification($value, $this->scheduler);
33+
} else {
34+
$notification = new OnNextNotification($value);
35+
}
36+
2737
$this->messages[] = new Recorded(
28-
$this->scheduler->getClock(),
29-
new OnNextNotification($value)
38+
$this->scheduler->getClock() - $this->startTime,
39+
$notification
3040
);
3141
}
3242

3343
public function onError(\Throwable $error)
3444
{
3545
$this->messages[] = new Recorded(
36-
$this->scheduler->getClock(),
46+
$this->scheduler->getClock() - $this->startTime,
3747
new OnErrorNotification($error)
3848
);
3949
}
4050

4151
public function onCompleted()
4252
{
4353
$this->messages[] = new Recorded(
44-
$this->scheduler->getClock(),
54+
$this->scheduler->getClock() - $this->startTime,
4555
new OnCompletedNotification()
4656
);
4757
}

0 commit comments

Comments
 (0)