-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): add explicit ConnectionClosed exception
- Loading branch information
1 parent
423487a
commit 4fca50d
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database\Exceptions; | ||
|
||
final class ConnectionClosed extends DatabaseException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('Connection is closed'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database\Tests; | ||
|
||
use Generator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Database\Connections\SQLiteConnection; | ||
use Tempest\Database\Exceptions\ConnectionClosed; | ||
use Tempest\Database\PDOConnection; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class PDOConnectionTest extends TestCase | ||
{ | ||
private const string PATH = ':memory:'; | ||
|
||
#[DataProvider('provideQueryMethods')] | ||
public function test_connection_must_be_open(string $method, array $params): void | ||
{ | ||
$this->expectException(ConnectionClosed::class); | ||
|
||
$connection = new PDOConnection(new SQLiteConnection(self::PATH)); | ||
|
||
$connection->$method(...$params); | ||
} | ||
|
||
#[DataProvider('provideQueryMethods')] | ||
public function test_close_must_be_open(string $method, array $params): void | ||
{ | ||
$this->expectException(ConnectionClosed::class); | ||
|
||
$connection = new PDOConnection(new SQLiteConnection(self::PATH)); | ||
$connection->connect(); | ||
$connection->close(); | ||
|
||
$connection->$method(...$params); | ||
} | ||
|
||
public static function provideQueryMethods(): Generator | ||
{ | ||
yield 'lastInsertId' => ['lastInsertId', []]; | ||
yield 'commit' => ['commit', []]; | ||
yield 'rollback' => ['rollback', []]; | ||
yield 'beginTransaction' => ['beginTransaction', []]; | ||
yield 'prepare' => ['prepare', ['select 1']]; | ||
} | ||
|
||
public function test_commit(): void | ||
{ | ||
$connection = new PDOConnection(new SQLiteConnection(self::PATH)); | ||
$connection->connect(); | ||
|
||
$this->assertTrue($connection->beginTransaction()); | ||
$this->assertTrue($connection->commit()); | ||
} | ||
|
||
public function test_rollback(): void | ||
{ | ||
$connection = new PDOConnection(new SQLiteConnection(self::PATH)); | ||
$connection->connect(); | ||
|
||
$this->assertTrue($connection->beginTransaction()); | ||
$this->assertTrue($connection->rollback()); | ||
} | ||
|
||
public function test_last_insert_id(): void | ||
{ | ||
$connection = new PDOConnection(new SQLiteConnection(self::PATH)); | ||
$connection->connect(); | ||
|
||
$this->assertSame('0', $connection->lastInsertId()); | ||
} | ||
|
||
public function test_prepare(): void | ||
{ | ||
$connection = new PDOConnection(new SQLiteConnection(self::PATH)); | ||
$connection->connect(); | ||
|
||
$this->assertNotFalse($connection->prepare('select 1')); | ||
} | ||
} |