packages/ztd-query-pdo-adapter/tests/Unit/Session/DriverSessionFactoryTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Session;
6
7use PDO;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\TestCase;
10use RuntimeException;
11use ZtdQuery\Adapter\Pdo\Session\DriverSessionFactory;
12
13#[CoversClass(DriverSessionFactory::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\ZtdPdoException::class)]
15#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\ZtdPdoStatement::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\ZtdPdo::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Driver\PdoConnection::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Driver\PdoStatement::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\StatementExecution::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\Bindings::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\BufferedRow::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\ParameterKind::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\ParameterBinder::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\PreparedQuery::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Adapter\Pdo\Session\ConnectionExecution::class)]
26final class DriverSessionFactoryTest extends TestCase
27{
28 public function testDriverNamesAnswersEveryDriverZtdHasAPlatformFor(): void
29 {
30 self::assertSame(['mysql', 'pgsql', 'sqlite'], (new DriverSessionFactory())->driverNames());
31 }
32
33 public function testForDriverAnswersThePlatformThatDriverSpeaks(): void
34 {
35 self::assertInstanceOf(
36 'ZtdQuery\\Platform\\Sqlite\\SqliteSessionFactory',
37 (new DriverSessionFactory())->forDriver('sqlite'),
38 );
39 }
40
41 public function testForDriverRefusesADriverZtdHasNoPlatformFor(): void
42 {
43 $this->expectException(RuntimeException::class);
44 $this->expectExceptionMessage('Unsupported PDO driver: "oci"');
45
46 (new DriverSessionFactory())->forDriver('oci');
47 }
48
49 public function testForDriverNamesEverySupportedDriverWhenItRefusesOne(): void
50 {
51 $this->expectExceptionMessage('Supported drivers: mysql, pgsql, sqlite.');
52
53 (new DriverSessionFactory())->forDriver('firebird');
54 }
55
56 public function testForConnectionReadsTheDriverOffTheConnection(): void
57 {
58 $factory = (new DriverSessionFactory())->forConnection(new PDO('sqlite::memory:'));
59
60 self::assertInstanceOf('ZtdQuery\\Platform\\Sqlite\\SqliteSessionFactory', $factory);
61 }
62
63
64}
65