diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 01aea69..ff58a62 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,8 +4,9 @@ + convertDeprecationsToExceptions="true"> ./tests/ diff --git a/src/TimeoutException.php b/src/TimeoutException.php index f5afaf5..7f03ba0 100644 --- a/src/TimeoutException.php +++ b/src/TimeoutException.php @@ -11,22 +11,16 @@ class TimeoutException extends RuntimeException /** * @param float $timeout - * @param ?string $message - * @param ?int $code + * @param string|null $message + * @param int|null $code * @param null|\Exception|\Throwable $previous */ - public function __construct($timeout, $message = null, $code = null, $previous = null) + public function __construct($timeout, $message = '', $code = 0, $previous = null) { - // Preserve compatibility with our former signature, but avoid invalid arguments for the parent constructor: - if ($message === null) { - $message = ''; - } - if ($code === null) { - $code = 0; - } - parent::__construct($message, $code, $previous); + // Preserve compatibility with our former nullable signature, but avoid invalid arguments for the parent constructor: + parent::__construct((string) $message, (int) $code, $previous); - $this->timeout = $timeout; + $this->timeout = (float) $timeout; } /** diff --git a/tests/TimeoutExceptionTest.php b/tests/TimeoutExceptionTest.php index eb2957e..4fe1ce4 100644 --- a/tests/TimeoutExceptionTest.php +++ b/tests/TimeoutExceptionTest.php @@ -2,47 +2,45 @@ namespace React\Tests\Promise\Timer; -use ErrorException; use React\Promise\Timer\TimeoutException; class TimeoutExceptionTest extends TestCase { - public function testAccessTimeout() + public function testCtorWithAllParameters() { - $e = new TimeoutException(10); + $previous = new \Exception(); + $e = new TimeoutException(1.0, 'Error', 42, $previous); - $this->assertEquals(10, $e->getTimeout()); + $this->assertEquals(1.0, $e->getTimeout()); + $this->assertEquals('Error', $e->getMessage()); + $this->assertEquals(42, $e->getCode()); + $this->assertSame($previous, $e->getPrevious()); } - public function testEnsureNoDeprecationsAreTriggered() + public function testCtorWithDefaultValues() { - $formerReporting = error_reporting(); - error_reporting(E_ALL | E_STRICT); - $this->setStrictErrorHandling(); - - try { - $e = new TimeoutException(10); - } catch (ErrorException $e) { - error_reporting($formerReporting); - throw $e; - } - - error_reporting($formerReporting); - $this->assertEquals(10, $e->getTimeout()); + $e = new TimeoutException(2.0); + + $this->assertEquals(2.0, $e->getTimeout()); + $this->assertEquals('', $e->getMessage()); + $this->assertEquals(0, $e->getCode()); + $this->assertNull($e->getPrevious()); + } + + public function testCtorWithIntTimeoutWillBeReturnedAsFloat() + { + $e = new TimeoutException(1); + + $this->assertSame(1.0, $e->getTimeout()); } - protected function setStrictErrorHandling() + public function testLegacyCtorWithNullValues() { - set_error_handler(function ($errno, $errstr, $errfile, $errline) { - if (! (error_reporting() & $errno)) { - return false; - } - switch ($errno) { - case E_DEPRECATED: - throw new ErrorException($errstr, 0, $errno, $errfile, $errline); - } - - return false; - }); + $e = new TimeoutException(10, null, null, null); + + $this->assertEquals(10.0, $e->getTimeout()); + $this->assertEquals('', $e->getMessage()); + $this->assertEquals(0, $e->getCode()); + $this->assertNull($e->getPrevious()); } }