Skip to content

Commit 9baf923

Browse files
committed
Add basic test for Export::exportDatabase
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent d85cb55 commit 9baf923

1 file changed

Lines changed: 77 additions & 23 deletions

File tree

test/classes/ExportTest.php

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpMyAdmin\ConfigStorage\Relation;
88
use PhpMyAdmin\Export;
99
use PhpMyAdmin\Plugins\Export\ExportPhparray;
10+
use PhpMyAdmin\Plugins\Export\ExportSql;
1011
use PhpMyAdmin\Transformations;
1112

1213
/**
@@ -15,24 +16,10 @@
1516
*/
1617
class ExportTest extends AbstractTestCase
1718
{
18-
/** @var Export */
19-
private $export;
20-
21-
/**
22-
* Sets up the fixture
23-
*/
24-
protected function setUp(): void
25-
{
26-
parent::setUp();
27-
$GLOBALS['dbi'] = $this->createDatabaseInterface();
28-
$this->export = new Export($GLOBALS['dbi']);
29-
}
30-
31-
/**
32-
* Test for mergeAliases
33-
*/
3419
public function testMergeAliases(): void
3520
{
21+
$GLOBALS['dbi'] = $this->createDatabaseInterface();
22+
$export = new Export($GLOBALS['dbi']);
3623
$aliases1 = [
3724
'test_db' => [
3825
'alias' => 'aliastest',
@@ -91,31 +78,30 @@ public function testMergeAliases(): void
9178
],
9279
],
9380
];
94-
$actual = $this->export->mergeAliases($aliases1, $aliases2);
81+
$actual = $export->mergeAliases($aliases1, $aliases2);
9582
$this->assertEquals($expected, $actual);
9683
}
9784

98-
/**
99-
* Test for getFinalFilenameAndMimetypeForFilename
100-
*/
10185
public function testGetFinalFilenameAndMimetypeForFilename(): void
10286
{
87+
$GLOBALS['dbi'] = $this->createDatabaseInterface();
88+
$export = new Export($GLOBALS['dbi']);
10389
$exportPlugin = new ExportPhparray(
10490
new Relation($GLOBALS['dbi']),
10591
new Export($GLOBALS['dbi']),
10692
new Transformations()
10793
);
108-
$finalFileName = $this->export->getFinalFilenameAndMimetypeForFilename($exportPlugin, 'zip', 'myfilename');
94+
$finalFileName = $export->getFinalFilenameAndMimetypeForFilename($exportPlugin, 'zip', 'myfilename');
10995
$this->assertSame([
11096
'myfilename.php.zip',
11197
'application/zip',
11298
], $finalFileName);
113-
$finalFileName = $this->export->getFinalFilenameAndMimetypeForFilename($exportPlugin, 'gzip', 'myfilename');
99+
$finalFileName = $export->getFinalFilenameAndMimetypeForFilename($exportPlugin, 'gzip', 'myfilename');
114100
$this->assertSame([
115101
'myfilename.php.gz',
116102
'application/x-gzip',
117103
], $finalFileName);
118-
$finalFileName = $this->export->getFinalFilenameAndMimetypeForFilename(
104+
$finalFileName = $export->getFinalFilenameAndMimetypeForFilename(
119105
$exportPlugin,
120106
'gzip',
121107
'export.db1.table1.file'
@@ -125,4 +111,72 @@ public function testGetFinalFilenameAndMimetypeForFilename(): void
125111
'application/x-gzip',
126112
], $finalFileName);
127113
}
114+
115+
public function testExportDatabase(): void
116+
{
117+
$GLOBALS['sql_create_view'] = 'something';
118+
$GLOBALS['output_kanji_conversion'] = false;
119+
$GLOBALS['buffer_needed'] = false;
120+
$GLOBALS['asfile'] = false;
121+
$GLOBALS['sql_structure_or_data'] = 'structure_and_data';
122+
$GLOBALS['cfg']['Server']['DisableIS'] = false;
123+
$GLOBALS['sql_insert_syntax'] = 'both';
124+
$GLOBALS['sql_max_query_size'] = '50000';
125+
126+
// phpcs:disable Generic.Files.LineLength.TooLong
127+
$dbiDummy = $this->createDbiDummy();
128+
$dbiDummy->addResult(
129+
'SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = \'test_db\' AND TABLE_NAME = \'test_table\'',
130+
[],
131+
['TABLE_NAME']
132+
);
133+
$dbiDummy->addResult(
134+
'SELECT *, `TABLE_SCHEMA` AS `Db`, `TABLE_NAME` AS `Name`, `TABLE_TYPE` AS `TABLE_TYPE`, `ENGINE` AS `Engine`, `ENGINE` AS `Type`, `VERSION` AS `Version`, `ROW_FORMAT` AS `Row_format`, `TABLE_ROWS` AS `Rows`, `AVG_ROW_LENGTH` AS `Avg_row_length`, `DATA_LENGTH` AS `Data_length`, `MAX_DATA_LENGTH` AS `Max_data_length`, `INDEX_LENGTH` AS `Index_length`, `DATA_FREE` AS `Data_free`, `AUTO_INCREMENT` AS `Auto_increment`, `CREATE_TIME` AS `Create_time`, `UPDATE_TIME` AS `Update_time`, `CHECK_TIME` AS `Check_time`, `TABLE_COLLATION` AS `Collation`, `CHECKSUM` AS `Checksum`, `CREATE_OPTIONS` AS `Create_options`, `TABLE_COMMENT` AS `Comment` FROM `information_schema`.`TABLES` t WHERE `TABLE_SCHEMA` IN (\'test_db\') AND t.`TABLE_NAME` = \'test_table\' ORDER BY Name ASC',
135+
[['def', 'test_db', 'test_table', 'BASE TABLE', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '0', '0', '4', '2011-12-13 14:15:16', null, null, 'utf8mb4_general_ci', null, '', '', '0', 'N', 'test_db', 'test_table', 'BASE TABLE', 'InnoDB', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '0', '0', '4', '2011-12-13 14:15:16', null, null, 'utf8mb4_general_ci', null, '', '']],
136+
['TABLE_CATALOG', 'TABLE_SCHEMA', 'TABLE_NAME', 'TABLE_TYPE', 'ENGINE', 'VERSION', 'ROW_FORMAT', 'TABLE_ROWS', 'AVG_ROW_LENGTH', 'DATA_LENGTH', 'MAX_DATA_LENGTH', 'INDEX_LENGTH', 'DATA_FREE', 'AUTO_INCREMENT', 'CREATE_TIME', 'UPDATE_TIME', 'CHECK_TIME', 'TABLE_COLLATION', 'CHECKSUM', 'CREATE_OPTIONS', 'TABLE_COMMENT', 'MAX_INDEX_LENGTH', 'TEMPORARY', 'Db', 'Name', 'TABLE_TYPE', 'Engine', 'Type', 'Version', 'Row_format', 'Rows', 'Avg_row_length', 'Data_length', 'Max_data_length', 'Index_length', 'Data_free', 'Auto_increment', 'Create_time', 'Update_time', 'Check_time', 'Collation', 'Checksum', 'Create_options', 'Comment']
137+
);
138+
$dbiDummy->addResult(
139+
'SELECT `id`, `name`, `datetimefield` FROM `test_db`.`test_table`',
140+
[
141+
['1', 'abcd', '2011-01-20 02:00:02'],
142+
['2', 'foo', '2010-01-20 02:00:02'],
143+
['3', 'Abcd', '2012-01-20 02:00:02'],
144+
],
145+
['id', 'name', 'datetimefield']
146+
);
147+
// phpcs:enable
148+
149+
$dbi = $this->createDatabaseInterface($dbiDummy);
150+
$GLOBALS['dbi'] = $dbi;
151+
$export = new Export($dbi);
152+
153+
$export->exportDatabase(
154+
'test_db',
155+
['test_table'],
156+
'structure_and_data',
157+
['test_table'],
158+
['test_table'],
159+
new ExportSql(new Relation($dbi), $export, new Transformations()),
160+
"\n",
161+
'index.php?route=/database/export&db=test_db',
162+
'database',
163+
false,
164+
true,
165+
false,
166+
false,
167+
[],
168+
''
169+
);
170+
171+
$expected = <<<SQL
172+
173+
INSERT INTO test_table (id, name, datetimefield) VALUES
174+
('1', 'abcd', '2011-01-20 02:00:02'),
175+
('2', 'foo', '2010-01-20 02:00:02'),
176+
('3', 'Abcd', '2012-01-20 02:00:02');
177+
178+
SQL;
179+
180+
$this->assertSame($expected, $this->getActualOutputForAssertion());
181+
}
128182
}

0 commit comments

Comments
 (0)