Skip to content

Commit

Permalink
no issue - add NotImplementedError and mark tests as incomplete for m…
Browse files Browse the repository at this point in the history
…issing driver related features
  • Loading branch information
pounard committed Nov 24, 2023
1 parent 6c43788 commit 7e13b7f
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 105 deletions.
18 changes: 13 additions & 5 deletions dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ do_checks() {
docker compose -p db_tools_bundle_test exec phpunit composer checks
}

# Launch PHPUnit tests without any database vendor
do_unittest() {
section_title "PHPUnit unit tests"
docker compose -p db_tools_bundle_test exec phpunit vendor/bin/phpunit
}

do_test_mysql57() {
section_title "Running tests with MySQL 5.7"
docker compose -p db_tools_bundle_test exec \
Expand Down Expand Up @@ -204,17 +210,19 @@ do_notice() {
printf "\n - build, up and down a complete docker stack with all database vendors"
printf "\n and versions that the DbToolsBundle supports"
printf "\n - run Code Style Fixer (with PHP CS Fixer) and launch Static Analysis (with PHPStan)"
printf "\n - run PHPunit tests for all database vendors"
printf "\n - run PHPUnit tests for all database vendors"
printf "\n\n--\n"
printf "\nLaunch the script with one of these available actions:"
printf "\n"
printf "\n - ${GREEN}build${NC}: Build docker containers"
printf "\n - ${GREEN}up${NC}: Start docker containers"
printf "\n - ${GREEN}down${NC}: Stop docker containers"
printf "\n - ${GREEN}checks${NC}: Launch composer checks (for Static analysis & Code style fixer)"
printf "\n - ${GREEN}test_all${NC}: Run PHPunit tests for all database vendors."
printf "\n PHPUnit options can be used as usual: ${GREEN}./dev.sh test_all --filter AnonymizatorFactoryTest${NC}"
printf "\n - ${GREEN}test${NC}: Run PHPunit tests for a specific database vendors or version"
printf "\n - ${GREEN}test_all${NC}: Run PHPUnit tests for all database vendors."
printf "\n PHPUnit options can be used as usual:"
printf "\n ${GREEN}./dev.sh test_all --filter AnonymizatorFactoryTest${NC}"
printf "\n - ${GREEN}test${NC}: Run PHPUnit tests for a specific database vendors or version"
printf "\n - ${GREEN}unittest${NC}: Run PHPUnit tests without any database vendor"
printf "\n - ${GREEN}notice${NC}: Display this help"
printf "\n\n"
}
Expand All @@ -225,6 +233,6 @@ action=${1-}
if [[ -n $@ ]];then shift;fi

case $action in
build|up|down|checks|test_all|test|notice) do_$action "$@";;
build|up|down|checks|test_all|unittest|test|notice) do_$action "$@";;
*) do_notice;;
esac
27 changes: 10 additions & 17 deletions src/Backupper/BackupperFactoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;

class BackupperFactoryRegistry
{
Expand Down Expand Up @@ -33,25 +34,17 @@ public function create(?string $connectionName = null): BackupperInterface
$connection = $this->doctrineRegistry->getConnection($connectionName);
$driver = $connection->getParams()['driver'];

if (!\array_key_exists($driver, $this->backupperBinaries)) {
throw new \InvalidArgumentException(\sprintf(
"There is no backupper binary provided for DBAL driver '%s'",
$driver
));
}

foreach($this->backupperFactories as $backupperFactory) {
if ($backupperFactory->isSupported($driver)) {
return $backupperFactory->create(
$this->backupperBinaries[$driver],
$connection
);
if (\array_key_exists($driver, $this->backupperBinaries)) {
foreach ($this->backupperFactories as $backupperFactory) {
if ($backupperFactory->isSupported($driver)) {
return $backupperFactory->create(
$this->backupperBinaries[$driver],
$connection
);
}
}
}

throw new \InvalidArgumentException(\sprintf(
"No backupper found for connection '%s'",
$connectionName
));
throw new NotImplementedException(\sprintf("Backup is not implemented or configured for driver '%s' while using connection '%s'", $driver, $connectionName));
}
}
16 changes: 12 additions & 4 deletions src/Command/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace MakinaCorpus\DbToolsBundle\Command;

use MakinaCorpus\DbToolsBundle\DbToolsStorage;
use MakinaCorpus\DbToolsBundle\Backupper\BackupperFactoryRegistry;
use MakinaCorpus\DbToolsBundle\Backupper\BackupperInterface;
use MakinaCorpus\DbToolsBundle\DbToolsStorage;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -64,6 +65,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);

if ($input->getOption('connection')) {
$this->connectionName = $input->getOption('connection');
}
Expand All @@ -72,10 +74,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->excludedTables[$this->connectionName] = \explode(',', $excludedTables);
}

$created = $this->doBackup();
try {
$created = $this->doBackup();

if (!$input->getOption('no-cleanup')) {
$this->cleanupBackups($created);
}
} catch (NotImplementedException $e) {
$this->io->error($e->getMessage());

if (!$input->getOption('no-cleanup')) {
$this->cleanupBackups($created);
return NotImplementedException::CONSOLE_EXIT_STATUS;
}

return Command::SUCCESS;
Expand Down
19 changes: 13 additions & 6 deletions src/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MakinaCorpus\DbToolsBundle\Command;

use MakinaCorpus\DbToolsBundle\Backupper\BackupperFactoryRegistry;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;
use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactoryRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -45,13 +46,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$connection = $input->getArgument('connection') ?? $this->defaultConnectionName;

$backupper = $this->backupperFactoryRegistry->create($connection);
$response = $backupper->checkBinary();
$io->success("Backupper binary ok : " . $response);
try {
$backupper = $this->backupperFactoryRegistry->create($connection);
$response = $backupper->checkBinary();
$io->success("Backupper binary ok : " . $response);

$restorer = $this->restorerFactoryRegistry->create($connection);
$response = $restorer->checkBinary();
$io->success("Restorer binary ok : " . $response);
$restorer = $this->restorerFactoryRegistry->create($connection);
$response = $restorer->checkBinary();
$io->success("Restorer binary ok : " . $response);
} catch (NotImplementedException $e) {
$io->error($e->getMessage());

return NotImplementedException::CONSOLE_EXIT_STATUS;
}

return Command::SUCCESS;
}
Expand Down
13 changes: 10 additions & 3 deletions src/Command/GdprifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use MakinaCorpus\DbToolsBundle\Anonymization\AnonymizatorFactory;
use MakinaCorpus\DbToolsBundle\Backupper\BackupperFactoryRegistry;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;
use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactoryRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -108,9 +109,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->connectionName = $input->getOption('connection') ?? $this->connectionName;

$this->doRestore();
$this->doAnonymize();
$this->doBackup();
try {
$this->doRestore();
$this->doAnonymize();
$this->doBackup();
} catch (NotImplementedException $e) {
$this->io->error($e->getMessage());

return NotImplementedException::CONSOLE_EXIT_STATUS;
}

return Command::SUCCESS;
}
Expand Down
29 changes: 18 additions & 11 deletions src/Command/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace MakinaCorpus\DbToolsBundle\Command;

use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactoryRegistry;
use MakinaCorpus\DbToolsBundle\DbToolsStorage;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;
use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactoryRegistry;
use MakinaCorpus\DbToolsBundle\Restorer\RestorerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -81,19 +82,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->preventMistake($input);

$this->restorer = $this->restorerFactory->create($this->connectionName);
try {
$this->restorer = $this->restorerFactory->create($this->connectionName);

if (!$input->getOption('filename')) {
$this->chooseBackup();
} else {
$this->backupFilename = $input->getOption('filename');
}
if (!$input->getOption('filename')) {
$this->chooseBackup();
} else {
$this->backupFilename = $input->getOption('filename');
}

if (!$this->backupFilename) {
return Command::INVALID;
}
if (!$this->backupFilename) {
return Command::INVALID;
}

$this->doRestore();
$this->doRestore();
} catch (NotImplementedException $e) {
$this->io->error($e->getMessage());

return Command::FAILURE;
}

return Command::SUCCESS;
}
Expand Down
35 changes: 21 additions & 14 deletions src/Command/StatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MakinaCorpus\DbToolsBundle\Command;

use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;
use MakinaCorpus\DbToolsBundle\Stats\StatValue;
use MakinaCorpus\DbToolsBundle\Stats\StatValueList;
use MakinaCorpus\DbToolsBundle\Stats\StatsProviderFactoryRegistry;
Expand Down Expand Up @@ -191,23 +192,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$which = $input->getArgument('which');

$collections = match ($which) {
'table' => $this->statsProviderFactoryRegistry->get($connection)->getTableStats($tags),
'index' => $this->statsProviderFactoryRegistry->get($connection)->getIndexStats($tags),
'global' => $this->statsProviderFactoryRegistry->get($connection)->getGlobalStats($tags),
default => throw new InvalidArgumentException(\sprintf("'which' allowed values are: '%s'", \implode("', '", ['global', 'table', 'index']))),
};
try {
$collections = match ($which) {
'table' => $this->statsProviderFactoryRegistry->get($connection)->getTableStats($tags),
'index' => $this->statsProviderFactoryRegistry->get($connection)->getIndexStats($tags),
'global' => $this->statsProviderFactoryRegistry->get($connection)->getGlobalStats($tags),
default => throw new InvalidArgumentException(\sprintf("'which' allowed values are: '%s'", \implode("', '", ['global', 'table', 'index']))),
};

$hasValues = false;
$hasValues = false;

if ($input->getOption('flat')) {
$hasValues = $this->displayFlat($collections, $output);
} else {
$hasValues = $this->displayTable($collections, $output);
}
if ($input->getOption('flat')) {
$hasValues = $this->displayFlat($collections, $output);
} else {
$hasValues = $this->displayTable($collections, $output);
}

if (!$hasValues) {
$io->warning(\sprintf("Statistics for '%s' are not supported for the current '%s' connexion database driver.", $which, $connection));
}
} catch (NotImplementedException $e) {
$io->error($e->getMessage());

if (!$hasValues) {
$io->warning(\sprintf("Statistics for '%s' are not supported for the current '%s' connexion database driver.", $which, $connection));
return NotImplementedException::CONSOLE_EXIT_STATUS;
}

return Command::SUCCESS;
Expand Down
15 changes: 15 additions & 0 deletions src/Error/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare (strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Error;

class NotImplementedException extends \InvalidArgumentException
{
/**
* Console exit status code when command fail with this exception.
*
* Number choice is arbitrary and in the 1-125 range.
*/
const CONSOLE_EXIT_STATUS = 9;
}
27 changes: 10 additions & 17 deletions src/Restorer/RestorerFactoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;

class RestorerFactoryRegistry
{
Expand Down Expand Up @@ -33,25 +34,17 @@ public function create(?string $connectionName = null): RestorerInterface
$connection = $this->doctrineRegistry->getConnection($connectionName);
$driver = $connection->getParams()['driver'];

if (!\array_key_exists($driver, $this->restorerBinaries)) {
throw new \InvalidArgumentException(\sprintf(
"There is no restorer binary provided for DBAL driver '%s'",
$driver
));
}

foreach($this->restorerFactories as $restorerFactory) {
if ($restorerFactory->isSupported($driver)) {
return $restorerFactory->create(
$this->restorerBinaries[$driver],
$connection
);
if (\array_key_exists($driver, $this->restorerBinaries)) {
foreach ($this->restorerFactories as $restorerFactory) {
if ($restorerFactory->isSupported($driver)) {
return $restorerFactory->create(
$this->restorerBinaries[$driver],
$connection
);
}
}
}

throw new \InvalidArgumentException(\sprintf(
"No restorer found for connection '%s'",
$connectionName
));
throw new NotImplementedException(\sprintf("Restore is not implemented or configured for driver '%s' while using connection '%s'", $driver, $connectionName));
}
}
3 changes: 2 additions & 1 deletion src/Stats/StatsProviderFactoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use MakinaCorpus\DbToolsBundle\Error\NotImplementedException;

class StatsProviderFactoryRegistry
{
Expand Down Expand Up @@ -37,6 +38,6 @@ public function get(?string $connectionName = null): StatsProvider
}
}

throw new \InvalidArgumentException(\sprintf("No stats provider found for connection '%s'", $connectionName));
throw new NotImplementedException(\sprintf("Stat collection is not implemented for driver '%s' while using connection '%s'", $driver, $connectionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Command\Anonymization;

use MakinaCorpus\DbToolsBundle\Tests\FunctionalKernelTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class AnonymizeCommandTest extends KernelTestCase
class AnonymizeCommandTest extends FunctionalKernelTestCase
{
public function testExecute(): void
{
self::skipIfNoDatabase();

$kernel = self::bootKernel();
$application = new Application($kernel);

Expand All @@ -27,6 +29,6 @@ public function testExecute(): void
]
);

$commandTester->assertCommandIsSuccessful();
self::assertCommandIsSuccessful($commandTester);
}
}
Loading

0 comments on commit 7e13b7f

Please sign in to comment.