diff --git a/app/config/services.yaml b/app/config/services.yaml index 1983bc82..d2ec9a00 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -69,6 +69,9 @@ services: arguments: - !tagged_iterator config.compiler_processor + config.compiler.dump_output: + class: 'Smile\GdprDump\Config\Compiler\Processor\DumpOutputProcessor' + config.compiler.processor.env_var: class: 'Smile\GdprDump\Config\Compiler\Processor\EnvVarProcessor' diff --git a/src/Config/Compiler/Processor/DumpOutputProcessor.php b/src/Config/Compiler/Processor/DumpOutputProcessor.php new file mode 100644 index 00000000..f16f4e39 --- /dev/null +++ b/src/Config/Compiler/Processor/DumpOutputProcessor.php @@ -0,0 +1,28 @@ +get('dump', []); + + if (array_key_exists('output', $dumpSettings)) { + $dumpSettings['output'] = preg_replace_callback( + '/{([^}]+)}/', + fn (array $matches) => date($matches[1]), + $dumpSettings['output'] + ); + + $config->set('dump', $dumpSettings); + } + } +} diff --git a/src/Config/Compiler/Processor/EnvVarProcessor.php b/src/Config/Compiler/Processor/EnvVarProcessor.php index 793b77ed..eea25fff 100644 --- a/src/Config/Compiler/Processor/EnvVarProcessor.php +++ b/src/Config/Compiler/Processor/EnvVarProcessor.php @@ -26,7 +26,7 @@ class EnvVarProcessor implements ProcessorInterface ]; /** - * @inheritdoc + * Replace environment variable placeholders (e.g. "%env(DB_HOST)%") */ public function process(ConfigInterface $config): void { diff --git a/src/Config/Compiler/Processor/VersionProcessor.php b/src/Config/Compiler/Processor/VersionProcessor.php index 627986e7..79131e2c 100644 --- a/src/Config/Compiler/Processor/VersionProcessor.php +++ b/src/Config/Compiler/Processor/VersionProcessor.php @@ -11,7 +11,7 @@ class VersionProcessor implements ProcessorInterface { /** - * @inheritdoc + * Process the "if_version" parameter. */ public function process(ConfigInterface $config): void { diff --git a/src/Database/TableDependencyResolver.php b/src/Database/TableDependencyResolver.php index 5a63c8ca..f802f008 100644 --- a/src/Database/TableDependencyResolver.php +++ b/src/Database/TableDependencyResolver.php @@ -120,6 +120,10 @@ private function buildDependencyTree(): void */ private function isForeignKeyIgnored(ForeignKey $foreignKey): bool { - return in_array($foreignKey->getConstraintName(), $this->config->getIgnoredForeignKeys(), true); + return in_array( + $foreignKey->getConstraintName(), + $this->config->getFilterPropagationSettings()->getIgnoredForeignKeys(), + true + ); } } diff --git a/src/Dumper/Config/Definition/FakerSettings.php b/src/Dumper/Config/Definition/FakerSettings.php new file mode 100644 index 00000000..e4230f45 --- /dev/null +++ b/src/Dumper/Config/Definition/FakerSettings.php @@ -0,0 +1,20 @@ +locale; + } +} diff --git a/src/Dumper/Config/Definition/FilterPropagationSettings.php b/src/Dumper/Config/Definition/FilterPropagationSettings.php new file mode 100644 index 00000000..4b3bc379 --- /dev/null +++ b/src/Dumper/Config/Definition/FilterPropagationSettings.php @@ -0,0 +1,28 @@ +enabled; + } + + /** + * Get foreign keys to ignore when propagating filters to table dependencies. + */ + public function getIgnoredForeignKeys(): array + { + return $this->ignoredForeignKeys; + } +} diff --git a/src/Dumper/Config/Table/Filter/Filter.php b/src/Dumper/Config/Definition/Table/Filter.php similarity index 97% rename from src/Dumper/Config/Table/Filter/Filter.php rename to src/Dumper/Config/Definition/Table/Filter.php index e1b138d7..1dd53127 100644 --- a/src/Dumper/Config/Table/Filter/Filter.php +++ b/src/Dumper/Config/Definition/Table/Filter.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Smile\GdprDump\Dumper\Config\Table\Filter; +namespace Smile\GdprDump\Dumper\Config\Definition\Table; use UnexpectedValueException; diff --git a/src/Dumper/Config/Table/Filter/SortOrder.php b/src/Dumper/Config/Definition/Table/SortOrder.php similarity index 94% rename from src/Dumper/Config/Table/Filter/SortOrder.php rename to src/Dumper/Config/Definition/Table/SortOrder.php index 1f14e5a2..cefe9e81 100644 --- a/src/Dumper/Config/Table/Filter/SortOrder.php +++ b/src/Dumper/Config/Definition/Table/SortOrder.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Smile\GdprDump\Dumper\Config\Table\Filter; +namespace Smile\GdprDump\Dumper\Config\Definition\Table; use UnexpectedValueException; diff --git a/src/Dumper/Config/Table/TableConfig.php b/src/Dumper/Config/Definition/TableConfig.php similarity index 93% rename from src/Dumper/Config/Table/TableConfig.php rename to src/Dumper/Config/Definition/TableConfig.php index 8e77a99e..5a99b395 100644 --- a/src/Dumper/Config/Table/TableConfig.php +++ b/src/Dumper/Config/Definition/TableConfig.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Smile\GdprDump\Dumper\Config\Table; +namespace Smile\GdprDump\Dumper\Config\Definition; -use Smile\GdprDump\Dumper\Config\Table\Filter\Filter; -use Smile\GdprDump\Dumper\Config\Table\Filter\SortOrder; +use Smile\GdprDump\Dumper\Config\Definition\Table\Filter; +use Smile\GdprDump\Dumper\Config\Definition\Table\SortOrder; +use Smile\GdprDump\Dumper\Config\Validation\ValidationException; use Smile\GdprDump\Dumper\Config\Validation\WhereExprValidator; use UnexpectedValueException; class TableConfig { - private WhereExprValidator $whereExprValidator; private string $name; private ?string $where = null; private ?int $limit = null; @@ -31,7 +31,6 @@ class TableConfig public function __construct(string $tableName, array $tableConfig) { - $this->whereExprValidator = new WhereExprValidator(); $this->name = $tableName; $this->prepareConfig($tableConfig); } @@ -145,6 +144,8 @@ private function prepareConfig(array $tableData): void /** * Prepare the table filters. + * + * @throws ValidationException */ private function prepareFilters(array $tableData): void { @@ -158,7 +159,8 @@ private function prepareFilters(array $tableData): void // New way of declaring table filters (`where` parameter) $whereCondition = (string) ($tableData['where'] ?? ''); if ($whereCondition !== '') { - $this->whereExprValidator->validate($whereCondition); + $whereExprValidator = new WhereExprValidator(); + $whereExprValidator->validate($whereCondition); $this->where = $whereCondition; } } diff --git a/src/Dumper/Config/Definition/TableConfigCollection.php b/src/Dumper/Config/Definition/TableConfigCollection.php new file mode 100644 index 00000000..c4c36f9d --- /dev/null +++ b/src/Dumper/Config/Definition/TableConfigCollection.php @@ -0,0 +1,67 @@ + + */ +class TableConfigCollection implements IteratorAggregate +{ + /** + * @var TableConfig[] + */ + private array $items = []; + + /**+ + * Add an item. + */ + public function add(TableConfig $tableConfig): void + { + $this->items[$tableConfig->getName()] = $tableConfig; + } + + /** + * Get an item. + * + * @throws UnexpectedValueException + */ + public function get(string $name): TableConfig + { + return $this->has($name) + ? $this->items[$name] + : throw new UnexpectedValueException(sprintf('The table "%s" is not defined.', $name)); + } + + /** + * Check whether an item exists. + */ + public function has(string $name): bool + { + return array_key_exists($name, $this->items); + } + + /** + * Get all items. + * + * @return TableConfig[] + */ + public function all(): array + { + return $this->items; + } + + /** + * @return ArrayIterator + */ + public function getIterator(): Traversable + { + return new ArrayIterator($this->items); + } +} diff --git a/src/Dumper/Config/DumperConfig.php b/src/Dumper/Config/DumperConfig.php index 42de2d94..c7a8bca0 100644 --- a/src/Dumper/Config/DumperConfig.php +++ b/src/Dumper/Config/DumperConfig.php @@ -7,50 +7,18 @@ use Druidfi\Mysqldump\Compress\CompressManagerFactory; use Druidfi\Mysqldump\DumpSettings; use Smile\GdprDump\Config\ConfigInterface; -use Smile\GdprDump\Dumper\Config\Table\TableConfig; +use Smile\GdprDump\Dumper\Config\Definition\FakerSettings; +use Smile\GdprDump\Dumper\Config\Definition\FilterPropagationSettings; +use Smile\GdprDump\Dumper\Config\Definition\TableConfig; +use Smile\GdprDump\Dumper\Config\Definition\TableConfigCollection; use Smile\GdprDump\Dumper\Config\Validation\QueryValidator; -use UnexpectedValueException; +use Smile\GdprDump\Dumper\Config\Validation\ValidationException; class DumperConfig { - private QueryValidator $selectQueryValidator; - private QueryValidator $initCommandQueryValidator; - - /** - * @var TableConfig[] - */ - private array $tablesConfig = []; - - /** - * @var string[] - */ - private array $varQueries = []; - - /** - * @var string[] - */ - private array $tablesWhitelist = []; - - /** - * @var string[] - */ - private array $tablesBlacklist = []; - - /** - * @var string[] - */ - private array $tablesToTruncate = []; - - /** - * @var string[] - */ - private array $tablesToFilter = []; - - /** - * @var string[] - */ - private array $tablesToSort = []; - + private FakerSettings $fakerSettings; + private FilterPropagationSettings $filterPropagationSettings; + private TableConfigCollection $tablesConfig; private array $dumpSettings = [ 'output' => 'php://stdout', 'add_drop_database' => false, @@ -79,81 +47,86 @@ class DumperConfig 'skip_tz_utc' => false, ]; - private array $filterPropagationSettings = [ - 'enabled' => true, - 'ignored_foreign_keys' => [], - ]; + /** + * @var string[] + */ + private array $tablesWhitelist = []; - private array $fakerSettings = [ - 'locale' => null, - ]; + /** + * @var string[] + */ + private array $tablesBlacklist = []; /** - * @throws UnexpectedValueException + * @var string[] */ - public function __construct(ConfigInterface $config) - { - $this->selectQueryValidator = new QueryValidator(['select']); - $this->initCommandQueryValidator = new QueryValidator(['set']); - $this->prepareConfig($config); - } + private array $varQueries = []; /** - * Get the dump output. + * @var string[] */ - public function getDumpOutput(): string - { - return $this->dumpSettings['output']; - } + private array $tablesToTruncate = []; /** - * Get dump settings. + * @var string[] */ - public function getDumpSettings(): array + private array $tablesToFilter = []; + + /** + * @var string[] + */ + private array $tablesToSort = []; + + /** + * @throws ValidationException + */ + public function __construct(ConfigInterface $config) { - return $this->dumpSettings; + $this->prepareVarQueries($config); + $this->prepareDumpSettings($config); + $this->prepareFakerSettings($config); + $this->prepareFilterPropagationSettings($config); + $this->prepareTableSettings($config); } /** - * Check whether to apply table filters recursively to table dependencies (by following foreign keys). + * Get the dump output. */ - public function isFilterPropagationEnabled(): bool + public function getDumpOutput(): string { - return $this->filterPropagationSettings['enabled']; + return $this->getDumpSettings()['output']; } /** - * Get the foreign keys to exclude from the table filter propagation. + * Get dump settings. */ - public function getIgnoredForeignKeys(): array + public function getDumpSettings(): array { - return $this->filterPropagationSettings['ignored_foreign_keys']; + return $this->dumpSettings; } /** * Get faker settings. */ - public function getFakerSettings(): array + public function getFakerSettings(): FakerSettings { return $this->fakerSettings; } /** - * Get the tables configuration (filters, orders, limits). - * - * @return TableConfig[] + * Get filter propagation settings. */ - public function getTablesConfig(): array + public function getFilterPropagationSettings(): FilterPropagationSettings { - return $this->tablesConfig; + return $this->filterPropagationSettings; } /** - * Get the configuration of a table. + * Get the tables configuration (filters, orders, limits). */ - public function getTableConfig(string $tableName): ?TableConfig + public function getTablesConfig(): TableConfigCollection { - return $this->tablesConfig[$tableName] ?? null; + return $this->tablesConfig; } /** @@ -220,38 +193,25 @@ public function getTablesToSort(): array } /** - * Prepare the config. + * Prepare SQL variable queries. * - * @throws UnexpectedValueException + * @throws ValidationException */ - private function prepareConfig(ConfigInterface $config): void + private function prepareVarQueries(ConfigInterface $config): void { - // Dump settings - $this->prepareDumpSettings($config); - - // Filter propagation settings - $this->prepareFilterPropagationSettings($config); - - // Faker settings - $this->prepareFakerSettings($config); - - // Tables config - $this->prepareTablesConfig($config); - - // Queries to run - $this->prepareVarQueries($config); - - // Tables whitelist - $this->prepareTablesWhitelist($config); + $this->varQueries = $config->get('variables', []); - // Tables blacklist - $this->prepareTablesBlacklist($config); + // Allow only "select" statements in queries + $selectQueryValidator = new QueryValidator(['select']); + foreach ($this->varQueries as $query) { + $selectQueryValidator->validate($query); + } } /** * Prepare dump settings. * - * @throws UnexpectedValueException + * @throws ValidationException */ private function prepareDumpSettings(ConfigInterface $config): void { @@ -259,75 +219,53 @@ private function prepareDumpSettings(ConfigInterface $config): void foreach ($settings as $param => $value) { if (!array_key_exists($param, $this->dumpSettings)) { - throw new UnexpectedValueException(sprintf('Invalid dump setting "%s".', $param)); + throw new ValidationException(sprintf('Invalid dump setting "%s".', $param)); } $this->dumpSettings[$param] = $value; } - // Validate init_commands + // Allow only "set" statements in init commands + $initCommandQueryValidator = new QueryValidator(['set']); foreach ($this->dumpSettings['init_commands'] as $query) { - $this->initCommandQueryValidator->validate($query); + $initCommandQueryValidator->validate($query); } - - // Replace {...} by the current date in dump output - $this->dumpSettings['output'] = preg_replace_callback( - '/{([^}]+)}/', - fn (array $matches) => date($matches[1]), - $this->dumpSettings['output'] - ); } /** - * Prepare table filter propagation settings. - * - * @throws UnexpectedValueException + * Prepare faker settings. */ - private function prepareFilterPropagationSettings(ConfigInterface $config): void + private function prepareFakerSettings(ConfigInterface $config): void { - $settings = $config->get('filter_propagation', []); - - foreach ($settings as $param => $value) { - if (!array_key_exists($param, $this->filterPropagationSettings)) { - throw new UnexpectedValueException(sprintf('Invalid filter propagation setting "%s".', $param)); - } - - $this->filterPropagationSettings[$param] = $value; - } + $settings = $config->get('faker', []); + $this->fakerSettings = new FakerSettings((string) ($settings['locale'] ?? '')); } /** - * Prepare faker settings. - * - * @throws UnexpectedValueException + * Prepare filter propagation settings. */ - private function prepareFakerSettings(ConfigInterface $config): void + private function prepareFilterPropagationSettings(ConfigInterface $config): void { - $settings = $config->get('faker', []); - - foreach ($settings as $param => $value) { - if (!array_key_exists($param, $this->fakerSettings)) { - throw new UnexpectedValueException(sprintf('Invalid faker setting "%s".', $param)); - } + $settings = $config->get('filter_propagation', []); - $this->fakerSettings[$param] = $value; - } + $this->filterPropagationSettings = new FilterPropagationSettings( + $settings['enabled'] ?? true, + $settings['ignored_foreign_keys'] ?? [] + ); } /** - * Prepare the tables configuration. - * - * @throws UnexpectedValueException + * Prepare table settings. */ - private function prepareTablesConfig(ConfigInterface $config): void + private function prepareTableSettings(ConfigInterface $config): void { - $tablesData = $config->get('tables', []); - - foreach ($tablesData as $tableName => $tableData) { - $tableName = (string) $tableName; + $this->tablesWhitelist = $config->get('tables_whitelist', []); + $this->tablesBlacklist = $config->get('tables_blacklist', []); + $this->tablesConfig = new TableConfigCollection(); - $tableConfig = new TableConfig($tableName, $tableData); - $this->tablesConfig[$tableName] = $tableConfig; + foreach ($config->get('tables', []) as $tableName => $tableData) { + $tableConfig = new TableConfig((string) $tableName, $tableData); + $this->tablesConfig->add($tableConfig); if ($tableConfig->getLimit() === 0) { $this->tablesToTruncate[] = $tableConfig->getName(); @@ -342,41 +280,4 @@ private function prepareTablesConfig(ConfigInterface $config): void } } } - - /** - * Prepare the SQL queries to run. - */ - private function prepareVarQueries(ConfigInterface $config): void - { - $this->varQueries = $config->get('variables', []); - - foreach ($this->varQueries as $index => $query) { - $this->selectQueryValidator->validate($query); - $this->varQueries[$index] = (string) $query; - } - } - - /** - * Prepare the tables whitelist. - */ - private function prepareTablesWhitelist(ConfigInterface $config): void - { - $this->tablesWhitelist = $config->get('tables_whitelist', []); - - foreach ($this->tablesWhitelist as $index => $tableName) { - $this->tablesWhitelist[$index] = (string) $tableName; - } - } - - /** - * Prepare the tables blacklist. - */ - private function prepareTablesBlacklist(ConfigInterface $config): void - { - $this->tablesBlacklist = $config->get('tables_blacklist', []); - - foreach ($this->tablesBlacklist as $index => $tableName) { - $this->tablesBlacklist[$index] = (string) $tableName; - } - } } diff --git a/src/Dumper/Listener/DataConverterListener.php b/src/Dumper/Listener/DataConverterListener.php index 1eee5eed..2be2919b 100644 --- a/src/Dumper/Listener/DataConverterListener.php +++ b/src/Dumper/Listener/DataConverterListener.php @@ -95,8 +95,8 @@ private function buildConverters(DumperConfig $config): void foreach ($config->getTablesConfig() as $tableName => $tableConfig) { // Build data converters - foreach ($tableConfig->getConverters() as $columnName => $definition) { - $this->converters[$tableName][$columnName] = $this->converterBuilder->build($definition); + foreach ($tableConfig->getConverters() as $columnName => $converterDefinition) { + $this->converters[$tableName][$columnName] = $this->converterBuilder->build($converterDefinition); } // Build conversion skip conditions diff --git a/src/Dumper/Listener/FakerLocaleListener.php b/src/Dumper/Listener/FakerLocaleListener.php index 0ba36351..81da3fc2 100644 --- a/src/Dumper/Listener/FakerLocaleListener.php +++ b/src/Dumper/Listener/FakerLocaleListener.php @@ -18,7 +18,7 @@ public function __construct(private FakerService $fakerService) */ public function __invoke(DumpEvent $event): void { - $locale = (string) ($event->getConfig()->getFakerSettings()['locale'] ?? ''); + $locale = $event->getConfig()->getFakerSettings()->getLocale(); if ($locale !== '') { $this->fakerService->setLocale($locale); } diff --git a/src/Dumper/Listener/TableFilterListener.php b/src/Dumper/Listener/TableFilterListener.php index 147c7259..5b280735 100644 --- a/src/Dumper/Listener/TableFilterListener.php +++ b/src/Dumper/Listener/TableFilterListener.php @@ -10,9 +10,9 @@ use Smile\GdprDump\Database\Metadata\Definition\Constraint\ForeignKey; use Smile\GdprDump\Database\Metadata\MetadataInterface; use Smile\GdprDump\Database\TableDependencyResolver; +use Smile\GdprDump\Dumper\Config\Definition\Table\Filter; +use Smile\GdprDump\Dumper\Config\Definition\TableConfig; use Smile\GdprDump\Dumper\Config\DumperConfig; -use Smile\GdprDump\Dumper\Config\Table\Filter\Filter; -use Smile\GdprDump\Dumper\Config\Table\TableConfig; use Smile\GdprDump\Dumper\Event\DumpEvent; use UnexpectedValueException; @@ -53,7 +53,7 @@ private function buildTablesWhere(): array // If recursive filters are enabled, tables to query must contain // all tables that depend on the tables that have filters/sort order - if ($this->config->isFilterPropagationEnabled()) { + if ($this->config->getFilterPropagationSettings()->isEnabled()) { $dependencyResolver = new TableDependencyResolver($this->metadata, $this->config); $dependencies = $dependencyResolver->getDependencies($tablesToFilter); } @@ -69,7 +69,7 @@ private function buildTablesWhere(): array // Add where conditions on the parent tables that also have active filters if ( - $this->config->isFilterPropagationEnabled() + $this->config->getFilterPropagationSettings()->isEnabled() && $queryBuilder->getMaxResults() !== 0 && array_key_exists($tableName, $dependencies) ) { @@ -176,8 +176,10 @@ private function createQueryBuilder(string $tableName): QueryBuilder $queryBuilder->select('*') ->from($this->connection->quoteIdentifier($tableName)); - $tableConfig = $this->config->getTableConfig($tableName); - if ($tableConfig === null) { + try { + $tableConfig = $this->config->getTablesConfig()->get($tableName); + } catch (UnexpectedValueException) { + // Table doesn't exist, skip return $queryBuilder; } diff --git a/tests/functional/Dumper/Config/ConfigProcessorTest.php b/tests/functional/Dumper/Config/ConfigProcessorTest.php index 23a01b25..a76d13f4 100644 --- a/tests/functional/Dumper/Config/ConfigProcessorTest.php +++ b/tests/functional/Dumper/Config/ConfigProcessorTest.php @@ -31,9 +31,11 @@ public function testTableNameResolution(): void // Check if the table names were resolved $this->assertSame(['customers'], $config->getTablesWhitelist()); $this->assertSame(['stores'], $config->getTablesBlacklist()); - $this->assertArrayHasKey('customers', $config->getTablesConfig()); - $this->assertArrayNotHasKey('cust*', $config->getTablesConfig()); - $this->assertArrayNotHasKey('notExist*', $config->getTablesConfig()); + + $tablesConfig = $config->getTablesConfig()->all(); + $this->assertArrayHasKey('customers', $tablesConfig); + $this->assertArrayNotHasKey('cust*', $tablesConfig); + $this->assertArrayNotHasKey('notExist*', $tablesConfig); } /** @@ -48,7 +50,7 @@ public function testWithEmptyConfig(): void $config = $processor->process(new Config([])); $this->assertEmpty($config->getTablesBlacklist()); $this->assertEmpty($config->getTablesWhitelist()); - $this->assertEmpty($config->getTablesConfig()); + $this->assertEmpty($config->getTablesConfig()->all()); } /** diff --git a/tests/functional/Dumper/MysqlDumperTest.php b/tests/functional/Dumper/MysqlDumperTest.php index 046e103d..8d42985f 100644 --- a/tests/functional/Dumper/MysqlDumperTest.php +++ b/tests/functional/Dumper/MysqlDumperTest.php @@ -48,6 +48,7 @@ public function testDumper(): void // Same tests but with filter propagation disabled $config = $this->createConfig(); $config->set('filter_propagation', ['enabled' => false]); + $dumper = $this->createDumper(); $dumper->dump($config); $this->assertDumpIsValid(false); diff --git a/tests/unit/Database/TableDependencyResolverTest.php b/tests/unit/Database/TableDependencyResolverTest.php index 04200453..71fb2db5 100644 --- a/tests/unit/Database/TableDependencyResolverTest.php +++ b/tests/unit/Database/TableDependencyResolverTest.php @@ -7,6 +7,7 @@ use Smile\GdprDump\Database\Metadata\Definition\Constraint\ForeignKey; use Smile\GdprDump\Database\Metadata\MysqlMetadata; use Smile\GdprDump\Database\TableDependencyResolver; +use Smile\GdprDump\Dumper\Config\Definition\FilterPropagationSettings; use Smile\GdprDump\Dumper\Config\DumperConfig; use Smile\GdprDump\Tests\Unit\TestCase; @@ -181,8 +182,8 @@ private function createResolver(array $foreignKeyMap, array $ignoredForeignKeys ->willReturnMap($foreignKeyMap); $configMock = $this->createMock(DumperConfig::class); - $configMock->method('getIgnoredForeignKeys') - ->willReturn($ignoredForeignKeys); + $configMock->method('getFilterPropagationSettings') + ->willReturn(new FilterPropagationSettings(true, $ignoredForeignKeys)); return new TableDependencyResolver($metadataMock, $configMock); } diff --git a/tests/unit/Dumper/Config/ConfigProcessorTest.php b/tests/unit/Dumper/Config/ConfigProcessorTest.php index 4792877d..c46d31ea 100644 --- a/tests/unit/Dumper/Config/ConfigProcessorTest.php +++ b/tests/unit/Dumper/Config/ConfigProcessorTest.php @@ -28,7 +28,7 @@ public function testProcessor(): void $this->assertSame(['table1'], $config->getTablesBlacklist()); $this->assertSame(['table2'], $config->getTablesWhitelist()); - $this->assertSame(['table3'], array_keys($config->getTablesConfig())); + $this->assertSame(['table3'], array_keys($config->getTablesConfig()->all())); } /** @@ -48,7 +48,7 @@ public function testProcessorWithWildCard(): void $this->assertSame(['table1', 'table2', 'table3'], $config->getTablesBlacklist()); $this->assertSame(['table1', 'table2', 'table3'], $config->getTablesWhitelist()); - $this->assertSame(['table1', 'table2', 'table3'], array_keys($config->getTablesConfig())); + $this->assertSame(['table1', 'table2', 'table3'], array_keys($config->getTablesConfig()->all())); } /** @@ -62,7 +62,7 @@ public function testProcessorWithEmptyConfig(): void $this->assertSame([], $config->getTablesBlacklist()); $this->assertSame([], $config->getTablesWhitelist()); - $this->assertSame([], $config->getTablesConfig()); + $this->assertSame([], $config->getTablesConfig()->all()); } /** diff --git a/tests/unit/Dumper/Config/Definition/FakerSettingsTest.php b/tests/unit/Dumper/Config/Definition/FakerSettingsTest.php new file mode 100644 index 00000000..8159ad56 --- /dev/null +++ b/tests/unit/Dumper/Config/Definition/FakerSettingsTest.php @@ -0,0 +1,20 @@ +assertSame('en_US', $settings->getLocale()); + } +} diff --git a/tests/unit/Dumper/Config/Definition/FilterPropagationSettingsTest.php b/tests/unit/Dumper/Config/Definition/FilterPropagationSettingsTest.php new file mode 100644 index 00000000..2ff34169 --- /dev/null +++ b/tests/unit/Dumper/Config/Definition/FilterPropagationSettingsTest.php @@ -0,0 +1,25 @@ +assertTrue($settings->isEnabled()); + $this->assertSame(['fk1'], $settings->getIgnoredForeignKeys()); + + $settings = new FilterPropagationSettings(false, []); + $this->assertFalse($settings->isEnabled()); + $this->assertSame([], $settings->getIgnoredForeignKeys()); + } +} diff --git a/tests/unit/Dumper/Config/Table/Filter/FilterTest.php b/tests/unit/Dumper/Config/Definition/Table/FilterTest.php similarity index 92% rename from tests/unit/Dumper/Config/Table/Filter/FilterTest.php rename to tests/unit/Dumper/Config/Definition/Table/FilterTest.php index b3996070..1f9e7dc6 100644 --- a/tests/unit/Dumper/Config/Table/Filter/FilterTest.php +++ b/tests/unit/Dumper/Config/Definition/Table/FilterTest.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Table\Filter; +namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Definition\Table; -use Smile\GdprDump\Dumper\Config\Table\Filter\Filter; +use Smile\GdprDump\Dumper\Config\Definition\Table\Filter; use Smile\GdprDump\Tests\Unit\TestCase; use UnexpectedValueException; diff --git a/tests/unit/Dumper/Config/Table/Filter/SortOrderTest.php b/tests/unit/Dumper/Config/Definition/Table/SortOrderTest.php similarity index 87% rename from tests/unit/Dumper/Config/Table/Filter/SortOrderTest.php rename to tests/unit/Dumper/Config/Definition/Table/SortOrderTest.php index 9a4f23b6..5470f42d 100644 --- a/tests/unit/Dumper/Config/Table/Filter/SortOrderTest.php +++ b/tests/unit/Dumper/Config/Definition/Table/SortOrderTest.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Table\Filter; +namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Definition\Table; -use Smile\GdprDump\Dumper\Config\Table\Filter\SortOrder; +use Smile\GdprDump\Dumper\Config\Definition\Table\SortOrder; use Smile\GdprDump\Tests\Unit\TestCase; use UnexpectedValueException; diff --git a/tests/unit/Dumper/Config/Table/TableConfigTest.php b/tests/unit/Dumper/Config/Definition/TableConfigTest.php similarity index 96% rename from tests/unit/Dumper/Config/Table/TableConfigTest.php rename to tests/unit/Dumper/Config/Definition/TableConfigTest.php index 7abde5ca..748712d7 100644 --- a/tests/unit/Dumper/Config/Table/TableConfigTest.php +++ b/tests/unit/Dumper/Config/Definition/TableConfigTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Table; +namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Definition; -use Smile\GdprDump\Dumper\Config\Table\Filter\Filter; -use Smile\GdprDump\Dumper\Config\Table\TableConfig; +use Smile\GdprDump\Dumper\Config\Definition\Table\Filter; +use Smile\GdprDump\Dumper\Config\Definition\TableConfig; use Smile\GdprDump\Dumper\Config\Validation\ValidationException; use Smile\GdprDump\Tests\Unit\TestCase; use UnexpectedValueException; diff --git a/tests/unit/Dumper/Config/DumperConfigTest.php b/tests/unit/Dumper/Config/DumperConfigTest.php index 4d2d87bd..dd21cf24 100644 --- a/tests/unit/Dumper/Config/DumperConfigTest.php +++ b/tests/unit/Dumper/Config/DumperConfigTest.php @@ -6,10 +6,8 @@ use Smile\GdprDump\Config\Config; use Smile\GdprDump\Dumper\Config\DumperConfig; -use Smile\GdprDump\Dumper\Config\Table\TableConfig; use Smile\GdprDump\Dumper\Config\Validation\ValidationException; use Smile\GdprDump\Tests\Unit\TestCase; -use UnexpectedValueException; class DumperConfigTest extends TestCase { @@ -52,8 +50,8 @@ public function testTablesData(): void $this->assertSame(['table1'], $config->getTablesToTruncate()); $this->assertSame(['table1', 'table2'], $config->getTablesToFilter()); $this->assertSame(['table3'], $config->getTablesToSort()); - $this->assertCount(4, $config->getTablesConfig()); - $this->assertInstanceOf(TableConfig::class, $config->getTableConfig('table1')); + $this->assertCount(4, $config->getTablesConfig()->all()); + $this->assertSame(['table1', 'table2', 'table3', 'table4'], array_keys($config->getTablesConfig()->all())); } /** @@ -86,8 +84,11 @@ public function testFilterPropagationSettings(): void $config = $this->createConfig($data); - $this->assertSame($data['filter_propagation']['enabled'], $config->isFilterPropagationEnabled()); - $this->assertSame($data['filter_propagation']['ignored_foreign_keys'], $config->getIgnoredForeignKeys()); + $this->assertSame($data['filter_propagation']['enabled'], $config->getFilterPropagationSettings()->isEnabled()); + $this->assertSame( + $data['filter_propagation']['ignored_foreign_keys'], + $config->getFilterPropagationSettings()->getIgnoredForeignKeys() + ); } /** @@ -96,10 +97,7 @@ public function testFilterPropagationSettings(): void public function testFakerSettings(): void { $config = $this->createConfig(['faker' => ['locale' => 'en_US']]); - - $settings = $config->getFakerSettings(); - $this->assertArrayHasKey('locale', $settings); - $this->assertSame('en_US', $settings['locale']); + $this->assertSame('en_US', $config->getFakerSettings()->getLocale()); } /** @@ -127,10 +125,10 @@ public function testDefaultValues(): void $this->assertSame([], $config->getTablesToSort()); $this->assertSame([], $config->getTablesToFilter()); $this->assertSame([], $config->getTablesToTruncate()); - $this->assertSame([], $config->getTablesConfig()); + $this->assertSame([], $config->getTablesConfig()->all()); $this->assertSame('php://stdout', $config->getDumpOutput()); - $this->assertTrue($config->isFilterPropagationEnabled()); - $this->assertSame([], $config->getIgnoredForeignKeys()); + $this->assertTrue($config->getFilterPropagationSettings()->isEnabled()); + $this->assertSame([], $config->getFilterPropagationSettings()->getIgnoredForeignKeys()); // Test these values because they differ from MySQLDump-PHP $settings = $config->getDumpSettings(); @@ -141,9 +139,7 @@ public function testDefaultValues(): void $this->assertArrayHasKey('lock_tables', $settings); $this->assertFalse($settings['lock_tables']); - $settings = $config->getFakerSettings(); - $this->assertArrayHasKey('locale', $settings); - $this->assertNull($settings['locale']); + $this->assertSame('', $config->getFakerSettings()->getLocale()); } /** @@ -151,7 +147,7 @@ public function testDefaultValues(): void */ public function testInvalidDumpParameter(): void { - $this->expectException(UnexpectedValueException::class); + $this->expectException(ValidationException::class); $this->createConfig(['dump' => ['not_exists' => true]]); }