Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

TestInitDbCommand.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Command;
8
9 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Exception;
13
14 class TestInitDbCommand extends ContainerAwareCommand
15 {
16 protected function configure()
17 {
18 $this
19 ->setName('ezpublish:test:init_db')
20 ->setHidden(true)
21 ->setDescription('Inits the configured database for test use based on existing fixtures for eZ Demo install (4.7 atm)')
22 ->setHelp(
23 <<<EOT
24 The command <info>%command.name%</info> initializes current configured database with existing fixture data.
25
26 WARNING: This command will delete all data in the configured database before filling it with fixture data.
27 EOT
28 );
29 }
30
31 protected function execute(InputInterface $input, OutputInterface $output)
32 {
33 $database = $this->getContainer()->get('ezpublish.connection')->getConnection()->getParams();
34 if (is_array($database)) {
35 $driverMap = [
36 'pdo_mysql' => 'mysql',
37 'pdo_pgsql' => 'pgsql',
38 'pdo_sqlite' => 'sqlite',
39 ];
40
41 $dbType = $driverMap[$database['driver']];
42 $database = $database['dbname'];
43 } else {
44 $dbType = preg_replace('(^([a-z]+).*)', '\\1', $database);
45 }
46
47 if (
48 $input->isInteractive() &&
49 !$this->getHelperSet()->get('dialog')->askConfirmation(
50 $output,
51 "<question>Are you sure you want to delete all data in '{$database}' database?</question>",
52 false
53 )
54 ) {
55 return;
56 }
57
58 $output->writeln('<info>Database is now being emptied and re filled with fixture data.</info>');
59
60 // @TODO Reuse API Integration tests SetupFactory when it has been refactored to be able to use Symfony DIC
61 $this->applyStatements($this->getSchemaStatements($dbType));
62 $this->insertData($dbType);
63 }
64
70 public function insertData($dbType)
71 {
72 // Get Initial fixture data and union with some tables that must be present but sometimes aren't
73 $data = $this->getInitialData() + [
74 'ezcontentobject_trash' => [],
75 'ezurlwildcard' => [],
76 'ezmedia' => [],
77 'ezkeyword' => [],
78 ];
79 $handler = $this->getDatabaseHandler();
80 foreach ($data as $table => $rows) {
81 // Cleanup before inserting
82 $deleteQuery = $handler->createDeleteQuery();
83 $deleteQuery->deleteFrom($handler->quoteIdentifier($table));
84 $stmt = $deleteQuery->prepare();
85 $stmt->execute();
86
87 // Check that at least one row exists
88 if (!isset($rows[0])) {
89 continue;
90 }
91
92 $q = $handler->createInsertQuery();
93 $q->insertInto($handler->quoteIdentifier($table));
94
95 // Contains the bound parameters
96 $values = [];
97
98 // Binding the parameters
99 foreach ($rows[0] as $col => $val) {
100 $q->set(
101 $handler->quoteIdentifier($col),
102 $q->bindParam($values[$col])
103 );
104 }
105
106 $stmt = $q->prepare();
107
108 foreach ($rows as $row) {
109 try {
110 // This CANNOT be replaced by:
111 // $values = $row
112 // each $values[$col] is a PHP reference which should be
113 // kept for parameters binding to work
114 foreach ($row as $col => $val) {
115 $values[$col] = $val;
116 }
117
118 $stmt->execute();
119 } catch (Exception $e) {
120 echo "$table ( ", implode(', ', $row), " )\n";
121 throw $e;
122 }
123 }
124 }
125
126 $this->applyStatements($this->getPostInsertStatements($dbType));
127 }
128
136 protected function getPostInsertStatements($dbType)
137 {
138 if ($dbType === 'pgsql') {
139 $setvalPath = __DIR__ . '/../../../Publish/Core/Persistence/Legacy/Tests/_fixtures/setval.pgsql.sql';
140
141 return array_filter(preg_split('(;\\s*$)m', file_get_contents($setvalPath)));
142 }
143
144 return [];
145 }
146
152 protected function getInitialData()
153 {
154 return include __DIR__ . '/../../../../data/demo_data.php';
155 }
156
162 protected function applyStatements(array $statements)
163 {
164 $handler = $this->getDatabaseHandler();
165 foreach ($statements as $statement) {
166 $handler->exec($statement);
167 }
168 }
169
177 protected function getSchemaStatements($dbType)
178 {
179 $schemaPath = __DIR__ . "/../../../../data/{$dbType}/schema.sql";
180
181 return array_filter(preg_split('(;\\s*$)m', file_get_contents($schemaPath)));
182 }
183
189 protected function getDatabaseHandler()
190 {
191 return $this->getContainer()->get('ezpublish.api.storage_engine.legacy.dbhandler');
192 }
193 }
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand
Definition: TestInitDbCommand.php:15
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\getInitialData
getInitialData()
Returns the initial database data.
Definition: TestInitDbCommand.php:152
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\getDatabaseHandler
getDatabaseHandler()
Returns the database handler from the service container.
Definition: TestInitDbCommand.php:189
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\configure
configure()
Definition: TestInitDbCommand.php:16
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\getSchemaStatements
getSchemaStatements($dbType)
Returns the database schema as an array of SQL statements.
Definition: TestInitDbCommand.php:177
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\insertData
insertData($dbType)
Insert the database data.
Definition: TestInitDbCommand.php:70
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\execute
execute(InputInterface $input, OutputInterface $output)
Definition: TestInitDbCommand.php:31
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\getPostInsertStatements
getPostInsertStatements($dbType)
Returns statements to be executed after data insert.
Definition: TestInitDbCommand.php:136
eZ\Bundle\EzPublishCoreBundle\Command\TestInitDbCommand\applyStatements
applyStatements(array $statements)
Applies the given SQL $statements to the database in use.
Definition: TestInitDbCommand.php:162
eZ\Bundle\EzPublishCoreBundle\Command
Definition: CheckURLsCommand.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.