3 namespace Drupal\Tests\system\Kernel\Scripts;
5 use Drupal\Core\Command\DbDumpCommand;
6 use Drupal\Core\Database\Database;
7 use Drupal\KernelTests\KernelTestBase;
8 use Symfony\Component\Console\Tester\CommandTester;
11 * Test that the DbDumpCommand works correctly.
15 class DbDumpCommandTest extends KernelTestBase {
20 public static $modules = ['system'];
25 protected function setUp() {
28 // Determine what database backend is running, and set the skip flag.
29 if (Database::getConnection()->databaseType() !== 'mysql') {
30 $this->markTestSkipped("Skipping test since the DbDumpCommand is currently only compatible with MySQL");
33 // Rebuild the router to ensure a routing table.
34 \Drupal::service('router.builder')->rebuild();
36 /** @var \Drupal\Core\Database\Connection $connection */
37 $connection = $this->container->get('database');
38 $connection->insert('router')->fields(['name', 'path', 'pattern_outline'])->values(['test', 'test', 'test'])->execute();
42 * Test the command directly.
44 public function testDbDumpCommand() {
45 $command = new DbDumpCommand();
46 $command_tester = new CommandTester($command);
47 $command_tester->execute([]);
49 // Assert that insert exists and that some expected fields exist.
50 $output = $command_tester->getDisplay();
51 $this->assertContains("createTable('router", $output, 'Table router found');
52 $this->assertContains("insert('router", $output, 'Insert found');
53 $this->assertContains("'name' => 'test", $output, 'Insert name field found');
54 $this->assertContains("'path' => 'test", $output, 'Insert path field found');
55 $this->assertContains("'pattern_outline' => 'test", $output, 'Insert pattern_outline field found');
56 $this->assertContains("// @codingStandardsIgnoreFile", $output);
60 * Test schema only option.
62 public function testSchemaOnly() {
63 $command = new DbDumpCommand();
64 $command_tester = new CommandTester($command);
65 $command_tester->execute(['--schema-only' => 'router']);
67 // Assert that insert statement doesn't exist for schema only table.
68 $output = $command_tester->getDisplay();
69 $this->assertContains("createTable('router", $output, 'Table router found');
70 $this->assertNotContains("insert('router", $output, 'Insert not found');
71 $this->assertNotContains("'name' => 'test", $output, 'Insert name field not found');
72 $this->assertNotContains("'path' => 'test", $output, 'Insert path field not found');
73 $this->assertNotContains("'pattern_outline' => 'test", $output, 'Insert pattern_outline field not found');
75 // Assert that insert statement doesn't exist for wildcard schema only match.
76 $command_tester->execute(['--schema-only' => 'route.*']);
77 $output = $command_tester->getDisplay();
78 $this->assertContains("createTable('router", $output, 'Table router found');
79 $this->assertNotContains("insert('router", $output, 'Insert not found');
80 $this->assertNotContains("'name' => 'test", $output, 'Insert name field not found');
81 $this->assertNotContains("'path' => 'test", $output, 'Insert path field not found');
82 $this->assertNotContains("'pattern_outline' => 'test", $output, 'Insert pattern_outline field not found');