From b42f6ed08ec863887d2a9a37a6a9cbaa66fd630c Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 7 May 2024 14:21:17 +0200 Subject: [PATCH 01/18] [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' + ] + ); + }; From cc64515cd61acde04ab7a4f63c9326b6f682c9cb Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Fri, 26 Jul 2024 08:35:31 +0200 Subject: [PATCH 02/18] [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 b40d55b..13d0885 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -8,7 +8,6 @@ namespace TTN\Tea\Command; * 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 277a1676215e387c0c57d2862710330577208548 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Fri, 26 Jul 2024 09:39:27 +0200 Subject: [PATCH 03/18] [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 0000000..092cad0 --- /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 0bcfdb5..84aac66 100644 --- a/Documentation/Index.rst +++ b/Documentation/Index.rst @@ -47,6 +47,7 @@ continuous integration. ReleaseBranchingStrategy Environment DependencyManager + CommandController Running ContinuousIntegration Documentation From 72d449b9bd30ce12b1bed0be1aa8ccd00ba725ad Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 17:21:27 +0200 Subject: [PATCH 04/18] [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 13d0885..39e0052 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 @@ final class CreateTestDataCommand extends Command 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 802bfa3..262c210 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -16,10 +16,11 @@ return static function (ContainerConfigurator $containerConfigurator) { ->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 5ad2d2d8ad8aa6a30e1763e192f5fc15ab373f54 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:38:54 +0200 Subject: [PATCH 05/18] 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 39e0052..966b011 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -19,7 +19,9 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; final class CreateTestDataCommand extends Command { - /** @phpstan-ignore-next-line */ + /** + * @var list}> + */ protected array $teaData = [ [ 'title' => 'Darjeeling', From dd1f4832ff113480b4a3e8fa99d98cfdb0b4425b Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:39:11 +0200 Subject: [PATCH 06/18] 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 966b011..b29b40c 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 8200302d31110fbc9bc00f93dfd773427879e0a2 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:44:06 +0200 Subject: [PATCH 07/18] [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 b29b40c..2659173 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -4,10 +4,6 @@ declare(strict_types=1); 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\Connection; 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 d1420a21b37af80043efe0e76bf8ed5fef8f229c Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Mon, 29 Jul 2024 19:48:34 +0200 Subject: [PATCH 08/18] [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 2659173..77cdb8c 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -39,7 +39,7 @@ final class CreateTestDataCommand extends Command $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 @@ final class CreateTestDataCommand extends Command 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 @@ final class CreateTestDataCommand extends Command 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 18b804f17930e5412f1ac532295ab394217916c8 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 11:24:13 +0200 Subject: [PATCH 09/18] [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 77cdb8c..f6ef35f 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -53,10 +53,10 @@ final class CreateTestDataCommand extends Command 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 e3544f4ade3c5cc75eb1668101aca4c8465ce877 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 12:32:54 +0200 Subject: [PATCH 10/18] [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 f6ef35f..d7d0173 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -4,13 +4,19 @@ declare(strict_types=1); 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 @@ final class CreateTestDataCommand extends Command } $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 1af328f27bd93b8ac5b2c58d887a5b4fa84c2113 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 19:11:47 +0200 Subject: [PATCH 11/18] [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 d7d0173..fe23cee 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -60,7 +60,6 @@ final class CreateTestDataCommand extends Command 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 271bd24c4f52b3328d8c89319458b4c1a6304794 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 19:13:44 +0200 Subject: [PATCH 12/18] [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 0000000..30bdb09 --- /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 0000000..fab4071 --- /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 0000000..f4878eb --- /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 0000000..40e686b --- /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 0000000..3967dcd --- /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 13b111653260d316a34952eea62a4a3454f40bb4 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 30 Jul 2024 19:51:15 +0200 Subject: [PATCH 13/18] 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 30bdb09..400a543 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -92,7 +92,7 @@ class CreateTestDataCommandTest extends FunctionalTestCase */ 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 0000000..e1dd287 --- /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 3967dcd..c0a2aec 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 a4ba1039930fc000dd7de622acbe3f079af2940e Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 20:19:18 +0200 Subject: [PATCH 14/18] [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 400a543..0f28823 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -103,4 +103,19 @@ class CreateTestDataCommandTest extends FunctionalTestCase 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 0000000..f78bd2e --- /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 fab4071..0000000 --- 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 0000000..a427bf0 --- /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 a1cf83ccbc9ad3b2d1cac1c696600926698f3e28 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 20:30:50 +0200 Subject: [PATCH 15/18] [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 0f28823..0cffd5b 100644 --- a/Tests/Functional/Command/CreateTestDataCommandTest.php +++ b/Tests/Functional/Command/CreateTestDataCommandTest.php @@ -78,7 +78,7 @@ class CreateTestDataCommandTest extends FunctionalTestCase /** * @test */ - public function testDataGetsCreated(): void + public function createsTestData(): void { $this->commandTester->execute([ 'pageUid' => '1', @@ -90,7 +90,7 @@ class CreateTestDataCommandTest extends FunctionalTestCase /** * @test */ - public function testDataGetsDeletedBeforeNewDataCreated(): void + public function deletesExistingDataOnGivenPidBeforeCreatingNewData(): void { $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/ExistingTeas.csv'); $this->commandTester->execute( @@ -106,7 +106,7 @@ class CreateTestDataCommandTest extends FunctionalTestCase /** * @test */ - public function existingDataWillBeNotDeleted(): void + public function doesNotDeleteDataOnOtherPid(): void { $this->importCSVDataSet(__DIR__ . '/Fixtures/Database/OtherExistingTeas.csv'); $this->commandTester->execute( @@ -115,7 +115,7 @@ class CreateTestDataCommandTest extends FunctionalTestCase '--delete-data-before' => true, ] ); + self::assertCSVDataSet(__DIR__ . '/Fixtures/Database/TeasAfterDeleteOtherExistingTeas.csv'); } - } From f86b37df41a004bfe24919b65f7e966edad15d88 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 30 Jul 2024 20:41:05 +0200 Subject: [PATCH 16/18] [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 fe23cee..201c034 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -4,16 +4,11 @@ declare(strict_types=1); 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 bf7ade8f982a71c03eba918e7d8b25c03a9a9a2a Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 20 Aug 2024 08:49:30 +0200 Subject: [PATCH 17/18] [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 201c034..6cc8d2a 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 @@ final class CreateTestDataCommand extends Command 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 @@ final class CreateTestDataCommand extends Command $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 055a2f07bc0d54cce5cde26576e3113c946f70a0 Mon Sep 17 00:00:00 2001 From: Karsten Nowak Date: Tue, 20 Aug 2024 09:15:23 +0200 Subject: [PATCH 18/18] [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 6cc8d2a..7e17cc7 100644 --- a/Classes/Command/CreateTestDataCommand.php +++ b/Classes/Command/CreateTestDataCommand.php @@ -69,7 +69,7 @@ final class CreateTestDataCommand extends Command $query = $connectionForTable; foreach ($this->teaData as $item) { - $item = ['pid' => $pageUid, ...$item]; + $item = ['pid' => $pageUid, 'title' => $item['title'], 'description' => $item['description']]; $query->insert( $table, $item