|
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
|
|
|
public function testRespectsASmallerFirstWhenSlicing() |
|
34
|
|
|
{ |
|
35
|
|
|
$promise = ConnectionBuilder::connectionFromPromisedArraySlice( |
|
36
|
|
|
$this->promisedLetters(['A', 'B', 'C']), |
|
37
|
|
|
['first' => 2], |
|
38
|
|
|
[ |
|
39
|
|
|
'sliceStart' => 0, |
|
40
|
|
|
'arrayLength' => 5, |
|
41
|
|
|
] |
|
42
|
|
|
); |
|
43
|
|
|
$expected = $this->getExpectedConnection(['A', 'B'], false, true); |
|
44
|
|
|
$this->assertEqualsFromPromised($expected, $promise); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function promisedLetters(array $letters = null) |
|
48
|
|
|
{ |
|
49
|
|
|
return \React\Promise\resolve($letters ?: $this->letters); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function assertEqualsFromPromised($expected, FulfilledPromise $promise) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->assertEquals($expected, self::await($promise)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private static function await(FulfilledPromise $promise) |
|
58
|
|
|
{ |
|
59
|
|
|
$resolvedValue = null; |
|
60
|
|
|
$rejectedReason = null; |
|
61
|
|
|
$promise->then( |
|
62
|
|
|
function ($value) use (&$resolvedValue) { |
|
63
|
|
|
$resolvedValue = $value; |
|
64
|
|
|
}, |
|
65
|
|
|
function ($reason) use (&$rejectedReason) { |
|
66
|
|
|
$rejectedReason = $reason; |
|
67
|
|
|
} |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
if ($rejectedReason instanceof \Exception) { |
|
71
|
|
|
throw $rejectedReason; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $resolvedValue; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|