Skip to content

Commit f43e8bb

Browse files
committed
feat: add driverName property to test classes for database compatibility.
1 parent e04784e commit f43e8bb

24 files changed

+61
-12
lines changed

tests/TestCase.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function array_values;
1717
use function dom_import_simplexml;
1818
use function file_get_contents;
19+
use function preg_replace;
1920
use function simplexml_load_string;
2021
use function str_replace;
2122

@@ -40,6 +41,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
4041
use SchemaBuilderTrait;
4142

4243
protected string|null $dsn = null;
44+
protected string $driverName = 'sqlite';
4345
protected string $fixtureDirectory = __DIR__ . '/support/data/';
4446
protected string $password = '';
4547
protected string $username = '';
@@ -105,12 +107,12 @@ protected function assertQueryHasOrderBy(ActiveQuery $query, string $methodName)
105107

106108
self::assertStringContainsString(
107109
'ORDER BY',
108-
$sql,
110+
$this->replaceQuotes($sql),
109111
"'{$methodName}' query should include 'ORDER BY' clause for deterministic results.",
110112
);
111113

112114
self::assertStringContainsString(
113-
'`lft`',
115+
$this->replaceQuotes('[[lft]]'),
114116
$sql,
115117
"'{$methodName}' query should order by 'left' attribute for consistent ordering.",
116118
);
@@ -362,6 +364,40 @@ protected function mockConsoleApplication(): void
362364
);
363365
}
364366

367+
/**
368+
* Adjust dbms specific escaping.
369+
*
370+
* @param string $sql SQL to adjust.
371+
*
372+
* @return string Adjusted SQL.
373+
*/
374+
protected function replaceQuotes(string $sql): string
375+
{
376+
return match ($this->driverName) {
377+
'mysql', 'sqlite' => str_replace(
378+
['[[', ']]'],
379+
'`',
380+
$sql,
381+
),
382+
'oci' => str_replace(
383+
['[[', ']]'],
384+
'"',
385+
$sql,
386+
),
387+
'pgsql' => str_replace(
388+
['\\[', '\\]'],
389+
['[', ']'],
390+
preg_replace('/(\[\[)|((?<!(\[))\]\])/', '"', $sql) ?? $sql,
391+
),
392+
'sqlsrv' => str_replace(
393+
['[[', ']]'],
394+
['[', ']'],
395+
$sql,
396+
),
397+
default => $sql,
398+
};
399+
}
400+
365401
/**
366402
* Applies database updates to tree nodes.
367403
*

tests/base/AbstractQueryBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function testRootsMethodRequiresLeftAttributeOrderingWhenTreeAttributeIsD
128128
"'roots()' query should include 'ORDER BY' clause for consistent results.",
129129
);
130130
self::assertStringContainsString(
131-
'`lft`',
131+
$this->replaceQuotes('[[lft]]'),
132132
$sql,
133133
"'roots()' query should order by 'left' attribute for deterministic ordering.",
134134
);

tests/mysql/CacheManagementTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
final class CacheManagementTest extends AbstractCacheManagement
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14+
protected string $driverName = 'mysql';
1415
protected string $password = 'root';
1516
protected string $username = 'root';
1617
}

tests/mysql/ExceptionHandlingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class ExceptionHandlingTest extends AbstractExceptionHandling
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14-
14+
protected string $driverName = 'mysql';
1515
protected string $password = 'root';
1616
protected string $username = 'root';
1717
}

tests/mysql/ExtensibilityTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
final class ExtensibilityTest extends AbstractExtensibility
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14+
protected string $driverName = 'mysql';
1415
protected string $password = 'root';
1516
protected string $username = 'root';
1617
}

tests/mysql/NodeAppendTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class NodeAppendTest extends AbstractNodeAppend
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14-
14+
protected string $driverName = 'mysql';
1515
protected string $password = 'root';
1616
protected string $username = 'root';
1717
}

tests/mysql/NodeDeleteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class NodeDeleteTest extends AbstractNodeDelete
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14-
14+
protected string $driverName = 'mysql';
1515
protected string $password = 'root';
1616
protected string $username = 'root';
1717
}

tests/mysql/NodeInsertTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class NodeInsertTest extends AbstractNodeInsert
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14-
14+
protected string $driverName = 'mysql';
1515
protected string $password = 'root';
1616
protected string $username = 'root';
1717
}

tests/mysql/NodePrependTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class NodePrependTest extends AbstractNodePrepend
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14-
14+
protected string $driverName = 'mysql';
1515
protected string $password = 'root';
1616
protected string $username = 'root';
1717
}

tests/mysql/NodeStateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
final class NodeStateTest extends AbstractNodeState
1212
{
1313
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
14-
14+
protected string $driverName = 'mysql';
1515
protected string $password = 'root';
1616
protected string $username = 'root';
1717
}

0 commit comments

Comments
 (0)