1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
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 Cubiche\Core\Async\Tests\Units\Promise; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Delegate\Delegate; |
15
|
|
|
use Cubiche\Core\Async\Promise\Promise; |
16
|
|
|
use Cubiche\Core\Async\Promise\State; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Promise Tests class. |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class PromiseTests extends PromiseInterfaceTestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Delegate |
28
|
|
|
*/ |
29
|
|
|
protected $resolve; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Delegate |
33
|
|
|
*/ |
34
|
|
|
protected $reject; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Delegate |
38
|
|
|
*/ |
39
|
|
|
protected $notify; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function defaultConstructorArguments() |
45
|
|
|
{ |
46
|
|
|
return array( |
47
|
|
|
function (callable $callable) { |
48
|
|
|
$this->resolve = new Delegate($callable); |
49
|
|
|
}, |
50
|
|
|
function (callable $callable) { |
51
|
|
|
$this->reject = new Delegate($callable); |
52
|
|
|
}, |
53
|
|
|
function (callable $callable) { |
54
|
|
|
$this->notify = new Delegate($callable); |
55
|
|
|
}, |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
protected function resolve($value = null) |
63
|
|
|
{ |
64
|
|
|
$this->resolve->__invoke($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function reject($reason = null) |
71
|
|
|
{ |
72
|
|
|
$this->reject->__invoke($reason); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
protected function notify($state = null) |
79
|
|
|
{ |
80
|
|
|
$this->notify->__invoke($state); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Test __construct. |
85
|
|
|
*/ |
86
|
|
|
public function testConstruct() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this |
89
|
|
|
->given( |
90
|
|
|
$resolve = $this->delegateMock(), |
91
|
|
|
$reject = $this->delegateMock(), |
92
|
|
|
$notify = $this->delegateMock() |
93
|
|
|
) |
94
|
|
|
->when($this->newTestedInstance($resolve, $reject, $notify)) |
95
|
|
|
->then() |
96
|
|
|
->delegateCall($resolve) |
97
|
|
|
->once() |
98
|
|
|
->delegateCall($reject) |
99
|
|
|
->once() |
100
|
|
|
->delegateCall($notify) |
101
|
|
|
->once() |
102
|
|
|
; |
103
|
|
|
|
104
|
|
|
$this |
105
|
|
|
/* @var \Cubiche\Core\Async\Promise\PromiseInterface $promise */ |
106
|
|
|
->given($promise = $this->newDefaultTestedInstance()) |
107
|
|
|
->then() |
108
|
|
|
->boolean($promise->state()->equals(State::PENDING())) |
109
|
|
|
->isTrue() |
110
|
|
|
; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Test notify. |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function testNotify() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this |
119
|
|
|
->given( |
120
|
|
|
$promise = $this->newDefaultTestedInstance(), |
121
|
|
|
$onNotify = $this->delegateMock() |
122
|
|
|
) |
123
|
|
|
->when(function () use ($promise, $onNotify) { |
124
|
|
|
$promise->then(null, null, $onNotify); |
125
|
|
|
$this->notify('foo'); |
126
|
|
|
}) |
127
|
|
|
->then() |
128
|
|
|
->delegateCall($onNotify) |
129
|
|
|
->withArguments('foo') |
130
|
|
|
->once() |
131
|
|
|
; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
protected function promiseDataProvider() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$pending = $this->newDefaultTestedInstance(); |
140
|
|
|
|
141
|
|
|
$fulfilled = $this->newDefaultTestedInstance(); |
142
|
|
|
$this->resolve($this->defaultResolveValue()); |
143
|
|
|
|
144
|
|
|
$rejected = $this->newDefaultTestedInstance(); |
145
|
|
|
$this->reject($this->defaultRejectReason()); |
146
|
|
|
|
147
|
|
|
return array( |
148
|
|
|
'pending' => array($pending), |
149
|
|
|
'fulfilled' => array($fulfilled), |
150
|
|
|
'rejected' => array($rejected), |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.