Skip to content

Commit

Permalink
Add RetryConnection test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 13, 2023
1 parent 493e6c1 commit 05ceaaa
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/RetryConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace ipl\Tests\Sql;

use Exception;
use ipl\Sql\RetryConnection;

class RetryConnectionTest extends \PHPUnit\Framework\TestCase
{
public function testIsRetryable()
{
$db = $this->getConnection();

$this->assertTrue($db::isRetryable(new Exception('SQLState: Connection refused by the server')));
$this->assertTrue($db::isRetryable(new Exception('SQLState: Error writing data to the connection')));
$this->assertTrue($db::isRetryable(new Exception('SQLState: No such file or directory found')));

$this->assertFalse($db::isRetryable(new Exception('SQLState: Cannot start transaction')));
$this->assertFalse($db::isRetryable(new Exception('Cannot establish the connection to SQL server')));
$this->assertFalse($db::isRetryable(new Exception('Fatal error encountered during command execution')));
}

public function testExecutionRetriesGivesUpAfterMaxRetries()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('SQLSTATE[HY000] [2002] No such file or directory');

$this->getConnection(2)->transaction(function () {
});
}

protected function getConnection(int $retries = 1): RetryConnection
{
return new RetryConnection([
'db' => 'mysql',
'dbname' => 'foo',
], $retries);
}
}

0 comments on commit 05ceaaa

Please sign in to comment.