Merge pull request #19552 from kamil-tekiela/Fix-default-values
[phpmyadmin.git] / tests / unit / StorageEngineTest.php
blob4e843eda015c33d1696ea1e3597dec0410fab448
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Cache;
8 use PhpMyAdmin\Dbal\DatabaseInterface;
9 use PhpMyAdmin\Engines\Bdb;
10 use PhpMyAdmin\Engines\Berkeleydb;
11 use PhpMyAdmin\Engines\Binlog;
12 use PhpMyAdmin\Engines\Innobase;
13 use PhpMyAdmin\Engines\Innodb;
14 use PhpMyAdmin\Engines\Memory;
15 use PhpMyAdmin\Engines\Merge;
16 use PhpMyAdmin\Engines\MrgMyisam;
17 use PhpMyAdmin\Engines\Myisam;
18 use PhpMyAdmin\Engines\Ndbcluster;
19 use PhpMyAdmin\Engines\Pbxt;
20 use PhpMyAdmin\Engines\PerformanceSchema;
21 use PhpMyAdmin\StorageEngine;
22 use PhpMyAdmin\Tests\Stubs\DbiDummy;
23 use PHPUnit\Framework\Attributes\CoversClass;
24 use PHPUnit\Framework\Attributes\DataProvider;
26 use function json_encode;
28 #[CoversClass(StorageEngine::class)]
29 class StorageEngineTest extends AbstractTestCase
31 protected DatabaseInterface $dbi;
33 protected DbiDummy $dummyDbi;
35 protected StorageEngine $object;
37 /**
38 * Sets up the fixture, for example, opens a network connection.
39 * This method is called before a test is executed.
41 protected function setUp(): void
43 parent::setUp();
45 $this->dummyDbi = $this->createDbiDummy();
46 $this->dbi = $this->createDatabaseInterface($this->dummyDbi);
47 DatabaseInterface::$instance = $this->dbi;
48 $this->object = new StorageEngine('dummy');
51 /**
52 * Tears down the fixture, for example, closes a network connection.
53 * This method is called after a test is executed.
55 protected function tearDown(): void
57 parent::tearDown();
59 unset($this->object);
62 public function testGetStorageEngines(): void
64 self::assertSame(
66 'dummy' => ['Engine' => 'dummy', 'Support' => 'YES', 'Comment' => 'dummy comment'],
67 'dummy2' => ['Engine' => 'dummy2', 'Support' => 'NO', 'Comment' => 'dummy2 comment'],
68 'FEDERATED' => [
69 'Engine' => 'FEDERATED',
70 'Support' => 'NO',
71 'Comment' => 'Federated MySQL storage engine',
73 'Pbxt' => ['Engine' => 'Pbxt', 'Support' => 'NO', 'Comment' => 'Pbxt storage engine'],
75 StorageEngine::getStorageEngines(),
79 public function testGetArray(): void
81 $actual = StorageEngine::getArray();
83 self::assertSame(
84 ['dummy' => ['name' => 'dummy', 'comment' => 'dummy comment', 'is_default' => false]],
85 $actual,
89 /**
90 * Test for StorageEngine::getEngine
92 * @param string $expectedClass Class that should be selected
93 * @param string $engineName Engine name
94 * @psalm-param class-string $expectedClass
96 #[DataProvider('providerGetEngine')]
97 public function testGetEngine(string $expectedClass, string $engineName): void
99 $actual = StorageEngine::getEngine($engineName);
100 self::assertInstanceOf($expectedClass, $actual);
104 * Provider for testGetEngine
106 * @return mixed[]
108 public static function providerGetEngine(): array
110 return [
111 [StorageEngine::class, 'unknown engine'],
112 [Bdb::class, 'Bdb'],
113 [Berkeleydb::class, 'Berkeleydb'],
114 [Binlog::class, 'Binlog'],
115 [Innobase::class, 'Innobase'],
116 [Innodb::class, 'Innodb'],
117 [Memory::class, 'Memory'],
118 [Merge::class, 'Merge'],
119 [MrgMyisam::class, 'Mrg_Myisam'],
120 [Myisam::class, 'Myisam'],
121 [Ndbcluster::class, 'Ndbcluster'],
122 [Pbxt::class, 'Pbxt'],
123 [PerformanceSchema::class, 'Performance_Schema'],
128 * Test for isValid
130 public function testIsValid(): void
132 self::assertTrue(
133 StorageEngine::isValid('PBMS'),
135 self::assertTrue(
136 StorageEngine::isValid('dummy'),
138 self::assertTrue(
139 StorageEngine::isValid('dummy2'),
141 self::assertFalse(
142 StorageEngine::isValid('invalid'),
147 * Test for getPage
149 public function testGetPage(): void
151 self::assertSame(
153 $this->object->getPage('Foo'),
158 * Test for getInfoPages
160 public function testGetInfoPages(): void
162 self::assertSame(
164 $this->object->getInfoPages(),
169 * Test for getVariablesLikePattern
171 public function testGetVariablesLikePattern(): void
173 self::assertSame(
175 $this->object->getVariablesLikePattern(),
180 * Test for getMysqlHelpPage
182 public function testGetMysqlHelpPage(): void
184 self::assertSame(
185 'dummy-storage-engine',
186 $this->object->getMysqlHelpPage(),
191 * Test for getVariables
193 public function testGetVariables(): void
195 self::assertSame(
197 $this->object->getVariables(),
202 * Test for getSupportInformationMessage
204 public function testGetSupportInformationMessage(): void
206 self::assertSame(
207 'dummy is available on this MySQL server.',
208 $this->object->getSupportInformationMessage(),
211 $this->object->support = 1;
212 self::assertSame(
213 'dummy has been disabled for this MySQL server.',
214 $this->object->getSupportInformationMessage(),
217 $this->object->support = 2;
218 self::assertSame(
219 'dummy is available on this MySQL server.',
220 $this->object->getSupportInformationMessage(),
223 $this->object->support = 3;
224 self::assertSame(
225 'dummy is the default storage engine on this MySQL server.',
226 $this->object->getSupportInformationMessage(),
231 * Test for getComment
233 public function testGetComment(): void
235 self::assertSame(
236 'dummy comment',
237 $this->object->getComment(),
242 * Test for getTitle
244 public function testGetTitle(): void
246 self::assertSame(
247 'dummy',
248 $this->object->getTitle(),
253 * Test for resolveTypeSize
255 public function testResolveTypeSize(): void
257 self::assertEquals(
258 [12, 'B'],
259 $this->object->resolveTypeSize(12),
263 public function testHasMroongaEngine(): void
265 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_list\');', [
267 (string) json_encode([]), // Fake result
270 self::assertTrue(StorageEngine::hasMroongaEngine());
271 self::assertTrue(StorageEngine::hasMroongaEngine()); // Does not call any query
273 Cache::remove('storage-engine.mroonga.has.mroonga_command'); // Cache clear
275 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_list\');', false);
276 self::assertFalse(StorageEngine::hasMroongaEngine());
278 $this->dummyDbi->assertAllQueriesConsumed();
281 public function testGetMroongaLengths(): void
283 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_list\');', [
285 // Partial
286 (string) json_encode([
287 'WGS84GeoPoint' => [
288 'id' => 18,
289 'name' => 'WGS84GeoPoint',
290 'opened' => true,
291 'n_elements' => 4,
292 'type' => ['id' => 32, 'name' => 'type'],
293 'flags' => ['value' => 24, 'names' => 'KEY_GEO_POINT'],
294 'path' => null,
295 'size' => 8,
297 'mroonga_operations' => ['type' => ['id' => 51, 'name' => 'table:no_key']],
298 'mroonga_operations.type' => ['type' => ['id' => 65, 'name' => 'column:var_size']],
299 'mroonga_operations.table' => ['type' => ['id' => 65, 'name' => 'column:var_size']],
300 'mroonga_operations.record' => ['type' => ['id' => 64, 'name' => 'column:fix_size']],
301 'idx_correo' => ['type' => ['id' => 49, 'name' => 'table:pat_key']],
302 'idx_correo.id' => ['type' => ['id' => 64, 'name' => 'column:fix_size']],
303 'idx_correo.search' => ['type' => ['id' => 65, 'name' => 'column:var_size']],
304 'idx_correo#idx_correo_search' => ['type' => ['id' => 49, 'name' => 'table:pat_key']],
305 'idx_correo#idx_correo_search.index' => ['type' => ['id' => 72, 'name' => 'column:index']],
309 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo\');', [
311 // Partial
312 (string) json_encode([
313 'id' => 265,
314 'name' => 'idx_correo',
315 'type' => ['id' => 49, 'name' => 'table:pat_key'],
316 'key' => [
317 'type' => ['id' => 8, 'name' => 'Int32', 'type' => ['id' => 32,'name' => 'type'], 'size' => 4],
318 'total_size' => 0,
319 'max_total_size' => 4294967294,
321 'value' => ['type' => null],
322 'n_records' => 0,
323 'disk_usage' => 4243456,
327 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo.id\');', [
329 // Full object
330 (string) json_encode([
331 'id' => 266,
332 'name' => 'id',
333 'table' => [
334 'id' => 265,
335 'name' => 'idx_correo',
336 'type' => ['id' => 49, 'name' => 'table:pat_key'],
337 'key' => [
338 'type' => [
339 'id' => 8,
340 'name' => 'Int32',
341 'type' => ['id' => 32, 'name' => 'type'],
342 'size' => 4,
344 'total_size' => 0,
345 'max_total_size' => 4294967294,
347 'value' => ['type' => null],
348 'n_records' => 0,
349 'disk_usage' => 4243456,
351 'full_name' => 'idx_correo.id',
352 'type' => ['name' => 'scalar', 'raw' => ['id' => 64, 'name' => 'column:fix_size']],
353 'value' => [
354 'type' => ['id' => 8, 'name' => 'Int32', 'type' => ['id' => 32,'name' => 'type'], 'size' => 4],
355 'compress' => null,
357 'disk_usage' => 4096,
361 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo.search\');', [
363 // Full object
364 (string) json_encode([
365 'id' => 267,
366 'name' => 'search',
367 'table' => [
368 'id' => 265,
369 'name' => 'idx_correo',
370 'type' => ['id' => 49, 'name' => 'table:pat_key'],
371 'key' => [
372 'type' => [
373 'id' => 8,
374 'name' => 'Int32',
375 'type' => ['id' => 32, 'name' => 'type'],
376 'size' => 4,
378 'total_size' => 0,
379 'max_total_size' => 4294967294,
381 'value' => ['type' => null],
382 'n_records' => 0,
383 'disk_usage' => 4243456,
385 'full_name' => 'idx_correo.search',
386 'type' => ['name' => 'scalar', 'raw' => ['id' => 65, 'name' => 'column:var_size']],
387 'value' => [
388 'type' => [
389 'id' => 16,
390 'name' => 'LongText',
391 'type' => ['id' => 32, 'name' => 'type'],
392 'size' => 2147483648,
394 'compress' => null,
396 'disk_usage' => 274432,
400 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo#idx_correo_search\');', [
402 // Partial
403 (string) json_encode([
404 'id' => 268,
405 'name' => 'idx_correo#idx_correo_search',
406 'type' => ['id' => 49, 'name' => 'table:pat_key'],
407 'key' => [
408 'type' => [
409 'id' => 14,
410 'name' => 'ShortText',
411 'type' => ['id' => 32, 'name' => 'type'],
412 'size' => 4096,
414 'total_size' => 0,
415 'max_total_size' => 4294967294,
417 'value' => ['type' => null],
418 'n_records' => 0,
419 'disk_usage' => 12878,
423 $this->dummyDbi->addResult('SELECT mroonga_command(\'object_inspect idx_correo#idx_correo_search.index\');', [
425 // Full object
426 (string) json_encode([
427 'id' => 269,
428 'name' => 'index',
429 'table' => [
430 'id' => 268,
431 'name' => 'idx_correo#idx_correo_search',
432 'type' => ['id' => 49, 'name' => 'table:pat_key'],
433 'key' => [
434 'type' => [
435 'id' => 14,
436 'name' => 'ShortText',
437 'type' => ['id' => 32, 'name' => 'type'],
438 'size' => 4096,
440 'total_size' => 0,
441 'max_total_size' => 4294967294,
443 'value' => ['type' => null],
444 'n_records' => 0,
445 'disk_usage' => 4243456,
447 'full_name' => 'idx_correo#idx_correo_search.index',
448 'type' => ['name' => 'index', 'raw' => ['id' => 72, 'name' => 'column:index']],
449 'value' => [
450 'type' => [
451 'id' => 265,
452 'name' => 'idx_correo',
453 'type' => ['id' => 49, 'name' => 'table:pat_key'],
454 'size' => 4,
456 'section' => false,
457 'weight' => false,
458 'position' => true,
459 'size' => 'normal',
460 'statistics' => [
461 'max_section_id' => 0,
462 'n_garbage_segments' => 0,
463 'max_array_segment_id' => 0,
464 'n_array_segments' => 0,
465 'max_buffer_segment_id' => 0,
466 'n_buffer_segments' => 0,
467 'max_in_use_physical_segment_id' => 0,
468 'n_unmanaged_segments' => 0,
469 'total_chunk_size' => 0,
470 'max_in_use_chunk_id' => 0,
471 'n_garbage_chunks' => [
490 'sources' => [
492 'id' => 267,
493 'name' => 'search',
494 'table' => [
495 'id' => 265,
496 'name' => 'idx_correo',
497 'type' => ['id' => 49, 'name' => 'table:pat_key'],
498 'key' => [
499 'type' => [
500 'id' => 8,
501 'name' => 'Int32',
502 'type' => ['id' => 32, 'name' => 'type'],
503 'size' => 4,
505 'total_size' => 0,
506 'max_total_size' => 4294967294,
508 'value' => ['type' => null],
509 'n_records' => 0,
510 'disk_usage' => 4243456,
512 'full_name' => 'idx_correo.search',
515 'disk_usage' => 565248,
520 $this->dummyDbi->addSelectDb('my_db');
521 $lengths = StorageEngine::getMroongaLengths('my_db', 'idx_correo');
522 $this->dummyDbi->assertAllSelectsConsumed();
523 self::assertSame([4521984, 578126], $lengths);
525 $this->dummyDbi->assertAllQueriesConsumed();