Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TimeoutException legacy API by using explicit type casts #52

Merged
merged 1 commit into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResult="false"
colors="true"
cacheResult="false">
convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="Promise Timer Test Suite">
<directory>./tests/</directory>
Expand Down
18 changes: 6 additions & 12 deletions src/TimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
58 changes: 28 additions & 30 deletions tests/TimeoutExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}