Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Higher-Order Marble tests #153

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1a1b2e1
Preliminary marble testing
mbonneau Feb 4, 2017
fbeff95
more marble testing features
martinsik Feb 17, 2017
16e7304
fix correct type for str_repeat()
martinsik Feb 17, 2017
637d3fe
added marker groups support
martinsik Feb 17, 2017
654614a
Added expectObservable and expectSubscriptions to FunctionalTestCase
davidwdan Feb 18, 2017
4b91912
Added exception message support to expectObservable()->toBe()
davidwdan Feb 18, 2017
b2a3ae6
Added dispose support to expectObservable()
davidwdan Feb 18, 2017
3ea6d38
Rename MarbleDiagramError to MarbleDiagramException
mbonneau Feb 20, 2017
6147c65
Grouped marbles now happen at the same time
mbonneau Feb 20, 2017
a619b72
Grouped subscription marbles happen at the same time
mbonneau Feb 20, 2017
6e35541
Added interfaces so we get type hinting for `toBe`
davidwdan Feb 20, 2017
416034c
Fixed continues to actually continue the loop
davidwdan Feb 20, 2017
14b7fd3
Merge branch '2.x' of github.com:ReactiveX/RxPHP into marbleTest
davidwdan Mar 10, 2017
c1d2d4a
Cherry picked higher-order observable test from v1
davidwdan Mar 10, 2017
08b0e0a
use single instance of TestScheduler in all Notification objects
martinsik Mar 11, 2017
eb24667
Materialize the inner observable when converting the OnNextNotificati…
davidwdan Mar 11, 2017
8d219fa
Simplified `materializeObservable`
davidwdan Mar 11, 2017
c153965
Removed unused use statement
davidwdan Mar 11, 2017
ee4ec61
remove redundant test
martinsik Mar 12, 2017
58d185d
`toBe` now does a string comparison
davidwdan Mar 12, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Notification/OnNextNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rx\Notification;

use Rx\Observable;
use Rx\ObserverInterface;
use Rx\Notification;

Expand All @@ -30,6 +31,11 @@ protected function doAccept($onNext, $onError, $onCompleted)

public function __toString(): string
{
if ($this->value instanceof Observable) {
$messages = materializeObservable($this->value);
return '[' . implode(', ', $messages) . ']';
}

return 'OnNext(' . json_encode($this->value) . ')';
}

Expand Down
12 changes: 6 additions & 6 deletions src/Testing/ColdObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ public function __construct(TestScheduler $scheduler, array $messages = [])

protected function _subscribe(ObserverInterface $observer): DisposableInterface
{
$currentObservable = $this;
$disposable = new CompositeDisposable();
$scheduler = $this->scheduler;
$isDisposed = false;
$this->subscriptions[] = new Subscription($this->scheduler->getClock());
$index = count($this->subscriptions) - 1;

$currentObservable = $this;
$disposable = new CompositeDisposable();
$scheduler = $this->scheduler;
$isDisposed = false;

foreach ($this->messages as $message) {
$notification = $message->getValue();
$time = $message->getTime();
Expand All @@ -53,7 +52,8 @@ protected function _subscribe(ObserverInterface $observer): DisposableInterface
$subscriptions = &$this->subscriptions;

return new CallbackDisposable(function () use (&$currentObservable, $index, $observer, $scheduler, &$subscriptions, &$isDisposed) {
$isDisposed = true;
$isDisposed = true;

$subscriptions[$index] = new Subscription($subscriptions[$index]->getSubscribed(), $scheduler->getClock());
});

Expand Down
11 changes: 4 additions & 7 deletions src/Testing/HotObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ public function __construct(TestScheduler $scheduler, array $messages)

protected function _subscribe(ObserverInterface $observer): DisposableInterface
{
$currentObservable = $this;

$currentObservable = $this;
$this->observers[] = $observer;
$subscriptions = &$this->subscriptions;
$this->subscriptions[] = new Subscription($this->scheduler->getClock());

$subscriptions = &$this->subscriptions;

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

return new CallbackDisposable(function () use (&$currentObservable, $index, $observer, $scheduler, &$subscriptions) {
$currentObservable->removeObserver($observer);
Expand Down
10 changes: 6 additions & 4 deletions src/Testing/MockObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,34 @@ class MockObserver implements ObserverInterface
{
private $scheduler;
private $messages = [];
private $startTime = 0;

public function __construct(TestScheduler $scheduler)
public function __construct(TestScheduler $scheduler, int $startTime = 0)
{
$this->scheduler = $scheduler;
$this->startTime = $startTime;
}

public function onNext($value)
{
$this->messages[] = new Recorded(
$this->scheduler->getClock(),
$this->scheduler->getClock() - $this->startTime,
new OnNextNotification($value)
);
}

public function onError(\Throwable $error)
{
$this->messages[] = new Recorded(
$this->scheduler->getClock(),
$this->scheduler->getClock() - $this->startTime,
new OnErrorNotification($error)
);
}

public function onCompleted()
{
$this->messages[] = new Recorded(
$this->scheduler->getClock(),
$this->scheduler->getClock() - $this->startTime,
new OnCompletedNotification()
);
}
Expand Down
1 change: 0 additions & 1 deletion src/Testing/TestScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Rx\Disposable\EmptyDisposable;
use Rx\DisposableInterface;
use Rx\ObserverInterface;
use Rx\Scheduler;
use Rx\Scheduler\VirtualTimeScheduler;

class TestScheduler extends VirtualTimeScheduler
Expand Down
16 changes: 16 additions & 0 deletions test/Rx/Functional/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ abstract class FunctionalTestCase extends TestCase
/** @var TestScheduler */
protected $scheduler;

/** @var TestScheduler */
static protected $globalScheduler;

const TIME_FACTOR = 10;

public function setup()
{
$this->scheduler = $this->createTestScheduler();
self::$globalScheduler = $this->scheduler;
}

static function getScheduler() {
return self::$globalScheduler;
}

/**
Expand All @@ -32,6 +40,8 @@ public function setup()
*/
public function assertMessages(array $expected, array $recorded)
{
$this->scheduler->start();

if (count($expected) !== count($recorded)) {
$this->fail(sprintf('Expected message count %d does not match actual count %d.', count($expected), count($recorded)));
}
Expand All @@ -51,6 +61,8 @@ public function assertMessages(array $expected, array $recorded)
*/
public function assertMessagesNotEqual(array $expected, array $recorded)
{
$this->scheduler->start();

if (count($expected) !== count($recorded)) {
$this->assertTrue(true);
return;
Expand All @@ -68,6 +80,8 @@ public function assertMessagesNotEqual(array $expected, array $recorded)

public function assertSubscription(HotObservable $observable, Subscription $expected)
{
$this->scheduler->start();

$subscriptionCount = count($observable->getSubscriptions());

if ($subscriptionCount === 0) {
Expand All @@ -89,6 +103,8 @@ public function assertSubscription(HotObservable $observable, Subscription $expe

public function assertSubscriptions(array $expected, array $recorded)
{
$this->scheduler->start();

if (count($expected) !== count($recorded)) {
$this->fail(sprintf('Expected subscription count %d does not match actual count %d.', count($expected), count($recorded)));
}
Expand Down
Loading