From 90ace1025dca2670a04508173ea515c08443dbfd Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 7 May 2024 14:21:17 +0200 Subject: [PATCH] [FEATURE] Add command controller to create test data Related: #11120 --- Classes/Command/CreateTestDataCommand.php | 79 +++++++++++++++++++++++ Configuration/Services.php | 10 +++ 2 files changed, 89 insertions(+) create mode 100644 Classes/Command/CreateTestDataCommand.php diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php new file mode 100644 index 0000000..b40d55b --- /dev/null +++ b/Classes/Command/CreateTestDataCommand.php @@ -0,0 +1,79 @@ + 'Darjeeling', + 'description' => 'I love that tea!', + 'sys_language_uid' => 0 + ], + [ + 'title' => 'Earl Grey', + 'description' => 'A nice tea!', + 'sys_language_uid' => 0 + ] + ]; + protected function configure(): void + { + $this + ->setHelp('Create test data for the tea extension in an already existing page (sysfolder).') + ->addArgument( + 'pageId', + InputArgument::REQUIRED, + 'Existing sysfolder page id.' + ) + ->addOption( + 'delete-data-before', + 'd', + InputOption::VALUE_NONE, + 'Delete all tea data in the defined pid before creating new data.' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + /** @var integer $pageId */ + $pageId = $input->getArgument('pageId') ?? 0; + /** @var boolean $deleteDataBefore */ + $deleteDataBefore = $input->getOption('delete-data-before') ?? false; + $table = 'tx_tea_domain_model_tea'; + $connectionForTable = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table); + + if($deleteDataBefore) { + $query = $connectionForTable; + $query->delete($table, ['pid' => $pageId], [Connection::PARAM_INT]); + $output->writeln(sprintf('Existing data in page %s deleted.',$pageId)); + } + + $query = $connectionForTable; + foreach ($this->teaData as $item) { + $item = ['pid' => $pageId, ...$item]; + $query->insert($table, + $item + ); + } + $output->writeln(sprintf('Test data in page %s created.', $pageId)); + + return Command::SUCCESS; + } +} diff --git a/Configuration/Services.php b/Configuration/Services.php index f0f766f..802bfa3 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use TTN\Tea\Command\CreateTestDataCommand; + return static function (ContainerConfigurator $containerConfigurator) { $services = $containerConfigurator->services() ->defaults() @@ -12,4 +14,12 @@ return static function (ContainerConfigurator $containerConfigurator) { $services->load('TTN\\Tea\\', '../Classes/*') ->exclude('../Classes/Domain/Model/*'); + + $services->set(CreateTestDataCommand::class) + ->tag('console.command', [ + 'command' => 'tea:createtestdata', + 'description'=>'Create test data in existing sysfolder' + ] + ); + };