Skip to content

Commit 060b245

Browse files
committed
Merge branch 'master' into enforce-exception-reasons
2 parents 1588735 + f5b9d6a commit 060b245

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/Promise.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(callable $resolver, callable $canceller = null)
2020
public function then(callable $onFulfilled = null, callable $onRejected = null)
2121
{
2222
if (null !== $this->result) {
23-
return $this->result()->then($onFulfilled, $onRejected);
23+
return $this->result->then($onFulfilled, $onRejected);
2424
}
2525

2626
if (null === $this->canceller) {
@@ -41,7 +41,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
4141
public function done(callable $onFulfilled = null, callable $onRejected = null)
4242
{
4343
if (null !== $this->result) {
44-
return $this->result()->done($onFulfilled, $onRejected);
44+
return $this->result->done($onFulfilled, $onRejected);
4545
}
4646

4747
$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected) {
@@ -117,15 +117,7 @@ private function reject($reason)
117117

118118
private function settle(PromiseInterface $result)
119119
{
120-
if ($result instanceof LazyPromise) {
121-
$result = $result->promise();
122-
}
123-
124-
if ($result === $this) {
125-
$result = new RejectedPromise(
126-
new \LogicException('Cannot resolve a promise with itself.')
127-
);
128-
}
120+
$result = $this->unwrap($result);
129121

130122
$handlers = $this->handlers;
131123

@@ -138,13 +130,30 @@ private function settle(PromiseInterface $result)
138130
}
139131
}
140132

141-
private function result()
133+
private function unwrap($promise)
142134
{
143-
while ($this->result instanceof self && null !== $this->result->result) {
144-
$this->result = $this->result->result;
135+
$promise = $this->extract($promise);
136+
137+
while ($promise instanceof self && null !== $promise->result) {
138+
$promise = $this->extract($promise->result);
139+
}
140+
141+
return $promise;
142+
}
143+
144+
private function extract($promise)
145+
{
146+
if ($promise instanceof LazyPromise) {
147+
$promise = $promise->promise();
148+
}
149+
150+
if ($promise === $this) {
151+
return new RejectedPromise(
152+
new \LogicException('Cannot resolve a promise with itself.')
153+
);
145154
}
146155

147-
return $this->result;
156+
return $promise;
148157
}
149158

150159
private function call(callable $callback)

tests/PromiseTest/ResolveTestTrait.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ public function resolveShouldRejectWhenResolvedWithItself()
136136
$adapter->resolve($adapter->promise());
137137
}
138138

139+
/**
140+
* @test
141+
*/
142+
public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself()
143+
{
144+
$adapter1 = $this->getPromiseTestAdapter();
145+
$adapter2 = $this->getPromiseTestAdapter();
146+
147+
$mock = $this->createCallableMock();
148+
$mock
149+
->expects($this->once())
150+
->method('__invoke')
151+
->with(new \LogicException('Cannot resolve a promise with itself.'));
152+
153+
$promise1 = $adapter1->promise();
154+
155+
$promise2 = $adapter2->promise();
156+
157+
$promise2->then(
158+
$this->expectCallableNever(),
159+
$mock
160+
);
161+
162+
$adapter1->resolve($promise2);
163+
$adapter2->resolve($promise1);
164+
}
165+
139166
/** @test */
140167
public function doneShouldInvokeFulfillmentHandler()
141168
{

0 commit comments

Comments
 (0)