Skip to content

Commit

Permalink
Added more tests (#117)
Browse files Browse the repository at this point in the history
Removed some unused code
Misc cleanup
  • Loading branch information
davidwdan authored and mbonneau committed Jan 9, 2017
1 parent 9391ef8 commit 16f9202
Show file tree
Hide file tree
Showing 14 changed files with 597 additions and 45 deletions.
21 changes: 4 additions & 17 deletions src/Disposable/BinaryDisposable.php
Expand Up @@ -30,9 +30,6 @@ public function __construct(DisposableInterface $first, DisposableInterface $sec
$this->second = $second;
}

/**
*
*/
public function dispose()
{
if ($this->isDisposed) {
Expand All @@ -41,24 +38,14 @@ public function dispose()

$this->isDisposed = true;

$old1 = $this->first;
$this->first = null;
if ($old1) {
$old1->dispose();
}
$this->first->dispose();
$this->second->dispose();

$old2 = $this->second;
$this->first = null;
$this->second = null;
if ($old2) {
$old2->dispose();
}

}

/**
* @return bool
*/
public function isDisposed()
public function isDisposed(): bool
{
return $this->isDisposed;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Disposable/CompositeDisposable.php
Expand Up @@ -39,7 +39,7 @@ public function add(DisposableInterface $disposable)
}
}

public function remove(DisposableInterface $disposable)
public function remove(DisposableInterface $disposable): bool
{
if ($this->isDisposed) {
return false;
Expand All @@ -58,7 +58,7 @@ public function remove(DisposableInterface $disposable)
return true;
}

public function contains(DisposableInterface $disposable)
public function contains(DisposableInterface $disposable): bool
{
return in_array($disposable, $this->disposables, true);
}
Expand Down
9 changes: 3 additions & 6 deletions src/Notification.php
Expand Up @@ -8,16 +8,13 @@
abstract class Notification
{
private $kind;
private $hasValue;

/**
* @param mixed $kind Kind of notification
* @param boolean $hasValue If the notification has a value
*/
public function __construct($kind, $hasValue = false)
public function __construct($kind)
{
$this->kind = $kind;
$this->hasValue = $hasValue;
$this->kind = $kind;
}

public function accept($observerOrOnNext, $onError = null, $onCompleted = null)
Expand All @@ -31,7 +28,7 @@ public function accept($observerOrOnNext, $onError = null, $onCompleted = null)
return $this->doAccept($observerOrOnNext, $onError, $onCompleted);
}

public function equals($other)
public function equals($other): bool
{
return (string)$this === (string)$other;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/OnCompletedNotification.php
Expand Up @@ -22,7 +22,7 @@ protected function doAccept($onNext, $onError, $onCompleted)
$onCompleted();
}

public function __toString()
public function __toString(): string
{
return 'OnCompleted()';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notification/OnErrorNotification.php
Expand Up @@ -26,7 +26,7 @@ protected function doAccept($onNext, $onError, $onCompleted)
$onError($this->exception);
}

public function __toString()
public function __toString(): string
{
return 'OnError(' . get_class($this->exception) . ')';
}
Expand Down
10 changes: 5 additions & 5 deletions src/Notification/OnNextNotification.php
Expand Up @@ -11,7 +11,7 @@ class OnNextNotification extends Notification

public function __construct($value)
{
parent::__construct('N', true);
parent::__construct('N');

$this->value = $value;
}
Expand All @@ -26,19 +26,19 @@ protected function doAccept($onNext, $onError, $onCompleted)
$onNext($this->value);
}

public function __toString()
public function __toString(): string
{
return 'OnNext(' . json_encode($this->value) . ')';
}
public function equals($other)

public function equals($other): bool
{
if (($other instanceof $this) && is_object($this->value) && is_object($other->value)) {
if ($this->value instanceof $other->value && method_exists($this->value, "equals")) {
return $this->value->equals($other->value);
}
}

return (string)$this === (string)$other;
}
}
4 changes: 2 additions & 2 deletions src/Observable.php
Expand Up @@ -220,7 +220,7 @@ public static function never(): NeverObservable
/**
* Returns an observable sequence that terminates with an exception.
*
* @param \Exception $error
* @param \Throwable $error
* @param SchedulerInterface $scheduler
* @return ErrorObservable The observable sequence that terminates exceptionally with the specified exception object.
*
Expand Down Expand Up @@ -1139,7 +1139,7 @@ public function concat(ObservableInterface $observable): Observable
public function concatMap(callable $selector, callable $resultSelector = null): Observable
{
return $this->lift(function () use ($selector, $resultSelector) {
return new ConcatMapOperator($selector, $resultSelector);
return new ConcatMapOperator(new ObservableFactoryWrapper($selector), $resultSelector);
});
}

Expand Down
8 changes: 0 additions & 8 deletions src/Operator/ConcatMapOperator.php
Expand Up @@ -3,15 +3,11 @@
namespace Rx\Operator;

use Rx\DisposableInterface;
use Rx\Observable;
use Rx\ObservableInterface;
use Rx\ObserverInterface;

final class ConcatMapOperator implements OperatorInterface
{
/** @var int */
private $count;

/** @var callable */
private $selector;

Expand All @@ -31,10 +27,6 @@ public function __invoke(ObservableInterface $observable, ObserverInterface $obs
try {
$result = ($this->selector)($value, $index, $observable);

if (!$result instanceof Observable) {
throw new \Exception('concatMap Error: You must return an Observable from the concatMap selector');
}

if ($this->resultSelector) {
return $result->mapWithIndex(function ($innerIndex, $innerValue) use ($value, $index) {
return ($this->resultSelector)($value, $innerValue, $index, $innerIndex);
Expand Down
76 changes: 76 additions & 0 deletions test/Rx/Disposable/BinaryDisposableTest.php
@@ -0,0 +1,76 @@
<?php


namespace Rx\Disposable;

use Rx\TestCase;

class BinaryDisposableTest extends TestCase
{
/**
* @test
*/
public function it_disposes_the_binary_disposable()
{
$disposed1 = false;

$d1 = new CallbackDisposable(function () use (&$disposed1) {
$disposed1 = true;
});

$disposed2 = false;

$d2 = new CallbackDisposable(function () use (&$disposed2) {
$disposed2 = true;
});

$disposable = new BinaryDisposable($d1, $d2);

$this->assertFalse($disposed1);
$this->assertFalse($disposed2);

$disposable->dispose();

$this->assertTrue($disposed1);
$this->assertTrue($disposed2);

$this->assertTrue($disposable->isDisposed());
}

/**
* @test
*/
public function it_does_nothing_if_disposed_twice()
{
$disposed1 = 0;

$d1 = new CallbackDisposable(function () use (&$disposed1) {
$disposed1++;
});

$disposed2 = 0;

$d2 = new CallbackDisposable(function () use (&$disposed2) {
$disposed2++;
});

$disposable = new BinaryDisposable($d1, $d2);

$this->assertEquals(0, $disposed1);
$this->assertEquals(0, $disposed2);

$disposable->dispose();

$this->assertEquals(1, $disposed1);
$this->assertEquals(1, $disposed2);

$this->assertTrue($disposable->isDisposed());

$disposable->dispose();

$this->assertEquals(1, $disposed1);
$this->assertEquals(1, $disposed2);

$this->assertTrue($disposable->isDisposed());
}
}
33 changes: 31 additions & 2 deletions test/Rx/Disposable/CompositeDisposableTest.php
Expand Up @@ -67,6 +67,31 @@ public function disposing_disposes_all_disposables()
$this->assertTrue($disposed2);
}

/**
* @test
*/
public function disposing_disposes_all_disposables_only_once()
{
$disposed1 = 0;
$disposed2 = 0;
$d1 = new CallbackDisposable(function() use (&$disposed1){ $disposed1++; });
$d2 = new CallbackDisposable(function() use (&$disposed2){ $disposed2++; });
$disposable = new CompositeDisposable(array($d1, $d2));

$this->assertEquals(0, $disposed1);
$this->assertEquals(0, $disposed2);

$disposable->dispose();

$this->assertEquals(1, $disposed1);
$this->assertEquals(1, $disposed2);

$disposable->dispose();

$this->assertEquals(1, $disposed1);
$this->assertEquals(1, $disposed2);
}

/**
* @test
*/
Expand Down Expand Up @@ -124,9 +149,11 @@ public function removing_when_disposed_has_no_effect()
$disposed1 = false;
$d1 = new CallbackDisposable(function() use (&$disposed1){ $disposed1 = true; });

$disposable->remove($d1);
$removed = $disposable->remove($d1);

$this->assertFalse($disposed1);

$this->assertFalse($removed);
}

/**
Expand All @@ -139,9 +166,11 @@ public function removing_a_disposable_that_is_not_contained_has_no_effect()
$disposed1 = false;
$d1 = new CallbackDisposable(function() use (&$disposed1){ $disposed1 = true; });

$disposable->remove($d1);
$removed = $disposable->remove($d1);

$this->assertFalse($disposed1);

$this->assertFalse($removed);
}

/**
Expand Down
68 changes: 68 additions & 0 deletions test/Rx/Disposable/ScheduledDisposableTest.php
@@ -0,0 +1,68 @@
<?php


namespace Rx\Disposable;

use Rx\TestCase;
use Rx\Testing\TestScheduler;

class ScheduledDisposableTest extends TestCase
{
/**
* @test
*/
public function it_disposes_the_scheduled_disposable()
{
$disposed1 = false;

$d1 = new CallbackDisposable(function () use (&$disposed1) {
$disposed1 = true;
});

$scheduler = new TestScheduler();

$disposable = new ScheduledDisposable($scheduler, $d1);

$this->assertFalse($disposed1);

$disposable->dispose();

$this->assertFalse($disposed1);

$scheduler->start();

$this->assertTrue($disposed1);
}

/**
* @test
*/
public function it_does_nothing_if_disposed_twice()
{
$disposed1 = 0;

$d1 = new CallbackDisposable(function () use (&$disposed1) {
$disposed1++;
});

$scheduler = new TestScheduler();

$disposable = new ScheduledDisposable($scheduler, $d1);

$this->assertEquals(0, $disposed1);

$disposable->dispose();

$this->assertEquals(0, $disposed1);

$scheduler->start();

$this->assertEquals(1, $disposed1);

$disposable->dispose();

$this->assertEquals(1, $disposed1);

$scheduler->start();
}
}

0 comments on commit 16f9202

Please sign in to comment.