Skip to content

Commit 9403107

Browse files
committed
Refactor php error collection to it own class
1 parent da7c9c0 commit 9403107

5 files changed

Lines changed: 50 additions & 39 deletions

File tree

tests/React/Promise/DeferredProgressTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ public function shouldAllowRejectAfterProgress()
319319
**/
320320
public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
321321
{
322-
$this->setErrorHandler();
322+
$errorCollector = new ErrorCollector();
323+
$errorCollector->register();
323324

324325
$mock = $this->createCallableMock();
325326
$mock
@@ -342,7 +343,7 @@ public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
342343

343344
$d->progress(1);
344345

345-
$this->assertError('Invalid $progressHandler argument passed to then(), must be null or callable.', E_USER_NOTICE);
346-
$this->restoreErrorHandler();
346+
$errorCollector->assertCollectedError('Invalid $progressHandler argument passed to then(), must be null or callable.', E_USER_NOTICE);
347+
$errorCollector->unregister();
347348
}
348349
}

tests/React/Promise/DeferredRejectTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public function shouldForwardReasonWhenCallbackIsNull()
9292
**/
9393
public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
9494
{
95-
$this->setErrorHandler();
95+
$errorCollector = new ErrorCollector();
96+
$errorCollector->register();
9697

9798
$mock = $this->createCallableMock();
9899
$mock
@@ -113,7 +114,7 @@ public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
113114

114115
$d->reject(1);
115116

116-
$this->assertError('Invalid $errorHandler argument passed to then(), must be null or callable.', E_USER_NOTICE);
117-
$this->restoreErrorHandler();
117+
$errorCollector->assertCollectedError('Invalid $errorHandler argument passed to then(), must be null or callable.', E_USER_NOTICE);
118+
$errorCollector->unregister();
118119
}
119120
}

tests/React/Promise/DeferredResolveTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public function shouldForwardValueWhenCallbackIsNull()
169169
**/
170170
public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
171171
{
172-
$this->setErrorHandler();
172+
$errorCollector = new ErrorCollector();
173+
$errorCollector->register();
173174

174175
$mock = $this->createCallableMock();
175176
$mock
@@ -189,7 +190,7 @@ public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
189190

190191
$d->resolve(1);
191192

192-
$this->assertError('Invalid $fulfilledHandler argument passed to then(), must be null or callable.', E_USER_NOTICE);
193-
$this->restoreErrorHandler();
193+
$errorCollector->assertCollectedError('Invalid $fulfilledHandler argument passed to then(), must be null or callable.', E_USER_NOTICE);
194+
$errorCollector->unregister();
194195
}
195196
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
class ErrorCollector
6+
{
7+
private $errors = array();
8+
9+
public function register()
10+
{
11+
$errors = array();
12+
13+
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$errors) {
14+
$errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
15+
});
16+
17+
$this->errors = &$errors;
18+
}
19+
20+
public function unregister()
21+
{
22+
$this->errors = array();
23+
restore_error_handler();
24+
}
25+
26+
public function assertCollectedError($errstr, $errno)
27+
{
28+
foreach ($this->errors as $error) {
29+
if ($error['errstr'] === $errstr && $error['errno'] === $errno) {
30+
return;
31+
}
32+
}
33+
34+
$message = 'Error with level ' . $errno . ' and message "' . $errstr . '" not found in ' . var_export($this->errors, true);
35+
36+
throw new \PHPUnit_Framework_AssertionFailedError($message);
37+
}
38+
}

tests/React/Promise/TestCase.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class TestCase extends \PHPUnit_Framework_TestCase
66
{
7-
private $errors = array();
8-
97
public function expectCallableExactly($amount)
108
{
119
$mock = $this->createCallableMock();
@@ -52,32 +50,4 @@ public function invalidCallbackDataProvider()
5250
'falsey' => array(0)
5351
);
5452
}
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-
}
8353
}

0 commit comments

Comments
 (0)