/** * @test */ public function interval_relative_time_Negative() { $results = $this->scheduler->startWithDispose(function () { return new IntervalObservable(-1, $this->scheduler); }, 210); $this->assertMessages([onNext(201, 0), onNext(202, 1), onNext(203, 2), onNext(204, 3), onNext(205, 4), onNext(206, 5), onNext(207, 6), onNext(208, 7), onNext(209, 8)], $results->getMessages()); }
/** * @test */ public function range_dispose() { $results = $this->scheduler->startWithDispose(function () { return Observable::range(-10, 5, $this->scheduler); }, 204); $this->assertMessages([onNext(201, -10), onNext(202, -9), onNext(203, -8)], $results->getMessages()); }
/** * @test */ public function timer_one_shot_relative_time_negative() { $results = $this->scheduler->startWithCreate(function () { return new TimerObservable(-1, $this->scheduler); }); $this->assertMessages([onNext(201, 0), onCompleted(201)], $results->getMessages()); }
/** * @test */ public function fromArray_dispose() { $xs = Observable::fromArray(['foo', 'bar', 'baz']); $results = $this->scheduler->startWithDispose(function () use($xs) { return $xs; }, 202); $this->assertMessages([onNext(201, 'foo')], $results->getMessages()); }
/** * @test */ public function switchLatest_Dispose() { $xs = $this->createHotObservable([onNext(300, $this->createColdObservable([onNext(10, 101), onNext(20, 102), onNext(110, 103), onNext(120, 104), onNext(210, 105), onNext(220, 106), onCompleted(230)])), onNext(400, $this->createColdObservable([onNext(10, 201), onNext(20, 202), onNext(30, 203), onNext(40, 204), onCompleted(50)])), onNext(500, $this->createColdObservable([onNext(10, 301), onNext(20, 302), onNext(30, 303), onNext(40, 304), onCompleted(150)])), onCompleted(600)]); $results = $this->scheduler->startWithDispose(function () use($xs) { return $xs->switchLatest(); }, 500); $this->assertMessages([onNext(310, 101), onNext(320, 102), onNext(410, 201), onNext(420, 202), onNext(430, 203), onNext(440, 204)], $results->getMessages()); }
/** * Adapted from RxJS * * @test */ public function sum_number_never() { $xs = $this->createHotObservable([onNext(150, 1)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->sum(); }); $this->assertMessages([], $results->getMessages()); }
/** * @test */ public function timestamp_dispose() { $xs = $this->createHotObservable([onNext(150, 1), onNext(210, 2), onNext(230, 3), onNext(260, 4), onNext(300, 5), onNext(350, 6), onCompleted(400)]); $results = $this->scheduler->startWithDispose(function () use($xs) { return $xs->timestamp($this->scheduler); }, 275); $this->assertMessages([onNext(210, new Timestamped(210, 2)), onNext(230, new Timestamped(230, 3)), onNext(260, new Timestamped(260, 4))], $results->getMessages()); }
/** * @test */ public function it_schedules_all_elements_from_the_array() { $xs = BaseObservable::fromArray(array('foo', 'bar', 'baz'), $this->scheduler); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs; }); $this->assertCount(4, $results->getMessages()); $this->assertMessages(array(onNext(201, 'foo'), onNext(202, 'bar'), onNext(203, 'baz'), onCompleted(204)), $results->getMessages()); }
/** * @test */ public function startWith_error() { $error = new \Exception(); $xs = $this->createHotObservable([onNext(150, 1), onError(250, $error)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->startWithArray([1, 2, 3]); }); $this->assertMessages([onNext(201, 1), onNext(202, 2), onNext(203, 3), onError(250, $error)], $results->getMessages()); }
/** * @test */ public function pluck_array_numeric_index() { $xs = $this->createHotObservable([onNext(180, [-1, -1, -1, -1]), onNext(210, [4, 3, 2, 1]), onNext(240, [4, 3, 20, 10]), onNext(290, [4, 3, 200, 100]), onNext(350, [4, 3, 2000, 1000]), onCompleted(400)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->pluck(2); }); $this->assertMessages([onNext(210, 2), onNext(240, 20), onNext(290, 200), onNext(350, 2000), onCompleted(400)], $results->getMessages()); $this->assertSubscriptions([subscribe(200, 400)], $xs->getSubscriptions()); }
/** * @test */ public function subscribeOn_never() { $xs = $this->createHotObservable([onNext(150, 1)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->subscribeOn($this->scheduler); }); $this->assertMessages([], $results->getMessages()); $this->assertSubscriptions([subscribe(201, 1001)], $xs->getSubscriptions()); }
/** * @test */ public function it_skips_multiple_values() { $scheduler = $this->createTestScheduler(); $xs = $this->createHotObservable(array(onNext(300, 21), onNext(500, 42), onNext(800, 84), onNext(850, 168), onCompleted(870))); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->skip(2); }); $this->assertMessages(array(onNext(800, 84), onNext(850, 168), onCompleted(870)), $results->getMessages()); }
/** * @test */ public function start_action_number() { $results = $this->scheduler->startWithCreate(function () { return Observable::start(function () { return 1; }, $this->scheduler); }); $this->assertMessages([onNext(200, 1), onCompleted(200)], $results->getMessages()); }
/** * @test */ public function take_zero_calls_on_completed() { $scheduler = $this->createTestScheduler(); $xs = $this->createHotObservable(array(onNext(300, 21), onNext(500, 42), onNext(800, 84), onCompleted(820))); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->take(0); }); $this->assertMessages(array(onCompleted(201)), $results->getMessages()); }
public function testToArrayDisposed() { $xs = $this->createHotObservable([onNext(110, 1), onNext(220, 2), onNext(330, 3), onNext(440, 4), onNext(550, 5)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->toArray(); }); $this->assertMessages([], $results->getMessages()); $this->assertSubscriptions([subscribe(200, 1000)], $xs->getSubscriptions()); }
/** * @test */ public function it_calls_relative_to_subscribe_time() { $xs = $this->createColdObservable(array(onNext(50, "foo"), onNext(75, "Bar"), onCompleted(105))); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs; }); $this->assertCount(3, $results->getMessages()); $this->assertMessages(array(onNext(250, "foo"), onNext(275, "Bar"), onCompleted(305)), $results->getMessages()); }
/** * @test */ public function generator_dispose() { $generator = $this->genOneToThree(); $xs = new \Rx\Observable\IteratorObservable($generator); $results = $this->scheduler->startWithDispose(function () use($xs) { return $xs; }, 202); $this->assertMessages([onNext(201, 1)], $results->getMessages()); }
/** * @test */ public function from_promise_success() { $source = PromiseFactory::toObservable(function () { return Promise::resolved(42); }); $results = $this->scheduler->startWithCreate(function () use($source) { return $source; }); $this->assertMessages(array(onNext(200, 42), onCompleted(200)), $results->getMessages()); }
/** * @test */ public function select_calls_selector() { $xs = $this->createHotObservable(array(onNext(100, 2), onNext(300, 21), onNext(500, 42), onNext(800, 84), onCompleted(820))); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->select(function ($elem) { return $elem * 2; }); }); $this->assertMessages(array(onNext(300, 42), onNext(500, 84), onNext(800, 168), onCompleted(820)), $results->getMessages()); }
/** * @test */ public function doOnNext_should_call_after_resubscription() { $xs = $this->createColdObservable([onNext(10, 1), onCompleted(20)]); $messages = []; $xs->doOnNext(function ($x) use(&$messages) { $messages[] = onNext($this->scheduler->getClock(), $x); })->repeat(2)->subscribeCallback(null, null, null, $this->scheduler); $this->scheduler->start(); $this->assertMessages([onNext(10, 1), onNext(30, 1)], $messages); }
/** * @test */ public function concatAll_timer_missing_item() { $xs = $this->createHotObservable([onNext(201, 0), onNext(206, 1), onNext(211, 2), onCompleted(212)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->map(function ($x) { return Observable::timer(5)->mapTo($x); })->concatAll(); }); $this->assertMessages([onNext(206, 0), onNext(211, 1), onNext(216, 2), onCompleted(216)], $results->getMessages()); }
/** * @test */ public function it_passes_on_error_from_sources() { $xs = $this->createColdObservable(array(onNext(100, 4), onNext(200, 2), onNext(300, 3), onNext(400, 1), onCompleted(500))); $ys = $this->createColdObservable(array(onNext(50, $xs), onError(200, new Exception()), onCompleted(250))); $results = $this->scheduler->startWithCreate(function () use($ys) { return Observable::mergeAll($ys); }); $this->assertMessages(array(onNext(350, 4), onError(400, new Exception())), $results->getMessages()); $this->assertSubscriptions(array(subscribe(250, 400)), $xs->getSubscriptions()); $this->assertSubscriptions(array(subscribe(200, 400)), $ys->getSubscriptions()); }
/** * @test */ public function it_passes_the_last_on_complete() { $xs = $this->createColdObservable(array(onNext(100, 4), onNext(200, 2), onNext(300, 3), onNext(400, 1), onCompleted(500))); $ys = $this->createColdObservable(array(onNext(50, 'foo'), onNext(100, 'bar'), onNext(150, 'baz'), onNext(200, 'qux'), onCompleted(250))); $results = $this->scheduler->startWithCreate(function () use($xs, $ys) { return $xs->merge($ys); }); $this->assertMessages(array(onNext(250, 'foo'), onNext(300, 4), onNext(300, 'bar'), onNext(350, 'baz'), onNext(400, 2), onNext(400, 'qux'), onNext(500, 3), onNext(600, 1), onCompleted(700)), $results->getMessages()); $this->assertSubscriptions(array(subscribe(200, 700)), $xs->getSubscriptions()); $this->assertSubscriptions(array(subscribe(200, 450)), $ys->getSubscriptions()); }
/** * @test */ public function doOnCompleted_should_be_called() { $xs = $this->createHotObservable([onNext(150, 1), onNext(210, 2), onNext(220, 3), onCompleted(250)]); $called = 0; $this->scheduler->startWithCreate(function () use($xs, &$called) { return $xs->doOnCompleted(function () use(&$called) { $called++; }); }); $this->assertEquals(1, $called); }
/** * @test */ public function flatMapTo_it_passes_on_error() { $xs = $this->createColdObservable(array(onNext(100, 4), onNext(200, 2), onNext(300, 3), onNext(400, 1), onCompleted(510))); $ys = $this->createColdObservable(array(onNext(50, 'foo'), onNext(100, 'bar'), onNext(150, 'baz'), onError(210, new Exception()), onCompleted(250))); $results = $this->scheduler->startWithCreate(function () use($xs, $ys) { return $xs->flatMapTo($ys); }); $this->assertMessages(array(onNext(350, "foo"), onNext(400, "bar"), onNext(450, "baz"), onNext(450, "foo"), onNext(500, "bar"), onError(510, new Exception())), $results->getMessages()); $this->assertSubscriptions(array(subscribe(200, 510)), $xs->getSubscriptions()); $this->assertSubscriptions(array(subscribe(300, 510), subscribe(400, 510), subscribe(500, 510)), $ys->getSubscriptions()); }
/** * @test */ public function defaultIfEmpty_empty_2() { $xs = $this->createHotObservable([onCompleted(420)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->defaultIfEmpty(new ReturnObservable(-1)); }); // Note: these tests differ from the RxJS tests that they were based on because RxJS was // explicitly using the immediate scheduler on subscribe internally. When we pass the // proper scheduler in, the subscription gets scheduled which requires an extra tick. $this->assertMessages([onNext(421, -1), onCompleted(421)], $results->getMessages()); $this->assertSubscriptions([subscribe(200, 420)], $xs->getSubscriptions()); }
/** * @test */ public function doOnNext_should_see_all_values() { $xs = $this->createHotObservable([onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250)]); $i = 0; $sum = 2 + 3 + 4 + 5; $this->scheduler->startWithCreate(function () use($xs, &$i, &$sum) { return $xs->doOnNext(function ($x) use(&$i, &$sum) { $i++; return $sum -= $x; }); }); $this->assertEquals(4, $i); $this->assertEquals(0, $sum); }
/** * @test */ public function race_throws_before_election() { $sourceNotDisposed = false; $error = new \Exception('error'); $o1 = $this->createHotObservable([onNext(150, 1), onError(210, $error)]); $o2 = $this->createHotObservable([onNext(150, 1), onNext(220, 3), onCompleted(250)])->doOnNext(function () use(&$sourceNotDisposed) { $sourceNotDisposed = true; }); $results = $this->scheduler->startWithCreate(function () use($o1, $o2) { return Observable::race([$o1, $o2]); }); $this->assertMessages([onError(210, $error)], $results->getMessages()); $this->assertFalse($sourceNotDisposed); }
/** * @test */ public function doOnError_should_see_errors() { $ex = new RuntimeException('boom!'); $xs = $this->createHotObservable([onNext(150, 1), onNext(210, 2), onError(220, $ex), onCompleted(250)]); $called = 0; $error = null; $this->scheduler->startWithCreate(function () use($xs, &$called, &$error) { return $xs->doOnError(function ($err) use(&$called, &$error) { $called++; $error = $err; }); }); $this->assertEquals(1, $called); $this->assertSame($ex, $error); }
/** * @test */ public function defer_dispose() { $invoked = 0; $xs = null; $results = $this->scheduler->startWithCreate(function () use(&$invoked, &$xs) { return Observable::defer(function () use(&$invoked, &$xs) { $invoked++; $xs = $this->createColdObservable([onNext(100, $this->scheduler->getClock()), onNext(200, $invoked), onNext(1100, 1000)]); return $xs; }); }); $this->assertMessages([onNext(300, 200), onNext(400, 1)], $results->getMessages()); $this->assertEquals(1, $invoked); $this->assertSubscriptions([subscribe(200, 1000)], $xs->getSubscriptions()); }