Skip to content

Commit 08b7f70

Browse files
committed
Ignore non-function callback arguments
1 parent 8fe0e60 commit 08b7f70

File tree

7 files changed

+95
-3
lines changed

7 files changed

+95
-3
lines changed

src/React/Promise/Deferred.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function then($fulfilledHandler = null, $errorHandler = null, $progressHa
1818

1919
$deferred = new static();
2020

21-
if ($progressHandler) {
21+
if (is_callable($progressHandler)) {
2222
$progHandler = function ($update) use ($deferred, $progressHandler) {
2323
try {
2424
$deferred->progress(call_user_func($progressHandler, $update));

src/React/Promise/FulfilledPromise.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public function then($fulfilledHandler = null, $errorHandler = null, $progressHa
1515
{
1616
try {
1717
$result = $this->result;
18-
if ($fulfilledHandler) {
18+
19+
if (is_callable($fulfilledHandler)) {
1920
$result = call_user_func($fulfilledHandler, $result);
2021
}
2122

src/React/Promise/RejectedPromise.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct($reason = null)
1414
public function then($fulfilledHandler = null, $errorHandler = null, $progressHandler = null)
1515
{
1616
try {
17-
if (!$errorHandler) {
17+
if (!is_callable($errorHandler)) {
1818
return new RejectedPromise($this->reason);
1919
}
2020

tests/React/Promise/DeferredProgressTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,32 @@ public function shouldAllowRejectAfterProgress()
312312
->resolver()
313313
->reject(2);
314314
}
315+
316+
/**
317+
* @test
318+
* @dataProvider typesDataProvider
319+
**/
320+
public function shouldIgnoreNonFunctions($var, $desc)
321+
{
322+
$mock = $this->createCallableMock();
323+
$mock
324+
->expects($this->once())
325+
->method('__invoke')
326+
->with($this->identicalTo(1));
327+
328+
$d = new Deferred();
329+
$d
330+
->then(
331+
null,
332+
null,
333+
$var
334+
)
335+
->then(
336+
$this->expectCallableNever(),
337+
$this->expectCallableNever(),
338+
$mock
339+
);
340+
341+
$d->progress(1);
342+
}
315343
}

tests/React/Promise/DeferredRejectTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,30 @@ public function shouldForwardReasonWhenCallbackIsNull()
8585

8686
$d->reject(1);
8787
}
88+
89+
/**
90+
* @test
91+
* @dataProvider typesDataProvider
92+
**/
93+
public function shouldIgnoreNonFunctions($var, $desc)
94+
{
95+
$mock = $this->createCallableMock();
96+
$mock
97+
->expects($this->once())
98+
->method('__invoke')
99+
->with($this->identicalTo(1));
100+
101+
$d = new Deferred();
102+
$d
103+
->then(
104+
null,
105+
$var
106+
)
107+
->then(
108+
$this->expectCallableNever(),
109+
$mock
110+
);
111+
112+
$d->reject(1);
113+
}
88114
}

tests/React/Promise/DeferredResolveTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,29 @@ public function shouldForwardValueWhenCallbackIsNull()
162162

163163
$d->resolve(1);
164164
}
165+
166+
/**
167+
* @test
168+
* @dataProvider typesDataProvider
169+
**/
170+
public function shouldIgnoreNonFunctions($var, $desc)
171+
{
172+
$mock = $this->createCallableMock();
173+
$mock
174+
->expects($this->once())
175+
->method('__invoke')
176+
->with($this->identicalTo(1));
177+
178+
$d = new Deferred();
179+
$d
180+
->then(
181+
$var
182+
)
183+
->then(
184+
$mock,
185+
$this->expectCallableNever()
186+
);
187+
188+
$d->resolve(1);
189+
}
165190
}

tests/React/Promise/TestCase.php

+12
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ public function createCallableMock()
3838
{
3939
return $this->getMock('React\\Promise\Stub\CallableStub');
4040
}
41+
42+
public function typesDataProvider()
43+
{
44+
return array(
45+
array('', 'empty string'),
46+
array(true, 'true'),
47+
array(false, 'false'),
48+
array(new \stdClass, 'object'),
49+
array(1, 'truthy'),
50+
array(0, 'falsey')
51+
);
52+
}
4153
}

0 commit comments

Comments
 (0)