1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Relay\Connection\Output; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder; |
15
|
|
|
use React\Promise\FulfilledPromise; |
16
|
|
|
|
17
|
|
|
class ConnectionBuilderFromPromisedTest extends AbstractConnectionBuilderTest |
18
|
|
|
{ |
19
|
|
|
public function testReturnsAllElementsWithoutFilters() |
20
|
|
|
{ |
21
|
|
|
$promise = ConnectionBuilder::connectionFromPromisedArray($this->promisedLetters(), []); |
22
|
|
|
$expected = $this->getExpectedConnection($this->letters, false, false); |
23
|
|
|
$this->assertEqualsFromPromised($expected, $promise); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testRespectsASmallerFirst() |
27
|
|
|
{ |
28
|
|
|
$promise = ConnectionBuilder::connectionFromPromisedArray($this->promisedLetters(), ['first' => 2]); |
29
|
|
|
$expected = $this->getExpectedConnection(['A', 'B'], false, true); |
30
|
|
|
$this->assertEqualsFromPromised($expected, $promise); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $invalidPromise |
35
|
|
|
* @dataProvider invalidPromiseDataProvider |
36
|
|
|
* |
37
|
|
|
* @expectedException \InvalidArgumentException |
38
|
|
|
* @expectedExceptionMessage This is not a valid promise. |
39
|
|
|
*/ |
40
|
|
|
public function testInvalidPromise($invalidPromise) |
41
|
|
|
{ |
42
|
|
|
ConnectionBuilder::connectionFromPromisedArray($invalidPromise, []); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function invalidPromiseDataProvider() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
[new \stdClass()], |
49
|
|
|
['fake'], |
50
|
|
|
[['fake']], |
51
|
|
|
[false], |
52
|
|
|
[true], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testRespectsASmallerFirstWhenSlicing() |
57
|
|
|
{ |
58
|
|
|
$promise = ConnectionBuilder::connectionFromPromisedArraySlice( |
59
|
|
|
$this->promisedLetters(['A', 'B', 'C']), |
60
|
|
|
['first' => 2], |
61
|
|
|
[ |
62
|
|
|
'sliceStart' => 0, |
63
|
|
|
'arrayLength' => 5, |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
$expected = $this->getExpectedConnection(['A', 'B'], false, true); |
67
|
|
|
$this->assertEqualsFromPromised($expected, $promise); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $invalidPromise |
72
|
|
|
* @dataProvider invalidPromiseDataProvider |
73
|
|
|
* |
74
|
|
|
* @expectedException \InvalidArgumentException |
75
|
|
|
* @expectedExceptionMessage This is not a valid promise. |
76
|
|
|
*/ |
77
|
|
|
public function testInvalidPromiseWhenSlicing($invalidPromise) |
78
|
|
|
{ |
79
|
|
|
ConnectionBuilder::connectionFromPromisedArraySlice($invalidPromise, [], []); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function promisedLetters(array $letters = null) |
83
|
|
|
{ |
84
|
|
|
return \React\Promise\resolve($letters ?: $this->letters); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function assertEqualsFromPromised($expected, FulfilledPromise $promise) |
88
|
|
|
{ |
89
|
|
|
$this->assertEquals($expected, self::await($promise)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private static function await(FulfilledPromise $promise) |
93
|
|
|
{ |
94
|
|
|
$resolvedValue = null; |
95
|
|
|
$rejectedReason = null; |
96
|
|
|
$promise->then( |
97
|
|
|
function ($value) use (&$resolvedValue) { |
98
|
|
|
$resolvedValue = $value; |
99
|
|
|
}, |
100
|
|
|
function ($reason) use (&$rejectedReason) { |
101
|
|
|
$rejectedReason = $reason; |
102
|
|
|
} |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if ($rejectedReason instanceof \Exception) { |
106
|
|
|
throw $rejectedReason; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $resolvedValue; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|