diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-04-19 03:50:18 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-04-19 03:50:18 +0300 |
commit | a9de97939719889579cf41e3f6d3328d178b9b95 (patch) | |
tree | 5e90f2cc75043becaacacce3185dadeb3c743e20 /test | |
parent | aa829af98c0eeec32037d5ddf9acc34b7337bd04 (diff) |
Inline dbi creation in some test classes
Creates a new instance of DatabaseInterface class for each test.
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
Diffstat (limited to 'test')
8 files changed, 63 insertions, 40 deletions
diff --git a/test/classes/AbstractTestCase.php b/test/classes/AbstractTestCase.php index 3871caf0e0..cc20542d34 100644 --- a/test/classes/AbstractTestCase.php +++ b/test/classes/AbstractTestCase.php @@ -189,14 +189,19 @@ abstract class AbstractTestCase extends TestCase protected function setGlobalDbi(): void { - $this->dummyDbi = new DbiDummy(); - $this->dbi = $this->getDatabaseInterface($this->dummyDbi); + $this->dummyDbi = $this->createDbiDummy(); + $this->dbi = $this->createDatabaseInterface($this->dummyDbi); $GLOBALS['dbi'] = $this->dbi; } - protected function getDatabaseInterface(?DbiExtension $extension = null): DatabaseInterface + protected function createDatabaseInterface(?DbiExtension $extension = null): DatabaseInterface { - return new DatabaseInterface($extension ?? new DbiDummy()); + return new DatabaseInterface($extension ?? $this->createDbiDummy()); + } + + protected function createDbiDummy(): DbiDummy + { + return new DbiDummy(); } protected function setGlobalConfig(): void diff --git a/test/classes/Controllers/Server/Status/Monitor/QueryAnalyzerControllerTest.php b/test/classes/Controllers/Server/Status/Monitor/QueryAnalyzerControllerTest.php index f8af9318cc..4c1dd9f407 100644 --- a/test/classes/Controllers/Server/Status/Monitor/QueryAnalyzerControllerTest.php +++ b/test/classes/Controllers/Server/Status/Monitor/QueryAnalyzerControllerTest.php @@ -35,7 +35,7 @@ class QueryAnalyzerControllerTest extends AbstractTestCase $response->setAjax(true); $dummyDbi = new DbiDummy(); - $dbi = $this->getDatabaseInterface($dummyDbi); + $dbi = $this->createDatabaseInterface($dummyDbi); $controller = new QueryAnalyzerController($response, new Template(), new Data(), new Monitor($dbi), $dbi); diff --git a/test/classes/Controllers/Table/AddFieldControllerTest.php b/test/classes/Controllers/Table/AddFieldControllerTest.php index 81f25609c3..44337c9686 100644 --- a/test/classes/Controllers/Table/AddFieldControllerTest.php +++ b/test/classes/Controllers/Table/AddFieldControllerTest.php @@ -32,8 +32,11 @@ class AddFieldControllerTest extends AbstractTestCase 'after_field' => 'datetimefield', ]; - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); + $dummyDbi = $this->createDbiDummy(); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); + $dbi = $this->createDatabaseInterface($dummyDbi); + $GLOBALS['dbi'] = $dbi; $contentCell = [ 'column_number' => 0, @@ -165,7 +168,7 @@ class AddFieldControllerTest extends AbstractTestCase 'mime_map' => [], ]; - $relation = new Relation($this->dbi); + $relation = new Relation($dbi); $response = new ResponseRenderer(); $template = new Template(); $expected = $template->render('columns_definitions/column_definitions_form', [ @@ -236,14 +239,14 @@ class AddFieldControllerTest extends AbstractTestCase 'is_integers_length_restricted' => false, 'browse_mime' => true, 'supports_stored_keyword' => false, - 'server_version' => $this->dbi->getVersion(), + 'server_version' => $dbi->getVersion(), 'max_rows' => 25, 'char_editing' => 'input', 'attribute_types' => ['', 'BINARY', 'UNSIGNED', 'UNSIGNED ZEROFILL', 'on update CURRENT_TIMESTAMP'], 'privs_available' => false, 'max_length' => 1024, 'have_partitioning' => true, - 'dbi' => $this->dbi, + 'dbi' => $dbi, 'disable_is' => true, ]); @@ -253,8 +256,8 @@ class AddFieldControllerTest extends AbstractTestCase $template, $transformations, new Config(), - $this->dbi, - new ColumnsDefinition($this->dbi, $relation, $transformations) + $dbi, + new ColumnsDefinition($dbi, $relation, $transformations) ))(); $this->assertSame($expected, $response->getHTMLResult()); diff --git a/test/classes/Controllers/Table/ChangeControllerTest.php b/test/classes/Controllers/Table/ChangeControllerTest.php index 6a939be670..4ec0b6283e 100644 --- a/test/classes/Controllers/Table/ChangeControllerTest.php +++ b/test/classes/Controllers/Table/ChangeControllerTest.php @@ -22,14 +22,17 @@ class ChangeControllerTest extends AbstractTestCase $GLOBALS['db'] = 'test_db'; $GLOBALS['table'] = 'test_table'; - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); - $this->dummyDbi->addResult( + $dummyDbi = $this->createDbiDummy(); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); + $dummyDbi->addResult( 'SELECT * FROM `test_db`.`test_table` LIMIT 1;', [['1', 'abcd', '2011-01-20 02:00:02']], ['id', 'name', 'datetimefield'] ); - $this->dummyDbi->addSelectDb('test_db'); + $dummyDbi->addSelectDb('test_db'); + $dbi = $this->createDatabaseInterface($dummyDbi); + $GLOBALS['dbi'] = $dbi; $response = new ResponseRenderer(); $pageSettings = new PageSettings('Edit'); @@ -37,8 +40,8 @@ class ChangeControllerTest extends AbstractTestCase (new ChangeController( $response, new Template(), - new InsertEdit($this->dbi), - new Relation($this->dbi) + new InsertEdit($dbi), + new Relation($dbi) ))(); $actual = $response->getHTMLResult(); diff --git a/test/classes/Controllers/Table/ChartControllerTest.php b/test/classes/Controllers/Table/ChartControllerTest.php index 3c3e67a175..8f71a62886 100644 --- a/test/classes/Controllers/Table/ChartControllerTest.php +++ b/test/classes/Controllers/Table/ChartControllerTest.php @@ -55,10 +55,11 @@ class ChartControllerTest extends AbstractTestCase ), ]; - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult('SHOW TABLES LIKE \'table_for_chart\';', [['table_for_chart']]); - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult( + $dummyDbi = $this->createDbiDummy(); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult('SHOW TABLES LIKE \'table_for_chart\';', [['table_for_chart']]); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult( 'SELECT * FROM `test_db`.`table_for_chart`;', [ ['1', '7', '2022-02-08'], @@ -69,6 +70,8 @@ class ChartControllerTest extends AbstractTestCase ['id', 'amount', 'date'], $fieldsMeta ); + $dbi = $this->createDatabaseInterface($dummyDbi); + $GLOBALS['dbi'] = $dbi; $response = new ResponseRenderer(); $template = new Template(); @@ -91,7 +94,7 @@ class ChartControllerTest extends AbstractTestCase ], ]); - (new ChartController($response, $template, $this->dbi))(); + (new ChartController($response, $template, $dbi))(); $this->assertSame($expected, $response->getHTMLResult()); } } diff --git a/test/classes/Controllers/Table/CreateControllerTest.php b/test/classes/Controllers/Table/CreateControllerTest.php index 1340833567..b1fc6be5ce 100644 --- a/test/classes/Controllers/Table/CreateControllerTest.php +++ b/test/classes/Controllers/Table/CreateControllerTest.php @@ -27,10 +27,13 @@ class CreateControllerTest extends AbstractTestCase $GLOBALS['cfg']['Server'] = $GLOBALS['config']->defaultServer; $_POST = ['db' => 'test_db', 'table' => 'new_test_table', 'num_fields' => '2']; - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult('SHOW COLUMNS FROM `test_db`.`new_test_table`', false); - $this->dummyDbi->addResult('SHOW FULL COLUMNS FROM `test_db`.`new_test_table`', false); - $this->dummyDbi->addResult('SHOW CREATE TABLE `test_db`.`new_test_table`', false); + $dummyDbi = $this->createDbiDummy(); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult('SHOW COLUMNS FROM `test_db`.`new_test_table`', false); + $dummyDbi->addResult('SHOW FULL COLUMNS FROM `test_db`.`new_test_table`', false); + $dummyDbi->addResult('SHOW CREATE TABLE `test_db`.`new_test_table`', false); + $dbi = $this->createDatabaseInterface($dummyDbi); + $GLOBALS['dbi'] = $dbi; $contentCell = [ 'column_number' => 0, @@ -162,7 +165,7 @@ class CreateControllerTest extends AbstractTestCase 'mime_map' => [], ]; - $relation = new Relation($this->dbi); + $relation = new Relation($dbi); $response = new ResponseRenderer(); $template = new Template(); $expected = $template->render('columns_definitions/column_definitions_form', [ @@ -231,14 +234,14 @@ class CreateControllerTest extends AbstractTestCase 'is_integers_length_restricted' => false, 'browse_mime' => true, 'supports_stored_keyword' => false, - 'server_version' => $this->dbi->getVersion(), + 'server_version' => $dbi->getVersion(), 'max_rows' => 25, 'char_editing' => 'input', 'attribute_types' => ['', 'BINARY', 'UNSIGNED', 'UNSIGNED ZEROFILL', 'on update CURRENT_TIMESTAMP'], 'privs_available' => false, 'max_length' => 1024, 'have_partitioning' => true, - 'dbi' => $this->dbi, + 'dbi' => $dbi, 'disable_is' => false, ]); @@ -248,8 +251,8 @@ class CreateControllerTest extends AbstractTestCase $template, $transformations, new Config(), - $this->dbi, - new ColumnsDefinition($this->dbi, $relation, $transformations) + $dbi, + new ColumnsDefinition($dbi, $relation, $transformations) ))(); $this->assertSame($expected, $response->getHTMLResult()); diff --git a/test/classes/Controllers/Table/DeleteConfirmControllerTest.php b/test/classes/Controllers/Table/DeleteConfirmControllerTest.php index 12bd55feff..4d0215ebf2 100644 --- a/test/classes/Controllers/Table/DeleteConfirmControllerTest.php +++ b/test/classes/Controllers/Table/DeleteConfirmControllerTest.php @@ -26,8 +26,11 @@ class DeleteConfirmControllerTest extends AbstractTestCase 'sql_query' => 'SELECT * FROM `test_db`.`test_table`', ]; - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); + $dummyDbi = $this->createDbiDummy(); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); + $dbi = $this->createDatabaseInterface($dummyDbi); + $GLOBALS['dbi'] = $dbi; $response = new ResponseRenderer(); $template = new Template(); diff --git a/test/classes/Controllers/Table/DeleteRowsControllerTest.php b/test/classes/Controllers/Table/DeleteRowsControllerTest.php index 64f7d32f31..73e0865967 100644 --- a/test/classes/Controllers/Table/DeleteRowsControllerTest.php +++ b/test/classes/Controllers/Table/DeleteRowsControllerTest.php @@ -33,23 +33,26 @@ class DeleteRowsControllerTest extends AbstractTestCase 'mult_btn' => 'Yes', ]; - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult('DELETE FROM `test_table` WHERE `test_table`.`id` = 3 LIMIT 1;', []); - $this->dummyDbi->addSelectDb('test_db'); - $this->dummyDbi->addResult( + $dummyDbi = $this->createDbiDummy(); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult('DELETE FROM `test_table` WHERE `test_table`.`id` = 3 LIMIT 1;', []); + $dummyDbi->addSelectDb('test_db'); + $dummyDbi->addResult( 'SELECT * FROM `test_db`.`test_table` LIMIT 0, 25', [['1', 'abcd', '2011-01-20 02:00:02'], ['2', 'foo', '2010-01-20 02:00:02']], ['id', 'name', 'datetimefield'] ); - $this->dummyDbi->addResult( + $dummyDbi->addResult( 'SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = \'test_db\'' . ' AND TABLE_NAME = \'test_table\' AND IS_UPDATABLE = \'YES\'', [], ['TABLE_NAME'] ); + $dbi = $this->createDatabaseInterface($dummyDbi); + $GLOBALS['dbi'] = $dbi; $response = new ResponseRenderer(); - (new DeleteRowsController($response, new Template(), $this->dbi))(); + (new DeleteRowsController($response, new Template(), $dbi))(); $actual = $response->getHTMLResult(); $this->assertStringContainsString( '<div class="alert alert-success" role="alert">Your SQL query has been executed successfully.</div>', |