We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||
| 8 | class ConnectionBuilderFromPromisedTest extends AbstractConnectionBuilderTest |
||
| 9 | { |
||
| 10 | public function testReturnsAllElementsWithoutFilters() |
||
| 11 | { |
||
| 12 | $promise = ConnectionBuilder::connectionFromPromisedArray($this->promisedLetters(), []); |
||
| 13 | $expected = $this->getExpectedConnection($this->letters, false, false); |
||
| 14 | $this->assertEqualsFromPromised($expected, $promise); |
||
| 15 | } |
||
| 16 | |||
| 17 | public function testRespectsASmallerFirst() |
||
| 18 | { |
||
| 19 | $promise = ConnectionBuilder::connectionFromPromisedArray($this->promisedLetters(), ['first' => 2]); |
||
| 20 | $expected = $this->getExpectedConnection(['A', 'B'], false, true); |
||
| 21 | $this->assertEqualsFromPromised($expected, $promise); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param $invalidPromise |
||
| 26 | * @dataProvider invalidPromiseDataProvider |
||
| 27 | * |
||
| 28 | * @expectedException \InvalidArgumentException |
||
| 29 | * @expectedExceptionMessage This is not a valid promise. |
||
| 30 | */ |
||
| 31 | public function testInvalidPromise($invalidPromise) |
||
| 32 | { |
||
| 33 | ConnectionBuilder::connectionFromPromisedArray($invalidPromise, []); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function invalidPromiseDataProvider() |
||
| 37 | { |
||
| 38 | return [ |
||
| 39 | [new \stdClass()], |
||
| 40 | ['fake'], |
||
| 41 | [['fake']], |
||
| 42 | [false], |
||
| 43 | [true], |
||
| 44 | ]; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testRespectsASmallerFirstWhenSlicing() |
||
| 48 | { |
||
| 49 | $promise = ConnectionBuilder::connectionFromPromisedArraySlice( |
||
| 50 | $this->promisedLetters(['A', 'B', 'C']), |
||
| 51 | ['first' => 2], |
||
| 52 | [ |
||
| 53 | 'sliceStart' => 0, |
||
| 54 | 'arrayLength' => 5, |
||
| 55 | ] |
||
| 56 | ); |
||
| 57 | $expected = $this->getExpectedConnection(['A', 'B'], false, true); |
||
| 58 | $this->assertEqualsFromPromised($expected, $promise); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $invalidPromise |
||
| 63 | * @dataProvider invalidPromiseDataProvider |
||
| 64 | * |
||
| 65 | * @expectedException \InvalidArgumentException |
||
| 66 | * @expectedExceptionMessage This is not a valid promise. |
||
| 67 | */ |
||
| 68 | public function testInvalidPromiseWhenSlicing($invalidPromise) |
||
| 69 | { |
||
| 70 | ConnectionBuilder::connectionFromPromisedArraySlice($invalidPromise, [], []); |
||
| 71 | } |
||
| 72 | |||
| 73 | private function promisedLetters(array $letters = null) |
||
| 74 | { |
||
| 75 | return \React\Promise\resolve($letters ?: $this->letters); |
||
| 76 | } |
||
| 77 | |||
| 78 | private function assertEqualsFromPromised($expected, FulfilledPromise $promise) |
||
| 79 | { |
||
| 80 | $this->assertEquals($expected, self::await($promise)); |
||
| 81 | } |
||
| 82 | |||
| 83 | private static function await(FulfilledPromise $promise) |
||
| 84 | { |
||
| 85 | $resolvedValue = null; |
||
| 86 | $rejectedReason = null; |
||
| 87 | $promise->then( |
||
| 88 | function ($value) use (&$resolvedValue) { |
||
| 89 | $resolvedValue = $value; |
||
| 90 | }, |
||
| 91 | function ($reason) use (&$rejectedReason) { |
||
| 92 | $rejectedReason = $reason; |
||
| 93 | } |
||
| 94 | ); |
||
| 95 | |||
| 96 | if ($rejectedReason instanceof \Exception) { |
||
| 97 | throw $rejectedReason; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $resolvedValue; |
||
| 101 | } |
||
| 102 | } |
||
| 103 |