|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rx\Functional\Operator; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Rx\Functional\FunctionalTestCase; |
| 7 | +use Rx\Testing\HotObservable; |
| 8 | +use Rx\Testing\TestScheduler; |
| 9 | +use Rx\Observable\ReturnObservable; |
| 10 | +use Rx\Observable\EmptyObservable; |
| 11 | +use Rx\Observable\ThrowObservable; |
| 12 | + |
| 13 | +class SelectTest extends FunctionalTestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @test |
| 17 | + */ |
| 18 | + public function exception_thrown_in_callable_is_not_catched() |
| 19 | + { |
| 20 | + $observable = new ReturnObservable(1); |
| 21 | + |
| 22 | + $called = false; |
| 23 | + $observable->select(function() { throw new Exception(); }) |
| 24 | + ->subscribeCallback(function() {}, function($ex) use (&$called) { $called = true; }); |
| 25 | + |
| 26 | + $this->assertTrue($called); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @test |
| 31 | + */ |
| 32 | + public function select_calls_on_completed() |
| 33 | + { |
| 34 | + $observable = new EmptyObservable(); |
| 35 | + |
| 36 | + $called = false; |
| 37 | + $observable->select('RxIdentity') |
| 38 | + ->subscribeCallback(function() {}, function() {}, function() use (&$called) { $called = true; }); |
| 39 | + |
| 40 | + $this->assertTrue($called); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @test |
| 45 | + */ |
| 46 | + public function select_calls_on_error() |
| 47 | + { |
| 48 | + $observable = new ThrowObservable(new Exception); |
| 49 | + |
| 50 | + $called = false; |
| 51 | + $observable->select('RxIdentity') |
| 52 | + ->subscribeCallback(function() {}, function() use (&$called) { $called = true; }); |
| 53 | + |
| 54 | + $this->assertTrue($called); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @test |
| 59 | + * @expectedException InvalidArgumentException |
| 60 | + */ |
| 61 | + public function select_expects_a_callable() |
| 62 | + { |
| 63 | + $observable = new ReturnObservable(1); |
| 64 | + $observable->select(42); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @test |
| 69 | + */ |
| 70 | + public function select_calls_selector() |
| 71 | + { |
| 72 | + $scheduler = $this->createTestScheduler(); |
| 73 | + $xs = new HotObservable($scheduler, array( |
| 74 | + onNext(100, 2), |
| 75 | + onNext(300, 21), |
| 76 | + onNext(500, 42), |
| 77 | + onNext(800, 84), |
| 78 | + onCompleted(820), |
| 79 | + )); |
| 80 | + |
| 81 | + $results = $scheduler->startWithCreate(function() use ($xs) { |
| 82 | + return $xs->select(function($elem) { return $elem * 2; }); |
| 83 | + }); |
| 84 | + |
| 85 | + $this->assertMessages(array( |
| 86 | + onNext(300, 42), |
| 87 | + onNext(500, 84), |
| 88 | + onNext(800, 168), |
| 89 | + onCompleted(820), |
| 90 | + ), $results->getMessages()); |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments