-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add command controller to create test data
Related: #11120
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use TYPO3\CMS\Core\Database\Connection; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
final class CreateTestDataCommand extends Command | ||
{ | ||
/** @phpstan-ignore-next-line */ | ||
protected array $teaData = [ | ||
[ | ||
'title' => '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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters