From 44cfc8249cacb265421bf7e69326c92cecee6c03 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 7 May 2024 14:21:17 +0200 Subject: [PATCH 01/23] [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 00000000..b40d55b7 --- /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 f0f766fd..802bfa39 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -4,6 +4,8 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use TTN\Tea\Command\CreateTestDataCommand; + return static function (ContainerConfigurator $containerConfigurator) { $services = $containerConfigurator->services() ->defaults() @@ -12,4 +14,12 @@ $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' + ] + ); + }; From aa624b297d3555e7b458e8c926ac470a82ef94a0 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Fri, 26 Jul 2024 08:35:31 +0200 Subject: [PATCH 02/23] [TASK] Remove phpDocumentor reflection type Related: #1120 --- Classes/Command/CreateTestDataCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index b40d55b7..13d0885c 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -8,7 +8,6 @@ * Command to create test data for the tea extension. */ -use phpDocumentor\Reflection\Types\Integer; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; From a067246c7ebfe76e6f0f68d5928a2b2ae9e17a8b Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Fri, 26 Jul 2024 09:39:27 +0200 Subject: [PATCH 03/23] [DOCS] Add documentation part for command controller Related: #1120 --- Documentation/CommandController.rst | 28 ++++++++++++++++++++++++++++ Documentation/Index.rst | 1 + 2 files changed, 29 insertions(+) create mode 100644 Documentation/CommandController.rst diff --git a/Documentation/CommandController.rst b/Documentation/CommandController.rst new file mode 100644 index 00000000..092cad02 --- /dev/null +++ b/Documentation/CommandController.rst @@ -0,0 +1,28 @@ +.. include:: /Includes.rst.txt + +.. _command-controller: + +================== +Command Controller +================== + +The "tea" extension comes with a CommandController that can be used for the +automatic creation of test data. It also serves to illustrate how data can be +created in the database using a command controller. + +You must set a page id as argument. Therefore it's necessary to create an +sysfolder before. + +You can add option `-d` to delete already existing data. + + +.. code-block:: bash + + vendor/bin/typo3 tea:createtestdata 3 + + +.. seealso:: + + For further details to Console Commands read the + :ref:`Creating a basic command ` + tutorial. diff --git a/Documentation/Index.rst b/Documentation/Index.rst index 0bcfdb5c..84aac666 100644 --- a/Documentation/Index.rst +++ b/Documentation/Index.rst @@ -47,6 +47,7 @@ continuous integration. ReleaseBranchingStrategy Environment DependencyManager + CommandController Running ContinuousIntegration Documentation From 8e3de9302b0ac220dccd7ee69d8c43e14cf69971 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 17:21:27 +0200 Subject: [PATCH 04/23] [BUGFIX] Fix errors from php cs fixer Related: #1120 --- Classes/Command/CreateTestDataCommand.php | 17 +++++++++-------- Configuration/Services.php | 7 ++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 13d0885c..39e00528 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -24,13 +24,13 @@ final class CreateTestDataCommand extends Command [ 'title' => 'Darjeeling', 'description' => 'I love that tea!', - 'sys_language_uid' => 0 + 'sys_language_uid' => 0, ], [ 'title' => 'Earl Grey', 'description' => 'A nice tea!', - 'sys_language_uid' => 0 - ] + 'sys_language_uid' => 0, + ], ]; protected function configure(): void { @@ -51,23 +51,24 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var integer $pageId */ + /** @var int $pageId */ $pageId = $input->getArgument('pageId') ?? 0; - /** @var boolean $deleteDataBefore */ + /** @var bool $deleteDataBefore */ $deleteDataBefore = $input->getOption('delete-data-before') ?? false; $table = 'tx_tea_domain_model_tea'; $connectionForTable = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table); - if($deleteDataBefore) { + if ($deleteDataBefore) { $query = $connectionForTable; $query->delete($table, ['pid' => $pageId], [Connection::PARAM_INT]); - $output->writeln(sprintf('Existing data in page %s deleted.',$pageId)); + $output->writeln(sprintf('Existing data in page %s deleted.', $pageId)); } $query = $connectionForTable; foreach ($this->teaData as $item) { $item = ['pid' => $pageId, ...$item]; - $query->insert($table, + $query->insert( + $table, $item ); } diff --git a/Configuration/Services.php b/Configuration/Services.php index 802bfa39..262c2108 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -16,10 +16,11 @@ ->exclude('../Classes/Domain/Model/*'); $services->set(CreateTestDataCommand::class) - ->tag('console.command', [ + ->tag( + 'console.command', + [ 'command' => 'tea:createtestdata', - 'description'=>'Create test data in existing sysfolder' + 'description' => 'Create test data in existing sysfolder', ] ); - }; From 41962da203b341e11a6042ec1e16fe7b95381ad3 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:38:54 +0200 Subject: [PATCH 05/23] Update Classes/Command/CreateTestDataCommand.php Co-authored-by: Oliver Klee --- Classes/Command/CreateTestDataCommand.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 39e00528..966b0117 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -19,7 +19,9 @@ final class CreateTestDataCommand extends Command { - /** @phpstan-ignore-next-line */ + /** + * @var list}> + */ protected array $teaData = [ [ 'title' => 'Darjeeling', From 393e55f22091aa5efbfe91b3304f3d1f5d7d44b5 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:39:11 +0200 Subject: [PATCH 06/23] Update Classes/Command/CreateTestDataCommand.php Co-authored-by: Oliver Klee --- Classes/Command/CreateTestDataCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 966b0117..b29b40c9 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -34,6 +34,7 @@ final class CreateTestDataCommand extends Command 'sys_language_uid' => 0, ], ]; + protected function configure(): void { $this From 91f27b048bea42bbf50f4f688a169812256d873a Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:44:06 +0200 Subject: [PATCH 07/23] [TASK] Move comment directly above the class Related #1120 --- Classes/Command/CreateTestDataCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index b29b40c9..2659173f 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -4,10 +4,6 @@ namespace TTN\Tea\Command; -/* - * Command to create test data for the tea extension. - */ - use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -17,6 +13,9 @@ use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Utility\GeneralUtility; +/* + * Command to create test data for the tea extension. + */ final class CreateTestDataCommand extends Command { /** From a430e320ae7804fe4105d3489397f48dec95cc46 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:48:34 +0200 Subject: [PATCH 08/23] [TASK] Rename pageId to pageUid Related #1120 --- Classes/Command/CreateTestDataCommand.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 2659173f..77cdb8c4 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -39,7 +39,7 @@ protected function configure(): void $this ->setHelp('Create test data for the tea extension in an already existing page (sysfolder).') ->addArgument( - 'pageId', + 'pageUid', InputArgument::REQUIRED, 'Existing sysfolder page id.' ) @@ -53,8 +53,8 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var int $pageId */ - $pageId = $input->getArgument('pageId') ?? 0; + /** @var int $pageUid */ + $pageUid = $input->getArgument('pageUid') ?? 0; /** @var bool $deleteDataBefore */ $deleteDataBefore = $input->getOption('delete-data-before') ?? false; $table = 'tx_tea_domain_model_tea'; @@ -62,19 +62,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($deleteDataBefore) { $query = $connectionForTable; - $query->delete($table, ['pid' => $pageId], [Connection::PARAM_INT]); - $output->writeln(sprintf('Existing data in page %s deleted.', $pageId)); + $query->delete($table, ['pid' => $pageUid], [Connection::PARAM_INT]); + $output->writeln(sprintf('Existing data in page %s deleted.', $pageUid)); } $query = $connectionForTable; foreach ($this->teaData as $item) { - $item = ['pid' => $pageId, ...$item]; + $item = ['pid' => $pageUid, ...$item]; $query->insert( $table, $item ); } - $output->writeln(sprintf('Test data in page %s created.', $pageId)); + $output->writeln(sprintf('Test data in page %s created.', $pageUid)); return Command::SUCCESS; } From d47b5908fac34ca493f28a6cb9e3915901e6b143 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 11:24:13 +0200 Subject: [PATCH 09/23] [TASK] use assert to state data restrictions Related #1120 --- Classes/Command/CreateTestDataCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 77cdb8c4..f6ef35f0 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -53,10 +53,10 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var int $pageUid */ - $pageUid = $input->getArgument('pageUid') ?? 0; - /** @var bool $deleteDataBefore */ + $pageUid = (int)$input->getArgument('pageUid') ?? 0; + \assert(\is_int($pageUid)); $deleteDataBefore = $input->getOption('delete-data-before') ?? false; + \assert(\is_bool($deleteDataBefore)); $table = 'tx_tea_domain_model_tea'; $connectionForTable = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table); From 05c4c1a8f65fa9781ec881995ec539eabe4c230e Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 12:32:54 +0200 Subject: [PATCH 10/23] [TASK] Update reference index after changing test data Related #1120 --- Classes/Command/CreateTestDataCommand.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index f6ef35f0..d7d01732 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -4,13 +4,19 @@ namespace TTN\Tea\Command; +use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; +use TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener; +use TYPO3\CMS\Backend\Command\ReferenceIndexUpdateCommand; +use TYPO3\CMS\Core\Core\Bootstrap; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\ReferenceIndex; use TYPO3\CMS\Core\Utility\GeneralUtility; /* @@ -76,6 +82,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $output->writeln(sprintf('Test data in page %s created.', $pageUid)); + $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class); + $referenceIndex->updateIndex(0); + $output->writeln('Reference index updated.'); + return Command::SUCCESS; } } From e96cdb03f1705b65c2364f7a4ff6bdb447ee7ce5 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 19:11:47 +0200 Subject: [PATCH 11/23] [TASK] Remove unnecessary is_int check of pageUid Related #1120 --- Classes/Command/CreateTestDataCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index d7d01732..fe23ceef 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -60,7 +60,6 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $pageUid = (int)$input->getArgument('pageUid') ?? 0; - \assert(\is_int($pageUid)); $deleteDataBefore = $input->getOption('delete-data-before') ?? false; \assert(\is_bool($deleteDataBefore)); $table = 'tx_tea_domain_model_tea'; From 9c3e7ef6c791a16420d3f2658bbd4f87831e7128 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 19:13:44 +0200 Subject: [PATCH 12/23] [TASK] Add functional test for createTestDataCommand Related #1120 --- .../Command/CreateTestDataCommandTest.php | 106 ++++++++++++++++++ .../Command/Fixtures/Database/OtherTeas.csv | 6 + .../Command/Fixtures/Database/Pages.csv | 4 + .../Command/Fixtures/Database/Teas.csv | 4 + .../Fixtures/Database/TeasAfterDelete.csv | 4 + 5 files changed, 124 insertions(+) create mode 100644 Tests/Functional/Command/CreateTestDataCommandTest.php create mode 100644 Tests/Functional/Command/Fixtures/Database/OtherTeas.csv create mode 100644 Tests/Functional/Command/Fixtures/Database/Pages.csv create mode 100644 Tests/Functional/Command/Fixtures/Database/Teas.csv create mode 100644 Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php new file mode 100644 index 00000000..30bdb09b --- /dev/null +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -0,0 +1,106 @@ +importCSVDataSet(__DIR__ . '/Fixtures/Database/Pages.csv'); + $this->subject = new CreateTestDataCommand(self::COMMAND_NAME); + $application = new Application(); + $application->add($this->subject); + + $command = $application->find('tea:createtestdata'); + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function isConsoleCommand(): void + { + self::assertInstanceOf(Command::class, $this->subject); + } + + /** + * @test + */ + public function hasDescription(): void + { + $expected = 'Create test data for the tea extension in an already existing page (sysfolder).'; + self::assertSame($expected, $this->subject->getHelp()); + } + + /** + * @test + */ + public function hasHelpText(): void + { + $expected = 'Create test data for the tea extension in an already existing page (sysfolder).'; + self::assertSame($expected, $this->subject->getHelp()); + } + + /** + * @test + */ + public function runReturnsSuccessStatus(): void + { + $result = $this->commandTester->execute( + [ + 'pageUid' => '1', + ], + ); + + self::assertSame(Command::SUCCESS, $result); + } + + /** + * @test + */ + public function testDataGetsCreated(): void + { + $this->commandTester->execute([ + 'pageUid' => '1', + ]); + + self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/Teas.csv'); + } + + /** + * @test + */ + public function testDataGetsDeletedBeforeNewDataCreated(): void + { + $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/Teas.csv'); + $this->commandTester->execute( + [ + 'pageUid' => '1', + '--delete-data-before' => true, + ] + ); + + self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDelete.csv'); + } + +} diff --git a/Tests/Functional/Command/Fixtures/Database/OtherTeas.csv b/Tests/Functional/Command/Fixtures/Database/OtherTeas.csv new file mode 100644 index 00000000..fab4071c --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/OtherTeas.csv @@ -0,0 +1,6 @@ +"tx_tea_domain_model_tea" +,"uid","pid","title","description","deleted" +,3,1,"Darjeeling","I love that tea!","0" +,4,1,"Earl Grey","A nice tea!","0" +,1,2,"Black tea","I hate that.","0" +,2,2,"Green tea","Is ok.","0" diff --git a/Tests/Functional/Command/Fixtures/Database/Pages.csv b/Tests/Functional/Command/Fixtures/Database/Pages.csv new file mode 100644 index 00000000..f4878ebe --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/Pages.csv @@ -0,0 +1,4 @@ +"pages",,,,,,,,, +,"uid","pid","sorting","deleted","t3_origuid","title",,, +,1,0,256,0,0,"Tea data",,, +,2,0,512,0,0,"Other tea data",,, diff --git a/Tests/Functional/Command/Fixtures/Database/Teas.csv b/Tests/Functional/Command/Fixtures/Database/Teas.csv new file mode 100644 index 00000000..40e686b6 --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/Teas.csv @@ -0,0 +1,4 @@ +"tx_tea_domain_model_tea" +,"uid","pid","title","description","deleted" +,1,1,"Darjeeling","I love that tea!",0 +,2,1,"Earl Grey","A nice tea!",0 diff --git a/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv b/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv new file mode 100644 index 00000000..3967dcd9 --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv @@ -0,0 +1,4 @@ +"tx_tea_domain_model_tea" +,"uid","pid","title","description","deleted" +,3,1,"Darjeeling","I love that tea!",0 +,4,1,"Earl Grey","A nice tea!",0 From fde77e6f911603dbf9e16814f849cd64d1414ebd Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 30 Jul 2024 19:51:15 +0200 Subject: [PATCH 13/23] Fix broken functional test SQLite does reset the UID autoincrement during deletion. That way the assertions do not match. We now use a different set up and remove the uids from assertions. That way we still ensure that the imported data is removed and new data is imported. We do not need to care about UID. --- Tests/Functional/Command/CreateTestDataCommandTest.php | 2 +- Tests/Functional/Command/Fixtures/Database/ExistingTeas.csv | 4 ++++ .../Command/Fixtures/Database/TeasAfterDelete.csv | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/Command/Fixtures/Database/ExistingTeas.csv diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php index 30bdb09b..400a5432 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -92,7 +92,7 @@ public function testDataGetsCreated(): void */ public function testDataGetsDeletedBeforeNewDataCreated(): void { - $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/Teas.csv'); + $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/ExistingTeas.csv'); $this->commandTester->execute( [ 'pageUid' => '1', diff --git a/Tests/Functional/Command/Fixtures/Database/ExistingTeas.csv b/Tests/Functional/Command/Fixtures/Database/ExistingTeas.csv new file mode 100644 index 00000000..e1dd287f --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/ExistingTeas.csv @@ -0,0 +1,4 @@ +"tx_tea_domain_model_tea" +,"uid","pid","title","description","deleted" +,1,1,"Darjeeling","Which already existed in the system",0 +,2,1,"Earl Grey","Which already existed in the system",0 diff --git a/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv b/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv index 3967dcd9..c0a2aecf 100644 --- a/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv +++ b/Tests/Functional/Command/Fixtures/Database/TeasAfterDelete.csv @@ -1,4 +1,4 @@ "tx_tea_domain_model_tea" -,"uid","pid","title","description","deleted" -,3,1,"Darjeeling","I love that tea!",0 -,4,1,"Earl Grey","A nice tea!",0 +,"pid","title","description","deleted" +,1,"Darjeeling","I love that tea!",0 +,1,"Earl Grey","A nice tea!",0 From a928788d3245a125e376c257f0181e4d0916790c Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 20:19:18 +0200 Subject: [PATCH 14/23] [TASK] Check that existing teas in other pids were not deleted Related #1120 --- .../Command/CreateTestDataCommandTest.php | 15 +++++++++++++++ .../Fixtures/Database/OtherExistingTeas.csv | 6 ++++++ .../Command/Fixtures/Database/OtherTeas.csv | 6 ------ .../Database/TeasAfterDeleteOtherExistingTeas.csv | 6 ++++++ 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 Tests/Functional/Command/Fixtures/Database/OtherExistingTeas.csv delete mode 100644 Tests/Functional/Command/Fixtures/Database/OtherTeas.csv create mode 100644 Tests/Functional/Command/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php index 400a5432..0f28823b 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -103,4 +103,19 @@ public function testDataGetsDeletedBeforeNewDataCreated(): void self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDelete.csv'); } + /** + * @test + */ + public function existingDataWillBeNotDeleted(): void + { + $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/OtherExistingTeas.csv'); + $this->commandTester->execute( + [ + 'pageUid' => '1', + '--delete-data-before' => true, + ] + ); + self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv'); + } + } diff --git a/Tests/Functional/Command/Fixtures/Database/OtherExistingTeas.csv b/Tests/Functional/Command/Fixtures/Database/OtherExistingTeas.csv new file mode 100644 index 00000000..f78bd2ec --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/OtherExistingTeas.csv @@ -0,0 +1,6 @@ +"tx_tea_domain_model_tea" +,"uid","pid","title","description","deleted" +,3,1,"Darjeeling","Exists and should be deleted","0" +,4,1,"Earl Grey","Exists and should be deleted","0" +,1,2,"Darjeeling","Which already existed in the system",0 +,2,2,"Earl Grey","Which already existed in the system",0 diff --git a/Tests/Functional/Command/Fixtures/Database/OtherTeas.csv b/Tests/Functional/Command/Fixtures/Database/OtherTeas.csv deleted file mode 100644 index fab4071c..00000000 --- a/Tests/Functional/Command/Fixtures/Database/OtherTeas.csv +++ /dev/null @@ -1,6 +0,0 @@ -"tx_tea_domain_model_tea" -,"uid","pid","title","description","deleted" -,3,1,"Darjeeling","I love that tea!","0" -,4,1,"Earl Grey","A nice tea!","0" -,1,2,"Black tea","I hate that.","0" -,2,2,"Green tea","Is ok.","0" diff --git a/Tests/Functional/Command/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv b/Tests/Functional/Command/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv new file mode 100644 index 00000000..a427bf08 --- /dev/null +++ b/Tests/Functional/Command/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv @@ -0,0 +1,6 @@ +"tx_tea_domain_model_tea" +,"pid","title","description","deleted" +,1,"Darjeeling","I love that tea!",0 +,1,"Earl Grey","A nice tea!",0 +,2,"Darjeeling","Which already existed in the system",0 +,2,"Earl Grey","Which already existed in the system",0 From 06e39b39f19e67302faf9c15b853ca2b19a5792f Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 20:30:50 +0200 Subject: [PATCH 15/23] [TASK] Rename test functions Related #1120 --- Tests/Functional/Command/CreateTestDataCommandTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php index 0f28823b..0cffd5bb 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -78,7 +78,7 @@ public function runReturnsSuccessStatus(): void /** * @test */ - public function testDataGetsCreated(): void + public function createsTestData(): void { $this->commandTester->execute([ 'pageUid' => '1', @@ -90,7 +90,7 @@ public function testDataGetsCreated(): void /** * @test */ - public function testDataGetsDeletedBeforeNewDataCreated(): void + public function deletesExistingDataOnGivenPidBeforeCreatingNewData(): void { $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/ExistingTeas.csv'); $this->commandTester->execute( @@ -106,7 +106,7 @@ public function testDataGetsDeletedBeforeNewDataCreated(): void /** * @test */ - public function existingDataWillBeNotDeleted(): void + public function doesNotDeleteDataOnOtherPid(): void { $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/OtherExistingTeas.csv'); $this->commandTester->execute( @@ -115,7 +115,7 @@ public function existingDataWillBeNotDeleted(): void '--delete-data-before' => true, ] ); + self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv'); } - } From 77f9b5c04b87bcc4edad9ba172bdef8f5acb9e05 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 20:41:05 +0200 Subject: [PATCH 16/23] [TASK] Remove unused imports Related #1120 --- Classes/Command/CreateTestDataCommand.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index fe23ceef..201c034c 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -4,16 +4,11 @@ namespace TTN\Tea\Command; -use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; -use TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener; -use TYPO3\CMS\Backend\Command\ReferenceIndexUpdateCommand; -use TYPO3\CMS\Core\Core\Bootstrap; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ReferenceIndex; From d8fc929075d2e4064cf3fe4a41d62cb47e61be50 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 20 Aug 2024 08:49:30 +0200 Subject: [PATCH 17/23] [BUGFIX] Fix some php stan errors Related #1120 --- Classes/Command/CreateTestDataCommand.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 201c034c..6cc8d2a5 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -28,7 +28,7 @@ final class CreateTestDataCommand extends Command 'description' => 'I love that tea!', 'sys_language_uid' => 0, ], - [ + [ 'title' => 'Earl Grey', 'description' => 'A nice tea!', 'sys_language_uid' => 0, @@ -54,7 +54,8 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $pageUid = (int)$input->getArgument('pageUid') ?? 0; + $pageUid = $input->getArgument('pageUid') ?? 0; + \assert(\is_int($pageUid)); $deleteDataBefore = $input->getOption('delete-data-before') ?? false; \assert(\is_bool($deleteDataBefore)); $table = 'tx_tea_domain_model_tea'; @@ -77,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln(sprintf('Test data in page %s created.', $pageUid)); $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class); - $referenceIndex->updateIndex(0); + $referenceIndex->updateIndex(false); $output->writeln('Reference index updated.'); return Command::SUCCESS; From ed08a12146c17bc7c975c9be80067bae07978272 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 20 Aug 2024 09:15:23 +0200 Subject: [PATCH 18/23] [BUGFIX] Fix error with spread operator in php 7.4 In php 7.4 error occured: `Cannot unpack array with string keys` Related #1120 --- Classes/Command/CreateTestDataCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 6cc8d2a5..7e17cc7e 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $query = $connectionForTable; foreach ($this->teaData as $item) { - $item = ['pid' => $pageUid, ...$item]; + $item = ['pid' => $pageUid, 'title' => $item['title'], 'description' => $item['description']]; $query->insert( $table, $item From 745f0433b9f55b79d8a3928f966a7939a8c748df Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Fri, 13 Dec 2024 17:20:31 +0100 Subject: [PATCH 19/23] [BUGFIX] Remove assert of int for the page id functional test are failing all with that assert Related: #1120 --- Classes/Command/CreateTestDataCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 7e17cc7e..16123e51 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -54,8 +54,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $pageUid = $input->getArgument('pageUid') ?? 0; - \assert(\is_int($pageUid)); + $pageUid = (int)$input->getArgument('pageUid') ?? 0; $deleteDataBefore = $input->getOption('delete-data-before') ?? false; \assert(\is_bool($deleteDataBefore)); $table = 'tx_tea_domain_model_tea'; From b05bbb7a4ca0badf7f0b93a95d66abcb308676c7 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Fri, 13 Dec 2024 17:51:34 +0100 Subject: [PATCH 20/23] [BUGFIX] Refactor that pageUid stuff with assert and type of int With the usage of an integer in functional test for the command we get rid of the issue with asert is int or cast mixed variable to int. Test are running fine locally. The page id should be just an integer and nobody will be set that in '' right? Related: #1120 --- Classes/Command/CreateTestDataCommand.php | 3 ++- Tests/Functional/Command/CreateTestDataCommandTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 16123e51..7e17cc7e 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -54,7 +54,8 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $pageUid = (int)$input->getArgument('pageUid') ?? 0; + $pageUid = $input->getArgument('pageUid') ?? 0; + \assert(\is_int($pageUid)); $deleteDataBefore = $input->getOption('delete-data-before') ?? false; \assert(\is_bool($deleteDataBefore)); $table = 'tx_tea_domain_model_tea'; diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php index 0cffd5bb..00e12e50 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -68,7 +68,7 @@ public function runReturnsSuccessStatus(): void { $result = $this->commandTester->execute( [ - 'pageUid' => '1', + 'pageUid' => 1, ], ); @@ -81,7 +81,7 @@ public function runReturnsSuccessStatus(): void public function createsTestData(): void { $this->commandTester->execute([ - 'pageUid' => '1', + 'pageUid' => 1, ]); self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/Teas.csv'); @@ -95,7 +95,7 @@ public function deletesExistingDataOnGivenPidBeforeCreatingNewData(): void $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/ExistingTeas.csv'); $this->commandTester->execute( [ - 'pageUid' => '1', + 'pageUid' => 1, '--delete-data-before' => true, ] ); @@ -111,7 +111,7 @@ public function doesNotDeleteDataOnOtherPid(): void $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/OtherExistingTeas.csv'); $this->commandTester->execute( [ - 'pageUid' => '1', + 'pageUid' => 1, '--delete-data-before' => true, ] ); From 5ca9abe7848ec491baafef1cd6c2c39b3bc2d854 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 16 Dec 2024 10:43:21 +0100 Subject: [PATCH 21/23] [TASK] Ignore error with missing type for constant Related: #1120 --- phpstan.neon | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 58d6f6de..aabb7d18 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -53,3 +53,7 @@ parameters: - '$_FILES' - '$_SERVER' message: 'Use PSR-7 API instead' + ignoreErrors: + - + message: '#Out of 1 possible constant types#' + path: Tests/Functional/Command/CreateTestDataCommandTest.php From 5207568dd64a73f900333263c09b345cbb358546 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 16 Dec 2024 13:35:24 +0100 Subject: [PATCH 22/23] [TASK] Rename command to improve readability Related: #1120 --- Configuration/Services.php | 2 +- Documentation/CommandController.rst | 2 +- Tests/Functional/Command/CreateTestDataCommandTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Configuration/Services.php b/Configuration/Services.php index 262c2108..15370d6e 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -19,7 +19,7 @@ ->tag( 'console.command', [ - 'command' => 'tea:createtestdata', + 'command' => 'tea:create-test-data', 'description' => 'Create test data in existing sysfolder', ] ); diff --git a/Documentation/CommandController.rst b/Documentation/CommandController.rst index 092cad02..5ed36d98 100644 --- a/Documentation/CommandController.rst +++ b/Documentation/CommandController.rst @@ -18,7 +18,7 @@ You can add option `-d` to delete already existing data. .. code-block:: bash - vendor/bin/typo3 tea:createtestdata 3 + vendor/bin/typo3 tea:create-test-data 3 .. seealso:: diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php index 00e12e50..67ecaa91 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -15,7 +15,7 @@ */ class CreateTestDataCommandTest extends FunctionalTestCase { - private const COMMAND_NAME = 'tea:createtestdata'; + private const COMMAND_NAME = 'tea:create-test-data'; protected array $testExtensionsToLoad = ['ttn/tea']; @@ -31,7 +31,7 @@ protected function setUp(): void $application = new Application(); $application->add($this->subject); - $command = $application->find('tea:createtestdata'); + $command = $application->find('tea:create-test-data'); $this->commandTester = new CommandTester($command); } From c7f3aff1217e7436a74b92a0560b5045f45fd520 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sat, 21 Dec 2024 18:57:10 +0100 Subject: [PATCH 23/23] Some polish after review --- Classes/Command/CreateTestDataCommand.php | 7 ++----- .../Functional/Command/CreateTestDataCommandTest.php | 11 +++++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Classes/Command/CreateTestDataCommand.php b/Classes/Command/CreateTestDataCommand.php index 7e17cc7e..ac5247ec 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -70,12 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $query = $connectionForTable; foreach ($this->teaData as $item) { $item = ['pid' => $pageUid, 'title' => $item['title'], 'description' => $item['description']]; - $query->insert( - $table, - $item - ); + $query->insert($table, $item); } - $output->writeln(sprintf('Test data in page %s created.', $pageUid)); + $output->writeln(\sprintf('Test data in page %s created.', $pageUid)); $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class); $referenceIndex->updateIndex(false); diff --git a/Tests/Functional/Command/CreateTestDataCommandTest.php b/Tests/Functional/Command/CreateTestDataCommandTest.php index 67ecaa91..00c558c5 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -13,8 +13,11 @@ /** * @covers \TTN\Tea\Command\CreateTestDataCommand */ -class CreateTestDataCommandTest extends FunctionalTestCase +final class CreateTestDataCommandTest extends FunctionalTestCase { + /** + * @var non-empty-string + */ private const COMMAND_NAME = 'tea:create-test-data'; protected array $testExtensionsToLoad = ['ttn/tea']; @@ -84,7 +87,7 @@ public function createsTestData(): void 'pageUid' => 1, ]); - self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/Teas.csv'); + $this->assertCSVDataSet(__DIR__ . '/Fixtures/Database/Teas.csv'); } /** @@ -100,7 +103,7 @@ public function deletesExistingDataOnGivenPidBeforeCreatingNewData(): void ] ); - self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDelete.csv'); + $this->assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDelete.csv'); } /** @@ -116,6 +119,6 @@ public function doesNotDeleteDataOnOtherPid(): void ] ); - self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv'); + $this->assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv'); } }