|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Promise; |
| 4 | + |
| 5 | +/** |
| 6 | + * @group Promise |
| 7 | + * @group LazyPromise |
| 8 | + */ |
| 9 | +class LazyPromiseTest extends TestCase |
| 10 | +{ |
| 11 | + /** @test */ |
| 12 | + public function shouldNotCallFactoryIfThenIsNotInvoked() |
| 13 | + { |
| 14 | + $factory = $this->createCallableMock(); |
| 15 | + $factory |
| 16 | + ->expects($this->never()) |
| 17 | + ->method('__invoke'); |
| 18 | + |
| 19 | + new LazyPromise($factory); |
| 20 | + } |
| 21 | + |
| 22 | + /** @test */ |
| 23 | + public function shouldCallFactoryIfThenIsInvoked() |
| 24 | + { |
| 25 | + $factory = $this->createCallableMock(); |
| 26 | + $factory |
| 27 | + ->expects($this->once()) |
| 28 | + ->method('__invoke'); |
| 29 | + |
| 30 | + $p = new LazyPromise($factory); |
| 31 | + $p->then(); |
| 32 | + } |
| 33 | + |
| 34 | + /** @test */ |
| 35 | + public function shouldReturnPromiseFromFactory() |
| 36 | + { |
| 37 | + $factory = $this->createCallableMock(); |
| 38 | + $factory |
| 39 | + ->expects($this->once()) |
| 40 | + ->method('__invoke') |
| 41 | + ->will($this->returnValue(new FulfilledPromise(1))); |
| 42 | + |
| 43 | + $fulfilledHandler = $this->createCallableMock(); |
| 44 | + $fulfilledHandler |
| 45 | + ->expects($this->once()) |
| 46 | + ->method('__invoke') |
| 47 | + ->with($this->identicalTo(1)); |
| 48 | + |
| 49 | + $p = new LazyPromise($factory); |
| 50 | + |
| 51 | + $p->then($fulfilledHandler); |
| 52 | + } |
| 53 | + |
| 54 | + /** @test */ |
| 55 | + public function shouldReturnPromiseIfFactoryReturnsNull() |
| 56 | + { |
| 57 | + $factory = $this->createCallableMock(); |
| 58 | + $factory |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('__invoke') |
| 61 | + ->will($this->returnValue(null)); |
| 62 | + |
| 63 | + $p = new LazyPromise($factory); |
| 64 | + $this->assertInstanceOf('React\\Promise\\PromiseInterface', $p->then()); |
| 65 | + } |
| 66 | + |
| 67 | + /** @test */ |
| 68 | + public function shouldReturnRejectedPromiseIfFactoryThrowsException() |
| 69 | + { |
| 70 | + $exception = new \Exception(); |
| 71 | + |
| 72 | + $factory = $this->createCallableMock(); |
| 73 | + $factory |
| 74 | + ->expects($this->once()) |
| 75 | + ->method('__invoke') |
| 76 | + ->will($this->throwException($exception)); |
| 77 | + |
| 78 | + $errorHandler = $this->createCallableMock(); |
| 79 | + $errorHandler |
| 80 | + ->expects($this->once()) |
| 81 | + ->method('__invoke') |
| 82 | + ->with($this->identicalTo($exception)); |
| 83 | + |
| 84 | + $p = new LazyPromise($factory); |
| 85 | + |
| 86 | + $p->then($this->expectCallableNever(), $errorHandler); |
| 87 | + } |
| 88 | +} |
0 commit comments