Skip to content

Commit a0f23cb

Browse files
committed
Trigger a E_USER_NOTICE for invalid arguments passed to then()
1 parent 08b7f70 commit a0f23cb

8 files changed

+59
-4
lines changed

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
backupStaticAttributes="false"
55
colors="true"
66
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
7+
convertNoticesToExceptions="false"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false"

src/React/Promise/Deferred.php

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function then($fulfilledHandler = null, $errorHandler = null, $progressHa
2727
}
2828
};
2929
} else {
30+
if (null !== $progressHandler) {
31+
trigger_error('Invalid $progressHandler argument passed to then(), must be null or callable.', \E_USER_NOTICE);
32+
}
33+
3034
$progHandler = array($deferred, 'progress');
3135
}
3236

src/React/Promise/FulfilledPromise.php

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

1919
if (is_callable($fulfilledHandler)) {
2020
$result = call_user_func($fulfilledHandler, $result);
21+
} elseif (null !== $fulfilledHandler) {
22+
trigger_error('Invalid $fulfilledHandler argument passed to then(), must be null or callable.', \E_USER_NOTICE);
2123
}
2224

2325
return Util::promiseFor($result);

src/React/Promise/RejectedPromise.php

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public function then($fulfilledHandler = null, $errorHandler = null, $progressHa
1515
{
1616
try {
1717
if (!is_callable($errorHandler)) {
18+
if (null !== $errorHandler) {
19+
trigger_error('Invalid $errorHandler argument passed to then(), must be null or callable.', \E_USER_NOTICE);
20+
}
21+
1822
return new RejectedPromise($this->reason);
1923
}
2024

tests/React/Promise/DeferredProgressTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ public function shouldAllowRejectAfterProgress()
317317
* @test
318318
* @dataProvider typesDataProvider
319319
**/
320-
public function shouldIgnoreNonFunctions($var, $desc)
320+
public function shouldIgnoreNonFunctionsAndTriggerPHPNotice($var, $desc)
321321
{
322+
$this->setErrorHandler();
323+
322324
$mock = $this->createCallableMock();
323325
$mock
324326
->expects($this->once())
@@ -339,5 +341,8 @@ public function shouldIgnoreNonFunctions($var, $desc)
339341
);
340342

341343
$d->progress(1);
344+
345+
$this->assertError('Invalid $progressHandler argument passed to then(), must be null or callable.', \E_USER_NOTICE);
346+
$this->restoreErrorHandler();
342347
}
343348
}

tests/React/Promise/DeferredRejectTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ public function shouldForwardReasonWhenCallbackIsNull()
9090
* @test
9191
* @dataProvider typesDataProvider
9292
**/
93-
public function shouldIgnoreNonFunctions($var, $desc)
93+
public function shouldIgnoreNonFunctionsAndTriggerPHPNotice($var, $desc)
9494
{
95+
$this->setErrorHandler();
96+
9597
$mock = $this->createCallableMock();
9698
$mock
9799
->expects($this->once())
@@ -110,5 +112,8 @@ public function shouldIgnoreNonFunctions($var, $desc)
110112
);
111113

112114
$d->reject(1);
115+
116+
$this->assertError('Invalid $errorHandler argument passed to then(), must be null or callable.', \E_USER_NOTICE);
117+
$this->restoreErrorHandler();
113118
}
114119
}

tests/React/Promise/DeferredResolveTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ public function shouldForwardValueWhenCallbackIsNull()
167167
* @test
168168
* @dataProvider typesDataProvider
169169
**/
170-
public function shouldIgnoreNonFunctions($var, $desc)
170+
public function shouldIgnoreNonFunctionsAndTriggerPHPNotice($var, $desc)
171171
{
172+
$this->setErrorHandler();
173+
172174
$mock = $this->createCallableMock();
173175
$mock
174176
->expects($this->once())
@@ -186,5 +188,8 @@ public function shouldIgnoreNonFunctions($var, $desc)
186188
);
187189

188190
$d->resolve(1);
191+
192+
$this->assertError('Invalid $fulfilledHandler argument passed to then(), must be null or callable.', \E_USER_NOTICE);
193+
$this->restoreErrorHandler();
189194
}
190195
}

tests/React/Promise/TestCase.php

+30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class TestCase extends \PHPUnit_Framework_TestCase
66
{
7+
private $errors = array();
8+
79
public function expectCallableExactly($amount)
810
{
911
$mock = $this->createCallableMock();
@@ -50,4 +52,32 @@ public function typesDataProvider()
5052
array(0, 'falsey')
5153
);
5254
}
55+
56+
public function setErrorHandler()
57+
{
58+
$errors = array();
59+
60+
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$errors) {
61+
$errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
62+
});
63+
64+
$this->errors = &$errors;
65+
}
66+
67+
public function restoreErrorHandler()
68+
{
69+
$this->errors = array();
70+
restore_error_handler();
71+
}
72+
73+
public function assertError($errstr, $errno)
74+
{
75+
foreach ($this->errors as $error) {
76+
if ($error['errstr'] === $errstr && $error['errno'] === $errno) {
77+
return;
78+
}
79+
}
80+
81+
$this->fail('Error with level ' . $errno . ' and message "' . $errstr . '" not found in ', var_export($this->errors, true));
82+
}
5383
}

0 commit comments

Comments
 (0)