88
99namespace Ibexa \Tests \DoctrineSchema \Database \DbPlatform ;
1010
11+ use Doctrine \DBAL \Configuration ;
12+ use Doctrine \DBAL \DriverManager ;
1113use Doctrine \DBAL \Exception ;
1214use Doctrine \DBAL \ParameterType ;
1315use Ibexa \DoctrineSchema \Database \DbPlatform \SqliteDbPlatform ;
1416use Ibexa \Tests \DoctrineSchema \Database \TestDatabaseFactory ;
1517use PHPUnit \Framework \TestCase ;
1618
17- class SqliteDbPlatformTest extends TestCase
19+ /**
20+ * @covers \Ibexa\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform
21+ * @covers \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseFactory
22+ */
23+ final class SqliteDbPlatformTest extends TestCase
1824{
1925 private TestDatabaseFactory $ testDatabaseFactory ;
2026
@@ -30,7 +36,7 @@ public function setUp(): void
3036 * @throws \Doctrine\DBAL\Exception
3137 * @throws \Ibexa\Tests\DoctrineSchema\Database\TestDatabaseConfigurationException
3238 */
33- public function testForeignKeys (): void
39+ public function testDatabaseFactoryEnablesForeignKeys (): void
3440 {
3541 $ connection = $ this ->testDatabaseFactory ->prepareAndConnect ($ this ->sqliteDbPlatform );
3642 $ schema = $ connection ->createSchemaManager ()->introspectSchema ();
@@ -52,9 +58,32 @@ public function testForeignKeys(): void
5258 $ connection ->insert ($ primaryTable ->getName (), ['id ' => 1 ], [ParameterType::INTEGER ]);
5359 $ connection ->insert ($ secondaryTable ->getName (), ['id ' => 1 ], [ParameterType::INTEGER ]);
5460
55- // insert broken record
61+ // insert a broken record
5662 $ this ->expectException (Exception::class);
5763 $ this ->expectExceptionMessage ('FOREIGN KEY constraint failed ' );
5864 $ connection ->insert ($ secondaryTable ->getName (), ['id ' => 2 ], [ParameterType::INTEGER ]);
5965 }
66+
67+ /**
68+ * For external usage (e.g.: by ibexa/core, a configure method needs to be called to enable foreign keys).
69+ *
70+ * @throws \Doctrine\DBAL\Exception
71+ */
72+ public function testConfigureEnablesForeignKeys (): void
73+ {
74+ $ configuration = new Configuration ();
75+ $ this ->sqliteDbPlatform ->configure ($ configuration );
76+
77+ $ connection = DriverManager::getConnection (
78+ [
79+ 'url ' => 'sqlite:///:memory: ' ,
80+ 'platform ' => $ this ->sqliteDbPlatform ,
81+ ],
82+ $ configuration
83+ );
84+ self ::assertTrue (
85+ (bool )$ connection ->executeQuery ('PRAGMA foreign_keys ' )->fetchOne (),
86+ 'Foreign keys are not enabled '
87+ );
88+ }
6089}
0 commit comments