Skip to content

Commit

Permalink
Move query validation to the dumper namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Jan 28, 2025
1 parent 7e37fd3 commit 6d4eaef
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 128 deletions.
3 changes: 0 additions & 3 deletions app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ services:
config.compiler.processor.env_var:
class: 'Smile\GdprDump\Config\Compiler\Processor\EnvVarProcessor'

config.compiler.processor.query_validation:
class: 'Smile\GdprDump\Config\Compiler\Processor\QueryValidationProcessor'

config.compiler.processor.version:
class: 'Smile\GdprDump\Config\Compiler\Processor\VersionProcessor'

Expand Down
52 changes: 0 additions & 52 deletions src/Config/Compiler/Processor/QueryValidationProcessor.php

This file was deleted.

35 changes: 33 additions & 2 deletions src/Dumper/Config/DumperConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

final class DumperConfig implements DumperConfigInterface
{
Expand Down Expand Up @@ -49,8 +50,8 @@ final class DumperConfig implements DumperConfigInterface

public function __construct(ConfigInterface $config)
{
$this->varQueries = (array) $config->get('variables', []);
$this->dumpSettings = (array) $config->get('dump', []);
$this->prepareDumpSettings($config);
$this->prepareVarQueries($config);
$this->prepareFakerSettings($config);
$this->prepareFilterPropagationSettings($config);
$this->prepareTableSettings($config);
Expand Down Expand Up @@ -144,6 +145,36 @@ public function getTablesToSort(): array
return $this->tablesToSort;
}

/**
* Prepare dump settings.
*/
private function prepareDumpSettings(ConfigInterface $config): void
{
$this->dumpSettings = (array) $config->get('dump', []);

// Validate init commands
$queryValidator = new QueryValidator(['set']);
$initCommands = (array) ($this->dumpSettings['init_commands'] ?? []);

foreach ($initCommands as $query) {
$queryValidator->validate($query);
}
}

/**
* Prepare SQL variables.
*/
private function prepareVarQueries(ConfigInterface $config): void
{
$this->varQueries = (array) $config->get('variables', []);

// Validate SQL queries
$queryValidator = new QueryValidator(['select']);
foreach ($this->varQueries as $query) {
$queryValidator->validate($query);
}
}

/**
* Prepare faker settings.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Smile\GdprDump\Util;
namespace Smile\GdprDump\Dumper\Config\Validation;

use TheSeer\Tokenizer\TokenCollection;
use TheSeer\Tokenizer\Tokenizer;
Expand Down
1 change: 0 additions & 1 deletion src/Dumper/Config/Validation/WhereExprValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Smile\GdprDump\Dumper\Config\Validation;

use Smile\GdprDump\Util\QueryValidator;
use TheSeer\Tokenizer\Token;
use UnexpectedValueException;

Expand Down

This file was deleted.

27 changes: 27 additions & 0 deletions tests/unit/Dumper/Config/DumperConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Smile\GdprDump\Config\Config;
use Smile\GdprDump\Dumper\Config\DumperConfig;
use Smile\GdprDump\Tests\Unit\TestCase;
use UnexpectedValueException;

final class DumperConfigTest extends TestCase
{
Expand Down Expand Up @@ -131,6 +132,32 @@ public function testDefaultValues(): void
$this->assertSame('', $config->getFakerSettings()->getLocale());
}

/**
* Assert that an exception is thrown when a var query contains a forbidden statement.
*/
public function testInvalidStatementInVariableQuery(): void
{
$this->expectException(UnexpectedValueException::class);
$this->createConfig([
'variables' => ['my_var' => 'select my_col from my_table; delete from my_table'],
]);
}

/**
* Assert that an exception is thrown when an init command contains a forbidden statement.
*/
public function testInvalidStatementInInitCommand(): void
{
$this->expectException(UnexpectedValueException::class);
$this->createConfig([
'dump' => [
'init_commands' => [
'my_var' => 'select my_col from my_table; delete from my_table',
],
],
]);
}

/**
* Create a dumper config object that stores the specified data.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Dumper/Config/Validation/QueryValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Validation;

use RuntimeException;
use Smile\GdprDump\Dumper\Config\Validation\QueryValidator;
use Smile\GdprDump\Tests\Unit\TestCase;
use Smile\GdprDump\Util\QueryValidator;
use TheSeer\Tokenizer\Token;
use UnexpectedValueException;

Expand Down

0 comments on commit 6d4eaef

Please sign in to comment.