1<?php
2
3namespace React\Promise;
4
5class FunctionAllTest extends TestCase
6{
7    /** @test */
8    public function shouldResolveEmptyInput()
9    {
10        $mock = $this->createCallableMock();
11        $mock
12            ->expects($this->once())
13            ->method('__invoke')
14            ->with($this->identicalTo([]));
15
16        all([])
17            ->then($mock);
18    }
19
20    /** @test */
21    public function shouldResolveValuesArray()
22    {
23        $mock = $this->createCallableMock();
24        $mock
25            ->expects($this->once())
26            ->method('__invoke')
27            ->with($this->identicalTo([1, 2, 3]));
28
29        all([1, 2, 3])
30            ->then($mock);
31    }
32
33    /** @test */
34    public function shouldResolvePromisesArray()
35    {
36        $mock = $this->createCallableMock();
37        $mock
38            ->expects($this->once())
39            ->method('__invoke')
40            ->with($this->identicalTo([1, 2, 3]));
41
42        all([resolve(1), resolve(2), resolve(3)])
43            ->then($mock);
44    }
45
46    /** @test */
47    public function shouldResolveSparseArrayInput()
48    {
49        $mock = $this->createCallableMock();
50        $mock
51            ->expects($this->once())
52            ->method('__invoke')
53            ->with($this->identicalTo([null, 1, null, 1, 1]));
54
55        all([null, 1, null, 1, 1])
56            ->then($mock);
57    }
58
59    /** @test */
60    public function shouldRejectIfAnyInputPromiseRejects()
61    {
62        $mock = $this->createCallableMock();
63        $mock
64            ->expects($this->once())
65            ->method('__invoke')
66            ->with($this->identicalTo(2));
67
68        all([resolve(1), reject(2), resolve(3)])
69            ->then($this->expectCallableNever(), $mock);
70    }
71
72    /** @test */
73    public function shouldAcceptAPromiseForAnArray()
74    {
75        $mock = $this->createCallableMock();
76        $mock
77            ->expects($this->once())
78            ->method('__invoke')
79            ->with($this->identicalTo([1, 2, 3]));
80
81        all(resolve([1, 2, 3]))
82            ->then($mock);
83    }
84
85    /** @test */
86    public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
87    {
88        $mock = $this->createCallableMock();
89        $mock
90            ->expects($this->once())
91            ->method('__invoke')
92            ->with($this->identicalTo([]));
93
94        all(resolve(1))
95            ->then($mock);
96    }
97
98    /** @test */
99    public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises()
100    {
101        $mock = $this->createCallableMock();
102        $mock
103            ->expects($this->once())
104            ->method('__invoke')
105            ->with($this->identicalTo([1, 2, 3]));
106
107        $deferred = new Deferred();
108
109        all([resolve(1), $deferred->promise(), resolve(3)])
110            ->then($mock);
111
112        $deferred->resolve(2);
113    }
114}
115