Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
106 / 106 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('integration'); |
| 11 | |
| 12 | use Phexium\Plugin\PdoFactory\Adapter\PdoFactory; |
| 13 | |
| 14 | describe('SqlitePdoFactory', function (): void { |
| 15 | afterEach(function (): void { |
| 16 | $root = dirname(__DIR__, 5); |
| 17 | |
| 18 | if (file_exists($root.'/var/database/sqlite.db')) { |
| 19 | unlink($root.'/var/database/sqlite.db'); |
| 20 | } |
| 21 | |
| 22 | if (file_exists($root.'/var/database/test.db')) { |
| 23 | unlink($root.'/var/database/test.db'); |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | it('creates a SQLite PDO when database.type is Sqlite', function (): void { |
| 28 | $root = dirname(__DIR__, 5); |
| 29 | $factory = new PdoFactory(['database.type' => 'Sqlite']); |
| 30 | |
| 31 | $pdo = $factory->create(); |
| 32 | |
| 33 | $dbPath = $pdo->query('PRAGMA database_list')->fetch(PDO::FETCH_ASSOC)['file']; |
| 34 | expect($pdo)->toBeInstanceOf(PDO::class); |
| 35 | expect($pdo->getAttribute(PDO::ATTR_DRIVER_NAME))->toBe('sqlite'); |
| 36 | expect(file_exists($root.'/var/database/sqlite.db'))->toBeTrue(); |
| 37 | expect($dbPath)->toStartWith('/'); |
| 38 | }); |
| 39 | |
| 40 | it('creates a SQLite PDO by default when no database.type is set', function (): void { |
| 41 | $factory = new PdoFactory([]); |
| 42 | |
| 43 | $pdo = $factory->create(); |
| 44 | |
| 45 | expect($pdo)->toBeInstanceOf(PDO::class); |
| 46 | expect($pdo->getAttribute(PDO::ATTR_DRIVER_NAME))->toBe('sqlite'); |
| 47 | }); |
| 48 | |
| 49 | it('creates a SQLite PDO for unknown database.type', function (): void { |
| 50 | $factory = new PdoFactory(['database.type' => 'UnknownDriver']); |
| 51 | |
| 52 | $pdo = $factory->create(); |
| 53 | |
| 54 | expect($pdo)->toBeInstanceOf(PDO::class); |
| 55 | expect($pdo->getAttribute(PDO::ATTR_DRIVER_NAME))->toBe('sqlite'); |
| 56 | }); |
| 57 | |
| 58 | it('creates a SQLite PDO with custom fullpath', function (): void { |
| 59 | $root = dirname(__DIR__, 5); |
| 60 | $cwd = getcwd(); |
| 61 | chdir('/tmp'); |
| 62 | |
| 63 | try { |
| 64 | $factory = new PdoFactory([ |
| 65 | 'database.type' => 'Sqlite', |
| 66 | 'database.sqlite.fullpath' => 'var/database/test.db', |
| 67 | ]); |
| 68 | |
| 69 | $pdo = $factory->create(); |
| 70 | |
| 71 | $dbPath = $pdo->query('PRAGMA database_list')->fetch(PDO::FETCH_ASSOC)['file']; |
| 72 | expect($dbPath)->toBe($root.'/var/database/test.db'); |
| 73 | } finally { |
| 74 | chdir($cwd); |
| 75 | } |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | describe('MysqlPdoFactory', function (): void { |
| 80 | it('creates a MySQL PDO connection', function (): void { |
| 81 | $factory = new PdoFactory([ |
| 82 | 'database.type' => 'Mysql', |
| 83 | 'database.mysql.host' => 'mysql-in-memory', |
| 84 | 'database.mysql.dbname' => 'mysql', |
| 85 | 'database.mysql.user' => 'root', |
| 86 | 'database.mysql.password' => 'root', |
| 87 | ]); |
| 88 | |
| 89 | $pdo = $factory->create(); |
| 90 | |
| 91 | expect($pdo)->toBeInstanceOf(PDO::class); |
| 92 | expect($pdo->getAttribute(PDO::ATTR_DRIVER_NAME))->toBe('mysql'); |
| 93 | expect($pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE))->toBe(PDO::FETCH_ASSOC); |
| 94 | }); |
| 95 | |
| 96 | it('uses the configured port', function (): void { |
| 97 | expect(fn (): PDO => new PdoFactory([ |
| 98 | 'database.type' => 'Mysql', |
| 99 | 'database.mysql.host' => 'mysql-in-memory', |
| 100 | 'database.mysql.port' => '9999', |
| 101 | 'database.mysql.dbname' => 'mysql', |
| 102 | 'database.mysql.user' => 'root', |
| 103 | 'database.mysql.password' => 'root', |
| 104 | ])->create())->toThrow(PDOException::class); |
| 105 | }); |
| 106 | |
| 107 | it('uses the configured charset', function (): void { |
| 108 | $factory = new PdoFactory([ |
| 109 | 'database.type' => 'Mysql', |
| 110 | 'database.mysql.host' => 'mysql-in-memory', |
| 111 | 'database.mysql.charset' => 'latin1', |
| 112 | 'database.mysql.dbname' => 'mysql', |
| 113 | 'database.mysql.user' => 'root', |
| 114 | 'database.mysql.password' => 'root', |
| 115 | ]); |
| 116 | |
| 117 | $pdo = $factory->create(); |
| 118 | |
| 119 | $charset = $pdo->query("SHOW VARIABLES LIKE 'character_set_client'")->fetch()['Value']; |
| 120 | expect($charset)->toBe('latin1'); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | describe('PostgresqlPdoFactory', function (): void { |
| 125 | it('creates a PostgreSQL PDO connection', function (): void { |
| 126 | $factory = new PdoFactory([ |
| 127 | 'database.type' => 'Postgresql', |
| 128 | 'database.postgresql.host' => 'postgres-in-memory', |
| 129 | 'database.postgresql.dbname' => 'postgres', |
| 130 | 'database.postgresql.user' => 'postgres', |
| 131 | 'database.postgresql.password' => 'postgres', |
| 132 | ]); |
| 133 | |
| 134 | $pdo = $factory->create(); |
| 135 | |
| 136 | expect($pdo)->toBeInstanceOf(PDO::class); |
| 137 | expect($pdo->getAttribute(PDO::ATTR_DRIVER_NAME))->toBe('pgsql'); |
| 138 | expect($pdo->getAttribute(PDO::ATTR_DEFAULT_FETCH_MODE))->toBe(PDO::FETCH_ASSOC); |
| 139 | }); |
| 140 | |
| 141 | it('uses the configured port', function (): void { |
| 142 | expect(fn (): PDO => new PdoFactory([ |
| 143 | 'database.type' => 'Postgresql', |
| 144 | 'database.postgresql.host' => 'postgres-in-memory', |
| 145 | 'database.postgresql.port' => '9999', |
| 146 | 'database.postgresql.dbname' => 'postgres', |
| 147 | 'database.postgresql.user' => 'postgres', |
| 148 | 'database.postgresql.password' => 'postgres', |
| 149 | ])->create())->toThrow(PDOException::class); |
| 150 | }); |
| 151 | }); |