Skip to content

Commit 05ceaaa

Browse files
committed
Add RetryConnection test cases
1 parent 493e6c1 commit 05ceaaa

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/RetryConnectionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace ipl\Tests\Sql;
4+
5+
use Exception;
6+
use ipl\Sql\RetryConnection;
7+
8+
class RetryConnectionTest extends \PHPUnit\Framework\TestCase
9+
{
10+
public function testIsRetryable()
11+
{
12+
$db = $this->getConnection();
13+
14+
$this->assertTrue($db::isRetryable(new Exception('SQLState: Connection refused by the server')));
15+
$this->assertTrue($db::isRetryable(new Exception('SQLState: Error writing data to the connection')));
16+
$this->assertTrue($db::isRetryable(new Exception('SQLState: No such file or directory found')));
17+
18+
$this->assertFalse($db::isRetryable(new Exception('SQLState: Cannot start transaction')));
19+
$this->assertFalse($db::isRetryable(new Exception('Cannot establish the connection to SQL server')));
20+
$this->assertFalse($db::isRetryable(new Exception('Fatal error encountered during command execution')));
21+
}
22+
23+
public function testExecutionRetriesGivesUpAfterMaxRetries()
24+
{
25+
$this->expectException(Exception::class);
26+
$this->expectExceptionMessage('SQLSTATE[HY000] [2002] No such file or directory');
27+
28+
$this->getConnection(2)->transaction(function () {
29+
});
30+
}
31+
32+
protected function getConnection(int $retries = 1): RetryConnection
33+
{
34+
return new RetryConnection([
35+
'db' => 'mysql',
36+
'dbname' => 'foo',
37+
], $retries);
38+
}
39+
}

0 commit comments

Comments
 (0)