From 4c9c7f9e6836018454ea7ccfc6dab9f31325f0e5 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 23 Apr 2018 16:26:39 +0100 Subject: [PATCH] Use the factories instead of messing about with the container (#16) --- etc/di.php | 17 +-------- src/Command/ConfigureCommand.php | 8 +++-- src/Command/ExportCommand.php | 14 ++++---- src/Command/GetCommand.php | 25 ++++++------- src/Command/ImportCommand.php | 13 ++++--- src/Command/LsCommand.php | 11 ++++-- src/Command/PutCommand.php | 23 ++++++------ src/Command/RmCommand.php | 9 +++-- src/Service/DatabaseFactory.php | 19 +++++++--- src/Service/FilesystemFactory.php | 19 +++++++--- src/Service/StorageFactory.php | 21 +++++++---- tests/Command/AbstractCommandTest.php | 49 ++++++++++++++++++++++++++ tests/Command/ConfigureCommandTest.php | 2 +- tests/Command/GetCommandTest.php | 2 +- tests/Command/LsCommandTest.php | 2 +- tests/Command/PutCommandTest.php | 2 +- tests/Command/RmCommandTest.php | 2 +- 17 files changed, 160 insertions(+), 78 deletions(-) diff --git a/etc/di.php b/etc/di.php index b855fe3..deb356a 100644 --- a/etc/di.php +++ b/etc/di.php @@ -19,22 +19,7 @@ $commands = []; foreach ($c->get('commands') as $command) { - $manualArgs = []; - - switch ($command) { - case 'Meanbee\Magedbm2\Command\LsCommand': - // Ensure that we get two different instances of the object for our LsCommand. The default behaviour - // of the container would give us the same instance. - $manualArgs['dataStorage'] = $c->make(\Meanbee\Magedbm2\Service\StorageInterface::class); - case 'Meanbee\Magedbm2\Command\ExportCommand': - case 'Meanbee\Magedbm2\Command\GetCommand': - case 'Meanbee\Magedbm2\Command\ImportCommand': - case 'Meanbee\Magedbm2\Command\PutCommand': - case 'Meanbee\Magedbm2\Command\RmCommand': - $manualArgs['storage'] = $c->make(\Meanbee\Magedbm2\Service\StorageInterface::class); - } - - $commands[] = $c->make($command, $manualArgs); + $commands[] = $c->make($command); } foreach ($commands as $command) { diff --git a/src/Command/ConfigureCommand.php b/src/Command/ConfigureCommand.php index 76b021a..c6d60ea 100644 --- a/src/Command/ConfigureCommand.php +++ b/src/Command/ConfigureCommand.php @@ -4,6 +4,7 @@ use Meanbee\Magedbm2\Application\ConfigFileResolver; use Meanbee\Magedbm2\Application\ConfigInterface; +use Meanbee\Magedbm2\Service\FilesystemFactory; use Meanbee\Magedbm2\Service\FilesystemInterface; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; @@ -44,21 +45,22 @@ class ConfigureCommand extends BaseCommand /** * @param ConfigInterface $config - * @param FilesystemInterface $filesystem + * @param ConfigFileResolver $configFileResolver + * @param FilesystemFactory $filesystemFactory * @param Yaml $yaml * @param array|null $excluded_options */ public function __construct( ConfigInterface $config, ConfigFileResolver $configFileResolver, - FilesystemInterface $filesystem, + FilesystemFactory $filesystemFactory, Yaml $yaml, array $excluded_options = null ) { parent::__construct($config, self::NAME); $this->config = $config; - $this->filesystem = $filesystem; + $this->filesystem = $filesystemFactory->create(); $this->yaml = $yaml; if ($excluded_options) { diff --git a/src/Command/ExportCommand.php b/src/Command/ExportCommand.php index 40b7c12..8fa2726 100644 --- a/src/Command/ExportCommand.php +++ b/src/Command/ExportCommand.php @@ -5,7 +5,9 @@ use Meanbee\Magedbm2\Application\Config\Option; use Meanbee\Magedbm2\Application\ConfigInterface; use Meanbee\Magedbm2\Service\Anonymiser\Export; +use Meanbee\Magedbm2\Service\FilesystemFactory; use Meanbee\Magedbm2\Service\FilesystemInterface; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use Meanbee\Magedbm2\Shell\Command\Gzip; use Meanbee\Magedbm2\Shell\Command\Mysqldump; @@ -34,17 +36,17 @@ class ExportCommand extends BaseCommand /** * @param ConfigInterface $config - * @param StorageInterface $storage - * @param FilesystemInterface $filesystem + * @param StorageFactory $storageFactory + * @param FilesystemFactory $filesystemFactory */ - public function __construct(ConfigInterface $config, StorageInterface $storage, FilesystemInterface $filesystem) + public function __construct(ConfigInterface $config, StorageFactory $storageFactory, FilesystemFactory $filesystemFactory) { parent::__construct($config, self::NAME); $this->anonymiser = new Export(); - $this->storage = $storage; - $this->filesystem = $filesystem; + $this->storage = $storageFactory->create(); + $this->filesystem = $filesystemFactory->create(); - $storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA); + $this->storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA); $this->ensureServiceConfigurationValidated('storage', $this->storage); } diff --git a/src/Command/GetCommand.php b/src/Command/GetCommand.php index 9b990d5..8b28452 100644 --- a/src/Command/GetCommand.php +++ b/src/Command/GetCommand.php @@ -4,9 +4,12 @@ use Meanbee\Magedbm2\Application\ConfigInterface; use Meanbee\Magedbm2\Application\Config\Option; +use Meanbee\Magedbm2\Service\DatabaseFactory; use Meanbee\Magedbm2\Service\DatabaseInterface; +use Meanbee\Magedbm2\Service\FilesystemFactory; use Meanbee\Magedbm2\Service\FilesystemInterface; use Meanbee\Magedbm2\Exception\ServiceException; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputArgument; @@ -32,26 +35,24 @@ class GetCommand extends BaseCommand protected $filesystem; /** - * GetCommand constructor. - * * @param ConfigInterface $config - * @param DatabaseInterface $database - * @param StorageInterface $storage - * @param FilesystemInterface $filesystem + * @param DatabaseFactory $databaseFactory + * @param StorageFactory $storageFactory + * @param FilesystemFactory $filesystemFactory */ public function __construct( ConfigInterface $config, - DatabaseInterface $database, - StorageInterface $storage, - FilesystemInterface $filesystem + DatabaseFactory $databaseFactory, + StorageFactory $storageFactory, + FilesystemFactory $filesystemFactory ) { parent::__construct($config, self::NAME); - $this->database = $database; - $this->storage = $storage; - $this->filesystem = $filesystem; + $this->database = $databaseFactory->create(); + $this->storage = $storageFactory->create(); + $this->filesystem = $filesystemFactory->create(); - $storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE); + $this->storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE); $this->ensureServiceConfigurationValidated('storage', $this->storage); } diff --git a/src/Command/ImportCommand.php b/src/Command/ImportCommand.php index 0d64bb7..262e838 100644 --- a/src/Command/ImportCommand.php +++ b/src/Command/ImportCommand.php @@ -5,7 +5,9 @@ use Meanbee\Magedbm2\Application\Config\Option; use Meanbee\Magedbm2\Application\ConfigInterface; use Meanbee\Magedbm2\Exception\ConfigurationException; +use Meanbee\Magedbm2\Service\FilesystemFactory; use Meanbee\Magedbm2\Service\FilesystemInterface; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use Meanbee\Magedbm2\Shell\Command\Gunzip; use PDO; @@ -48,15 +50,16 @@ class ImportCommand extends BaseCommand /** * @param ConfigInterface $config - * @throws \Symfony\Component\Console\Exception\LogicException + * @param StorageFactory $storageFactory + * @param FilesystemFactory $filesystemFactory */ - public function __construct(ConfigInterface $config, StorageInterface $storage, FilesystemInterface $filesystem) + public function __construct(ConfigInterface $config, StorageFactory $storageFactory, FilesystemFactory $filesystemFactory) { parent::__construct($config, self::NAME); - $this->storage = $storage; - $this->filesystem = $filesystem; + $this->storage = $storageFactory->create(); + $this->filesystem = $filesystemFactory->create(); - $storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA); + $this->storage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA); $this->ensureServiceConfigurationValidated('storage', $this->storage); } diff --git a/src/Command/LsCommand.php b/src/Command/LsCommand.php index 63be68a..44356a2 100644 --- a/src/Command/LsCommand.php +++ b/src/Command/LsCommand.php @@ -5,6 +5,7 @@ use Meanbee\Magedbm2\Application\ConfigInterface; use Meanbee\Magedbm2\Exception\ServiceException; use Meanbee\Magedbm2\Service\Storage\Data\File; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -24,14 +25,18 @@ class LsCommand extends BaseCommand */ private $dataStorage; - public function __construct(ConfigInterface $config, StorageInterface $storage, StorageInterface $dataStorage) + /** + * @param ConfigInterface $config + * @param StorageFactory $storageFactory + */ + public function __construct(ConfigInterface $config, StorageFactory $storageFactory) { parent::__construct($config, self::NAME); - $this->storage = $storage; + $this->storage = $storageFactory->create(); $this->storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE); - $this->dataStorage = $dataStorage; + $this->dataStorage = $storageFactory->create(); $this->dataStorage->setPurpose(StorageInterface::PURPOSE_ANONYMISED_DATA); } diff --git a/src/Command/PutCommand.php b/src/Command/PutCommand.php index 13f3e5f..42167ff 100644 --- a/src/Command/PutCommand.php +++ b/src/Command/PutCommand.php @@ -5,9 +5,12 @@ use Meanbee\Magedbm2\Application\Config\Option; use Meanbee\Magedbm2\Application\ConfigInterface; use Meanbee\Magedbm2\Helper\TableGroupExpander; +use Meanbee\Magedbm2\Service\DatabaseFactory; use Meanbee\Magedbm2\Service\DatabaseInterface; +use Meanbee\Magedbm2\Service\FilesystemFactory; use Meanbee\Magedbm2\Service\FilesystemInterface; use Meanbee\Magedbm2\Exception\ServiceException; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -36,25 +39,25 @@ class PutCommand extends BaseCommand /** * @param ConfigInterface $config - * @param DatabaseInterface $database - * @param StorageInterface $storage - * @param FilesystemInterface $filesystem + * @param DatabaseFactory $databaseFactory + * @param StorageFactory $storageFactory + * @param FilesystemFactory $filesystemFactory * @param TableGroupExpander $tableGroupExpander */ public function __construct( ConfigInterface $config, - DatabaseInterface $database, - StorageInterface $storage, - FilesystemInterface $filesystem, + DatabaseFactory $databaseFactory, + StorageFactory $storageFactory, + FilesystemFactory $filesystemFactory, TableGroupExpander $tableGroupExpander = null ) { - $this->database = $database; - $this->storage = $storage; - $this->filesystem = $filesystem; + $this->database = $databaseFactory->create(); + $this->storage = $storageFactory->create(); + $this->filesystem = $filesystemFactory->create(); $this->tableExpander = $tableGroupExpander ?? new TableGroupExpander(); $this->config = $config; - $storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE); + $this->storage->setPurpose(StorageInterface::PURPOSE_STRIPPED_DATABASE); parent::__construct($config, self::NAME); diff --git a/src/Command/RmCommand.php b/src/Command/RmCommand.php index 2ce7842..21b957f 100644 --- a/src/Command/RmCommand.php +++ b/src/Command/RmCommand.php @@ -4,6 +4,7 @@ use Meanbee\Magedbm2\Application\ConfigInterface; use Meanbee\Magedbm2\Exception\ServiceException; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -24,11 +25,15 @@ class RmCommand extends BaseCommand /** @var StorageInterface */ protected $storage; - public function __construct(ConfigInterface $config, StorageInterface $storage) + /** + * @param ConfigInterface $config + * @param StorageFactory $storageFactory + */ + public function __construct(ConfigInterface $config, StorageFactory $storageFactory) { parent::__construct($config, self::NAME); - $this->storage = $storage; + $this->storage = $storageFactory->create(); $this->ensureServiceConfigurationValidated('storage', $this->storage); } diff --git a/src/Service/DatabaseFactory.php b/src/Service/DatabaseFactory.php index fae0d3d..1c8ccd4 100644 --- a/src/Service/DatabaseFactory.php +++ b/src/Service/DatabaseFactory.php @@ -7,21 +7,30 @@ class DatabaseFactory { /** - * @param Container $container + * @var Container + */ + private $container; + + public function __construct(Container $container) + { + $this->container = $container; + } + + /** * @return DatabaseInterface * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ - public static function create(Container $container) + public function create() { - switch ($container->get('database_adapter')) { + switch ($this->container->get('database_adapter')) { case 'shell': default: - $instance = $container->make(\Meanbee\Magedbm2\Service\Database\Shell::class); + $instance = $this->container->make(\Meanbee\Magedbm2\Service\Database\Shell::class); } if ($instance instanceof \Psr\Log\LoggerAwareInterface) { - $instance->setLogger($container->get('logger')); + $instance->setLogger($this->container->get('logger')); } return $instance; diff --git a/src/Service/FilesystemFactory.php b/src/Service/FilesystemFactory.php index 702f000..d726d1a 100644 --- a/src/Service/FilesystemFactory.php +++ b/src/Service/FilesystemFactory.php @@ -7,21 +7,30 @@ class FilesystemFactory { /** - * @param Container $container + * @var Container + */ + private $container; + + public function __construct(Container $container) + { + $this->container = $container; + } + + /** * @return FilesystemInterface * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ - public static function create(Container $container) + public function create() { - switch ($container->get('filesystem_adapter')) { + switch ($this->container->get('filesystem_adapter')) { case 'simple': default: - $instance = $container->make(\Meanbee\Magedbm2\Service\Filesystem\Simple::class); + $instance = $this->container->make(\Meanbee\Magedbm2\Service\Filesystem\Simple::class); } if ($instance instanceof \Psr\Log\LoggerAwareInterface) { - $instance->setLogger($container->get('logger')); + $instance->setLogger($this->container->get('logger')); } return $instance; diff --git a/src/Service/StorageFactory.php b/src/Service/StorageFactory.php index c9f323b..0f37f95 100644 --- a/src/Service/StorageFactory.php +++ b/src/Service/StorageFactory.php @@ -7,24 +7,33 @@ class StorageFactory { /** - * @param Container $container + * @var Container + */ + private $container; + + public function __construct(Container $container) + { + $this->container = $container; + } + + /** * @return StorageInterface * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ - public static function create(Container $container) + public function create() { - switch ($container->get('storage_adapter')) { + switch ($this->container->get('storage_adapter')) { case 's3': - $instance = $container->make(\Meanbee\Magedbm2\Service\Storage\S3::class); + $instance = $this->container->make(\Meanbee\Magedbm2\Service\Storage\S3::class); break; case 'local': default: - $instance = $container->make(\Meanbee\Magedbm2\Service\Storage\Local::class); + $instance = $this->container->make(\Meanbee\Magedbm2\Service\Storage\Local::class); } if ($instance instanceof \Psr\Log\LoggerAwareInterface) { - $instance->setLogger($container->get('logger')); + $instance->setLogger($this->container->get('logger')); } return $instance; diff --git a/tests/Command/AbstractCommandTest.php b/tests/Command/AbstractCommandTest.php index bf25b2f..2b1e008 100644 --- a/tests/Command/AbstractCommandTest.php +++ b/tests/Command/AbstractCommandTest.php @@ -3,13 +3,25 @@ namespace Meanbee\Magedbm2\Tests\Command; use Meanbee\Magedbm2\Application\ConfigInterface; +use Meanbee\Magedbm2\Service\DatabaseFactory; use Meanbee\Magedbm2\Service\DatabaseInterface; +use Meanbee\Magedbm2\Service\FilesystemFactory; use Meanbee\Magedbm2\Service\FilesystemInterface; +use Meanbee\Magedbm2\Service\StorageFactory; use Meanbee\Magedbm2\Service\StorageInterface; use PHPUnit\Framework\TestCase; abstract class AbstractCommandTest extends TestCase { + /** + * @param $instance + * @return \PHPUnit\Framework\MockObject\MockObject + */ + protected function getStorageFactoryMock($instance) + { + return $this->createFactoryMock(StorageFactory::class, $instance); + } + /** * @return \PHPUnit\Framework\MockObject\MockObject|StorageInterface */ @@ -18,6 +30,15 @@ protected function getStorageMock() return $this->createMock(StorageInterface::class); } + /** + * @param $instance + * @return \PHPUnit\Framework\MockObject\MockObject + */ + protected function getFilesystemFactoryMock($instance) + { + return $this->createFactoryMock(FilesystemFactory::class, $instance); + } + /** * @return \PHPUnit\Framework\MockObject\MockObject|FilesystemInterface */ @@ -26,6 +47,15 @@ protected function getFilesystemMock() return $this->createMock(FilesystemInterface::class); } + /** + * @param $instance + * @return \PHPUnit\Framework\MockObject\MockObject + */ + protected function getDatabaseFactoryMock($instance) + { + return $this->createFactoryMock(DatabaseFactory::class, $instance); + } + /** * @return \PHPUnit\Framework\MockObject\MockObject|DatabaseInterface */ @@ -41,4 +71,23 @@ protected function getConfigMock() { return $this->createMock(ConfigInterface::class); } + + /** + * @param $class + * @param $instance + * @return \PHPUnit\Framework\MockObject\MockObject + */ + protected function createFactoryMock($class, $instance) + { + $mock = $this->createMock($class); + + if (!is_array($instance)) { + $instance = [$instance]; + } + + $mock->method('create') + ->willReturnOnConsecutiveCalls(...$instance); + + return $mock; + } } diff --git a/tests/Command/ConfigureCommandTest.php b/tests/Command/ConfigureCommandTest.php index b93215f..77c41ca 100644 --- a/tests/Command/ConfigureCommandTest.php +++ b/tests/Command/ConfigureCommandTest.php @@ -134,7 +134,7 @@ protected function getCommandTester($filesystem) $configResolver->method('getUserFilePath') ->willReturn($this->configPath); - $command = new ConfigureCommand($config, $configResolver, $filesystem, new Yaml()); + $command = new ConfigureCommand($config, $configResolver, $this->getFilesystemFactoryMock($filesystem), new Yaml()); $command->setApplication($application); return new CommandTester($command); diff --git a/tests/Command/GetCommandTest.php b/tests/Command/GetCommandTest.php index 96cc9de..4c11ebd 100644 --- a/tests/Command/GetCommandTest.php +++ b/tests/Command/GetCommandTest.php @@ -150,7 +150,7 @@ public function testLatestFile() */ protected function getCommandTester($database, $storage, $filesystem, $confirmation = false) { - $command = new GetCommand($this->getConfigMock(), $database, $storage, $filesystem); + $command = new GetCommand($this->getConfigMock(), $this->getDatabaseFactoryMock($database), $this->getStorageFactoryMock($storage), $this->getFilesystemFactoryMock($filesystem)); $helper = $this->createMock(QuestionHelper::class); $helper diff --git a/tests/Command/LsCommandTest.php b/tests/Command/LsCommandTest.php index 482409f..b47e1b7 100644 --- a/tests/Command/LsCommandTest.php +++ b/tests/Command/LsCommandTest.php @@ -117,7 +117,7 @@ public function testListFiles() */ protected function getCommandTester($storage, $dataStorage) { - $command = new LsCommand($this->getConfigMock(), $storage, $dataStorage); + $command = new LsCommand($this->getConfigMock(), $this->getStorageFactoryMock([$storage, $dataStorage])); return new CommandTester($command); } diff --git a/tests/Command/PutCommandTest.php b/tests/Command/PutCommandTest.php index 83f5471..925121f 100644 --- a/tests/Command/PutCommandTest.php +++ b/tests/Command/PutCommandTest.php @@ -158,7 +158,7 @@ protected function getCommandTester($config = null, $database = null, $storage = $filesystem = $filesystem ?? $this->createMock(FilesystemInterface::class); $tableexpander = $tableexpander ?? new TableGroupExpander(); - $command = new PutCommand($config, $database, $storage, $filesystem, $tableexpander); + $command = new PutCommand($config, $this->getDatabaseFactoryMock($database), $this->getStorageFactoryMock($storage), $this->getFilesystemFactoryMock($filesystem) , $tableexpander); return new CommandTester($command); } diff --git a/tests/Command/RmCommandTest.php b/tests/Command/RmCommandTest.php index 441178f..de4112f 100644 --- a/tests/Command/RmCommandTest.php +++ b/tests/Command/RmCommandTest.php @@ -43,7 +43,7 @@ public function testDelete() */ protected function getCommandTester($storage) { - $command = new RmCommand($this->getConfigMock(), $storage); + $command = new RmCommand($this->getConfigMock(), $this->getStorageFactoryMock($storage)); return new CommandTester($command); }