Skip to content

Commit e8401c3

Browse files
committed
Throw InvalidArgumentException for invalid canceller
1 parent 893777b commit e8401c3

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/React/Promise/Deferred.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ class Deferred implements PromiseInterface, ResolverInterface, PromisorInterface
1616

1717
public function __construct($canceller = null)
1818
{
19+
if ($canceller !== null && !is_callable($canceller)) {
20+
throw new \InvalidArgumentException(
21+
sprintf(
22+
'The canceller argument must be null or of type callable, %s given.',
23+
gettype($canceller)
24+
)
25+
);
26+
}
27+
1928
$this->canceller = $canceller;
2029
}
2130

tests/React/Promise/DeferredTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,13 @@ public function shouldInvokeCancellationHandleWhenCancellingAllDerived()
201201
$p1->cancel();
202202
$p2->cancel();
203203
}
204+
205+
/**
206+
* @test
207+
* @expectedException InvalidArgumentException
208+
*/
209+
public function shouldThrowIfCancellerIsNotACallable()
210+
{
211+
new Deferred(false);
212+
}
204213
}

tests/React/Promise/PromiseTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,13 @@ public function shouldInvokeCancellationHandlerAndStayPendingWhenCallingCancel()
9090

9191
$promise->then($this->expectCallableNever(), $this->expectCallableNever());
9292
}
93+
94+
/**
95+
* @test
96+
* @expectedException InvalidArgumentException
97+
*/
98+
public function shouldThrowIfCancellerIsNotACallable()
99+
{
100+
new Promise(function () { }, false);
101+
}
93102
}

0 commit comments

Comments
 (0)