1616use function array_values ;
1717use function dom_import_simplexml ;
1818use function file_get_contents ;
19+ use function preg_replace ;
1920use function simplexml_load_string ;
2021use function str_replace ;
2122
3839class TestCase extends \PHPUnit \Framework \TestCase
3940{
4041 use SchemaBuilderTrait;
42+ protected string $ driverName = 'sqlite ' ;
4143
4244 protected string |null $ dsn = null ;
4345 protected string $ fixtureDirectory = __DIR__ . '/support/data/ ' ;
46+ protected string $ password = '' ;
47+ protected string $ username = '' ;
4448
4549 protected function setUp (): void
4650 {
@@ -103,12 +107,12 @@ protected function assertQueryHasOrderBy(ActiveQuery $query, string $methodName)
103107
104108 self ::assertStringContainsString (
105109 'ORDER BY ' ,
106- $ sql ,
110+ $ this -> replaceQuotes ( $ sql) ,
107111 "' {$ methodName }' query should include 'ORDER BY' clause for deterministic results. " ,
108112 );
109113
110114 self ::assertStringContainsString (
111- ' ` lft` ' ,
115+ $ this -> replaceQuotes ( ' [[ lft]] ' ) ,
112116 $ sql ,
113117 "' {$ methodName }' query should order by 'left' attribute for consistent ordering. " ,
114118 );
@@ -167,11 +171,11 @@ protected function createDatabase(): void
167171 $ command = $ this ->getDb ()->createCommand ();
168172
169173 if ($ this ->getDb ()->getTableSchema ('tree ' , true ) !== null ) {
170- $ command ->dropTable ('tree ' );
174+ $ command ->dropTable ('tree ' )-> execute () ;
171175 }
172176
173177 if ($ this ->getDb ()->getTableSchema ('multiple_tree ' , true ) !== null ) {
174- $ command ->dropTable ('multiple_tree ' );
178+ $ command ->dropTable ('multiple_tree ' )-> execute () ;
175179 }
176180
177181 $ command ->createTable (
@@ -291,14 +295,14 @@ protected function generateFixtureTree(): void
291295 */
292296 protected function getDataSet (): array
293297 {
294- $ dataSetTree = Tree::find ()->asArray ()->all ();
298+ $ dataSetTree = Tree::find ()->orderBy ([ ' id ' => SORT_ASC ])-> asArray ()->all ();
295299
296300 foreach ($ dataSetTree as $ key => $ value ) {
297301 $ dataSetTree [$ key ]['type ' ] = 'tree ' ;
298302 $ dataSetTree [$ key ]['tree ' ] = 0 ;
299303 }
300304
301- $ dataSetMultipleTree = MultipleTree::find ()->asArray ()->all ();
305+ $ dataSetMultipleTree = MultipleTree::find ()->orderBy ([ ' id ' => SORT_ASC ])-> asArray ()->all ();
302306
303307 foreach ($ dataSetMultipleTree as $ key => $ value ) {
304308 $ dataSetMultipleTree [$ key ]['type ' ] = 'multiple_tree ' ;
@@ -314,7 +318,7 @@ protected function getDataSet(): array
314318 */
315319 protected function getDataSetMultipleTree (): array
316320 {
317- $ dataSetMultipleTree = MultipleTree::find ()->asArray ()->all ();
321+ $ dataSetMultipleTree = MultipleTree::find ()->orderBy ([ ' id ' => SORT_ASC ])-> asArray ()->all ();
318322
319323 foreach ($ dataSetMultipleTree as $ key => $ value ) {
320324 $ dataSetMultipleTree [$ key ]['type ' ] = 'multiple_tree ' ;
@@ -352,12 +356,48 @@ protected function mockConsoleApplication(): void
352356 'db ' => [
353357 'class ' => Connection::class,
354358 'dsn ' => $ this ->dsn !== null ? $ this ->dsn : 'sqlite::memory: ' ,
359+ 'password ' => $ this ->password ,
360+ 'username ' => $ this ->username ,
355361 ],
356362 ],
357363 ],
358364 );
359365 }
360366
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+
361401 /**
362402 * Applies database updates to tree nodes.
363403 *
0 commit comments