diff --git a/docs/content/anonymization/core-anonymizers.md b/docs/content/anonymization/core-anonymizers.md index dbc34d57..383ca782 100644 --- a/docs/content/anonymization/core-anonymizers.md +++ b/docs/content/anonymization/core-anonymizers.md @@ -19,3 +19,4 @@ This page list all *Anonymizers* provided by *DbToolsBundle*. + diff --git a/docs/content/anonymization/core-anonymizers/file-resolution.md b/docs/content/anonymization/core-anonymizers/file-resolution.md new file mode 100644 index 00000000..ae21c508 --- /dev/null +++ b/docs/content/anonymization/core-anonymizers/file-resolution.md @@ -0,0 +1,39 @@ +## File name resolution + +In various places you can configure relative file names in order to load data, +here is how relative file names are resolved. +**All relative file names will be considered relative to a given _base path_.** + +The default base path is always stable but depends upon your selected flavor. + +@todo examples + +@@@ symfony + +When parsing Symfony configuration, base path will always be the project +directory, known as `%kernel.project_dir%` variable in Symfony configuration. +This is the directory where your `composer.json` file. + +@todo examples + +@@@ +@@@ laravel + +When parsing Laravel configuration, base path will always be the project +directory, as returned by the `base_path()` Laravel function. + +@todo examples + +@@@ +@@@ standalone docker + +When parsing configuration in the standalone CLI version or in docker context, +base path will be currently being parsed Yaml file. + +:::tip +If you set the `workdir` option in your configuration file, then it will +override the file directory and use it as the base path. + +@todo link to `workdir` documentation +::: +@@@ diff --git a/src/Anonymization/Anonymizer/Core/StringPatternAnonymizer.php b/src/Anonymization/Anonymizer/Core/StringPatternAnonymizer.php index 4882c6db..6c3ffabe 100644 --- a/src/Anonymization/Anonymizer/Core/StringPatternAnonymizer.php +++ b/src/Anonymization/Anonymizer/Core/StringPatternAnonymizer.php @@ -192,7 +192,13 @@ private function getAnonymizer(string $anonymizer, ?Options $options = null, int return $ret; } - $config = new AnonymizerConfig($this->tableName, $this->columnName, $anonymizer, new Options()); + $config = new AnonymizerConfig( + $this->tableName, + $this->columnName, + $anonymizer, + new Options(), + __DIR__, // @todo FIXME + ); return $this->childAnonymizers[$key] = $this ->getAnonymizerRegistry() @@ -200,7 +206,7 @@ private function getAnonymizer(string $anonymizer, ?Options $options = null, int $anonymizer, $config, $options ?? new Options(), - $this->databaseSession + $this->databaseSession, ) ; } diff --git a/src/Anonymization/Config/AnonymizerConfig.php b/src/Anonymization/Config/AnonymizerConfig.php index e3310ace..3e535aa6 100644 --- a/src/Anonymization/Config/AnonymizerConfig.php +++ b/src/Anonymization/Config/AnonymizerConfig.php @@ -13,5 +13,11 @@ public function __construct( public readonly string $targetName, public readonly string $anonymizer, public readonly Options $options, + /** + * Root directory in which this anonymizer configuration was in the + * first place. This allows implementations to use it in order, for + * example, to load files using a relative path. + */ + public readonly string $basePath, ) {} } diff --git a/src/Anonymization/Config/Loader/ArrayLoader.php b/src/Anonymization/Config/Loader/ArrayLoader.php index 7b1d495e..fe5202df 100644 --- a/src/Anonymization/Config/Loader/ArrayLoader.php +++ b/src/Anonymization/Config/Loader/ArrayLoader.php @@ -14,7 +14,12 @@ class ArrayLoader implements LoaderInterface { public function __construct( private array $data, - private string $connectionName = 'default', + private string $connectionName, + /** + * Root directory from which the configuration was loaded. It allows + * later file loading (for example, when sources are CSV or TXT files). + */ + private readonly string $basePath, ) {} #[\Override] @@ -51,6 +56,7 @@ public function load(AnonymizationConfig $config): void $target, $targetConfig['anonymizer'], new Options($targetConfig['options']), + $this->basePath, )); } } diff --git a/src/Anonymization/Config/Loader/AttributesLoader.php b/src/Anonymization/Config/Loader/AttributesLoader.php index 1f5ce070..377fb811 100644 --- a/src/Anonymization/Config/Loader/AttributesLoader.php +++ b/src/Anonymization/Config/Loader/AttributesLoader.php @@ -15,6 +15,11 @@ class AttributesLoader implements LoaderInterface { public function __construct( private EntityManagerProvider $entityManagerProvider, + /** + * Root directory from which the configuration was loaded. It allows + * later file loading (for example, when sources are CSV or TXT files). + */ + private readonly string $basePath, ) {} #[\Override] @@ -62,6 +67,7 @@ public function load(AnonymizationConfig $config): void $anonymization->type . '_' . $key, $anonymization->type, new Options($anonymization->options), + $this->basePath, )); } } @@ -78,6 +84,7 @@ public function load(AnonymizationConfig $config): void $metadata->getColumnName($fieldName), $propertyConfig->type, new Options($propertyConfig->options), + $this->basePath, )); } } @@ -109,6 +116,7 @@ public function load(AnonymizationConfig $config): void $columnName, $anonymization->type, new Options($anonymization->options), + $this->basePath, )); } } diff --git a/src/Anonymization/Config/Loader/PhpFileLoader.php b/src/Anonymization/Config/Loader/PhpFileLoader.php index 3b830cbb..025d8b1d 100644 --- a/src/Anonymization/Config/Loader/PhpFileLoader.php +++ b/src/Anonymization/Config/Loader/PhpFileLoader.php @@ -11,8 +11,20 @@ class PhpFileLoader extends ArrayLoader public function __construct( private string $file, string $connectionName = 'default', + /** + * Root directory from which the configuration was loaded. It allows + * later file loading (for example, when sources are CSV or TXT files). + * + * If not set, the file directory will be used instead. If set, it will + * override the file directory. + */ + ?string $basePath = null, ) { - parent::__construct([], $connectionName); + // @todo dirname() is purely soft logic, it does not imply any + // syscalls, whereas realpah() may. + $basePath ??= \realpath(\dirname($file)); + + parent::__construct([], $connectionName, $basePath); } #[\Override] diff --git a/src/Anonymization/Config/Loader/YamlLoader.php b/src/Anonymization/Config/Loader/YamlLoader.php index 4bf92519..5af31396 100644 --- a/src/Anonymization/Config/Loader/YamlLoader.php +++ b/src/Anonymization/Config/Loader/YamlLoader.php @@ -11,8 +11,20 @@ class YamlLoader extends ArrayLoader public function __construct( private string $file, string $connectionName = 'default', + /** + * Root directory from which the configuration was loaded. It allows + * later file loading (for example, when sources are CSV or TXT files). + * + * If not set, the file directory will be used instead. If set, it will + * override the file directory. + */ + ?string $basePath = null, ) { - parent::__construct([], $connectionName); + // @todo dirname() is purely soft logic, it does not imply any + // syscalls, whereas realpah() may. + $basePath ??= \realpath(\dirname($file)); + + parent::__construct([], $connectionName, $basePath); } #[\Override] diff --git a/src/Bridge/Laravel/DbToolsServiceProvider.php b/src/Bridge/Laravel/DbToolsServiceProvider.php index 75b71b65..f2c3e144 100644 --- a/src/Bridge/Laravel/DbToolsServiceProvider.php +++ b/src/Bridge/Laravel/DbToolsServiceProvider.php @@ -144,6 +144,8 @@ function (AnonymizatorFactory $factory, Application $app): void { /** @var Repository $config */ $config = $app->make('config'); + $workdir = $config->get('db-tools.workdir', $app->basePath()); + foreach ($config->get('db-tools.anonymization_files', []) as $connectionName => $file) { // 0 is not a good index for extension, this fails for false and 0. if (!($pos = \strrpos($file, '.'))) { @@ -155,8 +157,8 @@ function (AnonymizatorFactory $factory, Application $app): void { $ext = \substr($file, $pos + 1); $loader = match ($ext) { - 'php' => new PhpFileLoader($file, $connectionName), - 'yml', 'yaml' => new YamlLoader($file, $connectionName), + 'php' => new PhpFileLoader($file, $connectionName, $workdir), + 'yml', 'yaml' => new YamlLoader($file, $connectionName, $workdir), default => throw new ConfigurationException(\sprintf( "File extension \"%s\" is unsupported (given path: \"%s\").", $ext, @@ -168,7 +170,7 @@ function (AnonymizatorFactory $factory, Application $app): void { } foreach ($config->get('db-tools.anonymization', []) as $connectionName => $array) { - $factory->addConfigurationLoader(new ArrayLoader($array, $connectionName)); + $factory->addConfigurationLoader(new ArrayLoader($array, $connectionName, $workdir)); } } ); diff --git a/src/Bridge/Standalone/Bootstrap.php b/src/Bridge/Standalone/Bootstrap.php index 32b16932..8aa704ee 100644 --- a/src/Bridge/Standalone/Bootstrap.php +++ b/src/Bridge/Standalone/Bootstrap.php @@ -237,10 +237,10 @@ public static function bootstrap(array $config = [], array $configFiles = [], ?L $anonymizatorFactory = new AnonymizatorFactory($databaseSessionRegistry, $anonymizerRegistry, $logger); foreach (($config['anonymization_files'] ?? []) as $connectionName => $file) { - $anonymizatorFactory->addConfigurationLoader(new YamlLoader($file, $connectionName)); + $anonymizatorFactory->addConfigurationLoader(new YamlLoader($file, $connectionName, $config['workdir'])); } foreach (($config['anonymization'] ?? []) as $connectionName => $array) { - $anonymizatorFactory->addConfigurationLoader(new ArrayLoader($array, $connectionName)); + $anonymizatorFactory->addConfigurationLoader(new ArrayLoader($array, $connectionName, $config['workdir'])); } $backupperFactory = new BackupperFactory($databaseSessionRegistry, $configRegistry, $logger); diff --git a/src/Bridge/Symfony/DependencyInjection/Compiler/DbToolsPass.php b/src/Bridge/Symfony/DependencyInjection/Compiler/DbToolsPass.php index b4a2982f..173671e2 100644 --- a/src/Bridge/Symfony/DependencyInjection/Compiler/DbToolsPass.php +++ b/src/Bridge/Symfony/DependencyInjection/Compiler/DbToolsPass.php @@ -12,6 +12,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Parameter; use Symfony\Component\DependencyInjection\Reference; class DbToolsPass implements CompilerPassInterface @@ -49,7 +50,7 @@ private function registerArrayLoader(array $data, string $connectionName, Contai { $definition = new Definition(); $definition->setClass(ArrayLoader::class); - $definition->setArguments([$data, $connectionName]); + $definition->setArguments([$data, $connectionName, new Parameter('kernel.project_dir')]); $loaderId = 'db_tools.anonymization.loader.array.' . $connectionName; $container->setDefinition($loaderId, $definition); @@ -61,7 +62,7 @@ private function registerYamlLoader(string $file, string $connectionName, Contai { $definition = new Definition(); $definition->setClass(YamlLoader::class); - $definition->setArguments([$file, $connectionName]); + $definition->setArguments([$file, $connectionName, new Parameter('kernel.project_dir')]); $loaderId = 'db_tools.anonymization.loader.yaml.' . $connectionName; $container->setDefinition($loaderId, $definition); @@ -73,7 +74,7 @@ private function registerAttributesLoader(ContainerBuilder $container): string { $definition = new Definition(); $definition->setClass(AttributesLoader::class); - $definition->setArguments([new Reference('doctrine.orm.command.entity_manager_provider')]); + $definition->setArguments([new Reference('doctrine.orm.command.entity_manager_provider'), new Parameter('kernel.project_dir')]); $loaderId = 'db_tools.anonymization.loader.attributes'; $container->setDefinition($loaderId, $definition); diff --git a/src/Test/FunctionalTestCase.php b/src/Test/FunctionalTestCase.php index 0091f33b..8d5a133c 100644 --- a/src/Test/FunctionalTestCase.php +++ b/src/Test/FunctionalTestCase.php @@ -6,21 +6,22 @@ use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Driver\AbstractSQLiteDriver\Middleware\EnableForeignKeys; use Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession; -use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Logging\Middleware; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; use Doctrine\Persistence\ManagerRegistry; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizator; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry; +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig; use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Bridge\Symfony\DoctrineDatabaseSessionRegistry; use MakinaCorpus\DbToolsBundle\Database\DatabaseSessionRegistry; -use MakinaCorpus\QueryBuilder\Bridge\Doctrine\DoctrineQueryBuilder; use MakinaCorpus\QueryBuilder\DatabaseSession; +use MakinaCorpus\QueryBuilder\Bridge\Doctrine\DoctrineQueryBuilder; use MakinaCorpus\QueryBuilder\Error\Server\DatabaseObjectDoesNotExistError; use Psr\Log\AbstractLogger; @@ -257,6 +258,27 @@ protected function createAnonymizatorWithConfig(AnonymizerConfig ...$anonymizerC ); } + /** + * For anonymizers unit test, creates an anonymizator with the given + * configuration, which should register all anonymizers required for + * the test. + */ + protected function createAnonymizatorArbitrary( + string $table, + string $targetName, + string $anonymizer, + ?Options $options = null, + ): Anonymizator { + $config = new AnonymizationConfig(); + $config->add(new AnonymizerConfig($table, $targetName, $anonymizer, $options ?? new Options(), __DIR__)); + + return new Anonymizator( + $this->getDatabaseSession(), + new AnonymizerRegistry(), + $config + ); + } + /** * Create database connection. * diff --git a/tests/Functional/Anonymizer/AnonymizatorTest.php b/tests/Functional/Anonymizer/AnonymizatorTest.php index 037daeaf..746554df 100644 --- a/tests/Functional/Anonymizer/AnonymizatorTest.php +++ b/tests/Functional/Anonymizer/AnonymizatorTest.php @@ -55,6 +55,7 @@ public function testMultipleAnonymizersAtOnce(): void new Options([ 'sample' => ['foo', 'bar', 'baz'], ]), + __DIR__, )); $config->add(new AnonymizerConfig( 'table_test', @@ -64,6 +65,7 @@ public function testMultipleAnonymizersAtOnce(): void 'iban' => 'my_iban', 'bic' => 'my_bic', ]), + __DIR__, )); $anonymizator = new Anonymizator($this->getDatabaseSession(), new AnonymizerRegistry(), $config); diff --git a/tests/Functional/Anonymizer/Core/AddressAnonymizerTest.php b/tests/Functional/Anonymizer/Core/AddressAnonymizerTest.php index 4e4389e9..c4f38d56 100644 --- a/tests/Functional/Anonymizer/Core/AddressAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/AddressAnonymizerTest.php @@ -5,7 +5,6 @@ namespace DbToolsBundle\PackFrFR\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class AddressAnonymizerTest extends FunctionalTestCase @@ -52,7 +51,7 @@ protected function createTestData(): void public function testAnonymize(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'address', @@ -64,7 +63,7 @@ public function testAnonymize(): void 'region' => 'my_region', 'country' => 'my_country', ]) - )); + ); $this->assertSame( "Rue Aristide Briand", diff --git a/tests/Functional/Anonymizer/Core/ConstantAnonymizerTest.php b/tests/Functional/Anonymizer/Core/ConstantAnonymizerTest.php index 270427f9..b851f79c 100644 --- a/tests/Functional/Anonymizer/Core/ConstantAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/ConstantAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class ConstantAnonymizerTest extends FunctionalTestCase @@ -37,12 +36,12 @@ public function testAnonymize(): void ], ); - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'constant', new Options(['value' => 'xxxxxx']) - )); + ); $this->assertSame( "toto1@example.com", @@ -86,12 +85,12 @@ public function testAnonymizeWithCustomType(): void ], ); - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'constant', new Options(['value' => '2012', 'type' => 'integer']) - )); + ); $this->assertSame( 52, diff --git a/tests/Functional/Anonymizer/Core/DateAnonymizerTest.php b/tests/Functional/Anonymizer/Core/DateAnonymizerTest.php index 97492c55..84dcbf51 100644 --- a/tests/Functional/Anonymizer/Core/DateAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/DateAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class DateAnonymizerTest extends FunctionalTestCase @@ -47,7 +46,7 @@ protected function createTestData(): void public function testAnonymizeWithRangeAsDate(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'date1', 'date', @@ -56,7 +55,7 @@ public function testAnonymizeWithRangeAsDate(): void 'max' => '2010-08-31', 'format' => 'datetime', ]) - )); + ); $this->assertStringStartsWith( "1983-03-22 08:25:00", @@ -84,7 +83,7 @@ public function testAnonymizeWithRangeAsDate(): void */ public function testAnonymizeWithRangeAsDateHuge(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'date1', 'date', @@ -93,7 +92,7 @@ public function testAnonymizeWithRangeAsDateHuge(): void 'max' => '2560-03-31', 'format' => 'date', ]) - )); + ); $this->assertStringStartsWith( "1983-03-22 08:25:00", @@ -118,7 +117,7 @@ public function testAnonymizeWithRangeAsDateHuge(): void public function testAnonymizeWithRangeAsDateTime(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'date1', 'date', @@ -127,7 +126,7 @@ public function testAnonymizeWithRangeAsDateTime(): void 'max' => '2010-08-31 18:25:00', 'format' => 'datetime', ]) - )); + ); $this->assertStringStartsWith( "1983-03-22 08:25:00", @@ -155,7 +154,7 @@ public function testAnonymizeWithRangeAsDateTime(): void */ public function testAnonymizeWithRangeAsDateTimeHuge(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'date1', 'date', @@ -164,7 +163,7 @@ public function testAnonymizeWithRangeAsDateTimeHuge(): void 'max' => '2100-12-31', 'format' => 'datetime', ]) - )); + ); $this->assertStringStartsWith( "1983-03-22 08:25:00", @@ -189,7 +188,7 @@ public function testAnonymizeWithRangeAsDateTimeHuge(): void public function testAnonymizeWithDeltaAsDate(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'date2', 'date', @@ -197,7 +196,7 @@ public function testAnonymizeWithDeltaAsDate(): void 'delta' => '1 month 6 days 4 hour', 'format' => 'date', ]) - )); + ); $this->assertStringStartsWith( "1983-03-22", @@ -222,7 +221,7 @@ public function testAnonymizeWithDeltaAsDate(): void public function testAnonymizeWithDeltaAsDateTime(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'date1', 'date', @@ -230,7 +229,7 @@ public function testAnonymizeWithDeltaAsDateTime(): void 'delta' => '1 month 6 days 4 hour', 'format' => 'datetime', ]) - )); + ); $this->assertStringStartsWith( "1983-03-22 08:25:00", diff --git a/tests/Functional/Anonymizer/Core/EmailAnonymizerTest.php b/tests/Functional/Anonymizer/Core/EmailAnonymizerTest.php index a41d3cf8..e69716e4 100644 --- a/tests/Functional/Anonymizer/Core/EmailAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/EmailAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class EmailAnonymizerTest extends FunctionalTestCase @@ -41,12 +40,12 @@ protected function createTestData(): void public function testAnonymize(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'email', new Options() - )); + ); $this->assertSame( "toto1@example.com", @@ -71,12 +70,12 @@ public function testAnonymize(): void public function testAnonymizeWithDomain(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'email', new Options(['domain' => 'custom_domain.tld']) - )); + ); $this->assertSame( "toto1@example.com", diff --git a/tests/Functional/Anonymizer/Core/FloatAnonymizerTest.php b/tests/Functional/Anonymizer/Core/FloatAnonymizerTest.php index 782eaacd..ea25e814 100644 --- a/tests/Functional/Anonymizer/Core/FloatAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/FloatAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; use MakinaCorpus\QueryBuilder\Vendor; @@ -42,12 +41,12 @@ protected function createTestData(): void public function testAnonymizeWithMinAndMax(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'float', new Options(['min' => 2, 'max' => 5.5, 'precision' => 6]) - )); + ); $this->assertSame( 10.5, @@ -89,12 +88,12 @@ public function testAnonymizeWithMinAndMax(): void public function testAnonymizeWithDelta(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'float', new Options(['delta' => 5.2, 'precision' => 4]) - )); + ); $this->assertSame( 10.5, @@ -140,12 +139,12 @@ public function testAnonymizeWithPercent(): void // @see https://github.com/makinacorpus/DbToolsBundle/issues/210 $this->skipIfDatabaseLessThan(Vendor::MYSQL, '8', 'A well known issue appears randomly with MYSQL 5.7.'); - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'float', new Options(['percent' => 5]) - )); + ); $this->assertSame( 10.5, diff --git a/tests/Functional/Anonymizer/Core/IntegerAnonymizerTest.php b/tests/Functional/Anonymizer/Core/IntegerAnonymizerTest.php index e38a7f25..9f697c78 100644 --- a/tests/Functional/Anonymizer/Core/IntegerAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/IntegerAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class IntegerAnonymizerTest extends FunctionalTestCase @@ -41,12 +40,12 @@ protected function createTestData(): void public function testAnonymizeWithMinAndMax(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'integer', new Options(['min' => 200, 'max' => 10000]) - )); + ); $this->assertSame( 10, @@ -82,12 +81,12 @@ public function testAnonymizeWithMinAndMax(): void public function testAnonymizeWithDelta(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'integer', new Options(['delta' => 10]) - )); + ); $this->assertSame( 10, @@ -124,12 +123,12 @@ public function testAnonymizeWithDelta(): void public function testAnonymizeWithPercent(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'integer', new Options(['percent' => 50]) - )); + ); $this->assertSame( 10, diff --git a/tests/Functional/Anonymizer/Core/LoremIpsumAnonymizerTest.php b/tests/Functional/Anonymizer/Core/LoremIpsumAnonymizerTest.php index ff0ec5c7..b450aabd 100644 --- a/tests/Functional/Anonymizer/Core/LoremIpsumAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/LoremIpsumAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class LoremIpsumAnonymizerTest extends FunctionalTestCase @@ -41,12 +40,11 @@ protected function createTestData(): void public function testAnonymizeWithDefaultOptions(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'lorem', - new Options() - )); + ); $this->assertSame( 'test1', @@ -83,7 +81,7 @@ public function testAnonymizeWithDefaultOptions(): void public function testAnonymizeWithParagraphsAndTag(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'lorem', @@ -91,7 +89,7 @@ public function testAnonymizeWithParagraphsAndTag(): void 'paragraphs' => 5, 'html' => true ]) - )); + ); $this->assertSame( 'test1', @@ -125,12 +123,12 @@ public function testAnonymizeWithParagraphsAndTag(): void public function testAnonymizeWithWords(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'lorem', new Options(['words' => 5]) - )); + ); $this->assertSame( 'test1', diff --git a/tests/Functional/Anonymizer/Core/Md5AnonymizerTest.php b/tests/Functional/Anonymizer/Core/Md5AnonymizerTest.php index 0252f1ae..c9c725c5 100644 --- a/tests/Functional/Anonymizer/Core/Md5AnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/Md5AnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; use MakinaCorpus\QueryBuilder\Vendor; @@ -46,12 +45,12 @@ public function testAnonymize(): void $sample = ['sample1', 'sample2', 'sample3', 'sample4', 'sample5']; - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'md5', new Options(['sample' => $sample]) - )); + ); $this->assertSame( 'test1', diff --git a/tests/Functional/Anonymizer/Core/NullAnonymizerTest.php b/tests/Functional/Anonymizer/Core/NullAnonymizerTest.php index 6eb76cfe..99ec0111 100644 --- a/tests/Functional/Anonymizer/Core/NullAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/NullAnonymizerTest.php @@ -4,8 +4,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; -use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class NullAnonymizerTest extends FunctionalTestCase @@ -41,12 +39,11 @@ protected function createTestData(): void public function testAnonymize(): void { - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'null', - new Options() - )); + ); $this->assertSame( "toto1@example.com", diff --git a/tests/Functional/Anonymizer/Core/StringAnonymizerTest.php b/tests/Functional/Anonymizer/Core/StringAnonymizerTest.php index 601bb9a2..10473958 100644 --- a/tests/Functional/Anonymizer/Core/StringAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/StringAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class StringAnonymizerTest extends FunctionalTestCase @@ -43,12 +42,12 @@ public function testAnonymize(): void { $sample = ['sample1', 'sample2', 'sample3', 'sample4', 'sample5']; - $anonymizator = $this->createAnonymizatorWithConfig(new AnonymizerConfig( + $anonymizator = $this->createAnonymizatorArbitrary( 'table_test', 'data', 'string', new Options(['sample' => $sample]) - )); + ); $this->assertSame( 'test1', diff --git a/tests/Functional/Anonymizer/Core/StringPatternAnonymizerTest.php b/tests/Functional/Anonymizer/Core/StringPatternAnonymizerTest.php index cc7ec864..83c2e1fc 100644 --- a/tests/Functional/Anonymizer/Core/StringPatternAnonymizerTest.php +++ b/tests/Functional/Anonymizer/Core/StringPatternAnonymizerTest.php @@ -5,7 +5,6 @@ namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core; use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; -use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase; class StringPatternAnonymizerTest extends FunctionalTestCase @@ -41,13 +40,11 @@ protected function createTestData(): void public function testAnonymize(): void { - $anonymizator = $this->createAnonymizatorWithConfig( - new AnonymizerConfig( - 'table_test', - 'data', - 'pattern', - new Options(['pattern' => "Range [1-1000] for {email} and {address:locality} in {address:country}"]) - ), + $anonymizator = $this->createAnonymizatorArbitrary( + 'table_test', + 'data', + 'pattern', + new Options(['pattern' => "Range [1-1000] for {email} and {address:locality} in {address:country}"]), ); self::assertSame( diff --git a/tests/Unit/Anonymization/AnonymizatorFactoryTest.php b/tests/Unit/Anonymization/AnonymizatorFactoryTest.php index 0544afb5..688e5e07 100644 --- a/tests/Unit/Anonymization/AnonymizatorFactoryTest.php +++ b/tests/Unit/Anonymization/AnonymizatorFactoryTest.php @@ -63,13 +63,15 @@ public function testGetOrCreateWithConfig(): void 'table_test', 'target_test', 'anonymizer_test', - new Options() + new Options(), + __DIR__, ), new AnonymizerConfig( 'table_test', 'target_test2', 'anonymizer_test2', - new Options(['option1' => 'value1']) + new Options(['option1' => 'value1']), + __DIR__, ) ])); diff --git a/tests/Unit/Anonymization/Configuration/AttributeLoaderTest.php b/tests/Unit/Anonymization/Configuration/AttributeLoaderTest.php index 7625f9c7..adcd575f 100644 --- a/tests/Unit/Anonymization/Configuration/AttributeLoaderTest.php +++ b/tests/Unit/Anonymization/Configuration/AttributeLoaderTest.php @@ -40,7 +40,7 @@ public function testLoadOk(): void // We try to load configuration for the 'default' connection. $config = new AnonymizationConfig('default'); - (new AttributesLoader($entityManagerProvider))->load($config); + (new AttributesLoader($entityManagerProvider, __DIR__))->load($config); // Then we validate what's in it: $testTableConfig = $config->getTableConfig('test'); @@ -89,7 +89,7 @@ public function testLoadWithJoinedInheritance(): void // We load configuration for the 'default' connection. $config = new AnonymizationConfig('default'); - (new AttributesLoader($entityManagerProvider))->load($config); + (new AttributesLoader($entityManagerProvider, __DIR__))->load($config); // Then we validate what's in it: $testTableConfig = $config->getTableConfig('test_joined_child'); @@ -126,7 +126,7 @@ public function testLoadWithEmbeddedOk(): void // We load configuration for the 'default' connection. $config = new AnonymizationConfig('default'); - (new AttributesLoader($entityManagerProvider))->load($config); + (new AttributesLoader($entityManagerProvider, __DIR__))->load($config); // Then we validate what's in it: $testTableConfig = $config->getTableConfig('test_with_embedded');