From a51719ae6047849069fcc64ef9f10cf384263383 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Wed, 12 Jul 2023 18:55:58 +0200 Subject: [PATCH] Test of DateValidator --- .../Validation/TCommandWithValidation.php | 6 ++--- .../Validators/DateTimeValidator.php | 5 ++-- .../Validation/Validators/DateValidator.php | 11 ++++----- .../Validation/Validators/TimeValidator.php | 5 ++-- tests/Commands/Hello.php | 24 ++++++++++++++++--- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/CliProgram/Validation/TCommandWithValidation.php b/src/CliProgram/Validation/TCommandWithValidation.php index 4baf7d0..f4bfafd 100644 --- a/src/CliProgram/Validation/TCommandWithValidation.php +++ b/src/CliProgram/Validation/TCommandWithValidation.php @@ -212,10 +212,10 @@ trait TCommandWithValidation { throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name)); } if ($validator === null) { - unset($this->argumentsValidator[$name]); + unset($this->optionsValidator[$name]); } else { - $this->argumentsValidator[$name] = $validator; + $this->optionsValidator[$name] = $validator; } return $this; @@ -231,7 +231,7 @@ trait TCommandWithValidation { if (!$this->getDefinition()->hasOption($name)) { throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name)); } - return $this->argumentsValidator[$name] ?? null; + return $this->optionsValidator[$name] ?? null; } /** diff --git a/src/CliProgram/Validation/Validators/DateTimeValidator.php b/src/CliProgram/Validation/Validators/DateTimeValidator.php index de37e6a..2dfa898 100644 --- a/src/CliProgram/Validation/Validators/DateTimeValidator.php +++ b/src/CliProgram/Validation/Validators/DateTimeValidator.php @@ -50,17 +50,16 @@ class DateTimeValidator extends AbstractDateTimeValidator { */ public function getValue (): DateTimeInterface { $value = new DateTimeImmutable(); - $value->setDate( + $value = $value->setDate( (int)$this->getInternalValidator()->getMatch('year'), (int)$this->getInternalValidator()->getMatch('month'), (int)$this->getInternalValidator()->getMatch('day'), ); - $value->setTime( + return $value->setTime( (int)($this->getInternalValidator()->getMatch('hour') ?? 0), (int)($this->getInternalValidator()->getMatch('minute') ?? 0), (int)($this->getInternalValidator()->getMatch('second') ?? 0), ); - return $value; } /** diff --git a/src/CliProgram/Validation/Validators/DateValidator.php b/src/CliProgram/Validation/Validators/DateValidator.php index c7911dd..5f99892 100644 --- a/src/CliProgram/Validation/Validators/DateValidator.php +++ b/src/CliProgram/Validation/Validators/DateValidator.php @@ -19,10 +19,10 @@ class DateValidator extends AbstractDateTimeValidator { /** * Create a validator * - * @param DateTimeInterface|null $datePart The time part + * @param DateTimeInterface|null $timePart The time part */ - public function __construct (?DateTimeInterface $datePart = null) { - $this->setTimePart($datePart ?? (new DateTimeImmutable())->setTimestamp(0)); + public function __construct (?DateTimeInterface $timePart = null) { + $this->setTimePart($timePart ?? (new DateTimeImmutable())->setTimestamp(0)); parent::__construct(); } @@ -47,17 +47,16 @@ class DateValidator extends AbstractDateTimeValidator { */ public function getValue (): DateTimeInterface { $value = new DateTimeImmutable(); - $value->setDate( + $value = $value->setDate( (int)$this->getInternalValidator()->getMatch('year'), (int)$this->getInternalValidator()->getMatch('month'), (int)$this->getInternalValidator()->getMatch('day'), ); - $value->setTime( + return $value->setTime( (int)$this->getTimePart()->format('%H'), (int)$this->getTimePart()->format('%i'), (int)$this->getTimePart()->format('%s'), ); - return $value; } /** diff --git a/src/CliProgram/Validation/Validators/TimeValidator.php b/src/CliProgram/Validation/Validators/TimeValidator.php index beef772..43ed547 100644 --- a/src/CliProgram/Validation/Validators/TimeValidator.php +++ b/src/CliProgram/Validation/Validators/TimeValidator.php @@ -47,17 +47,16 @@ class TimeValidator extends AbstractDateTimeValidator { */ public function getValue (): DateTimeInterface { $value = new DateTimeImmutable(); - $value->setDate( + $value = $value->setDate( (int)$this->getDatePart()->format('%Y'), (int)$this->getDatePart()->format('%m'), (int)$this->getDatePart()->format('%d'), ); - $value->setTime( + return $value->setTime( (int)($this->getInternalValidator()->getMatch('hour') ?? 0), (int)($this->getInternalValidator()->getMatch('minute') ?? 0), (int)($this->getInternalValidator()->getMatch('second') ?? 0), ); - return $value; } /** diff --git a/tests/Commands/Hello.php b/tests/Commands/Hello.php index a447edd..4129fad 100644 --- a/tests/Commands/Hello.php +++ b/tests/Commands/Hello.php @@ -2,8 +2,11 @@ namespace jrosset\Tests\Commands; +use DateTimeImmutable; +use DateTimeInterface; use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog; use jrosset\CliProgram\Validation\CommandWithValidation; +use jrosset\CliProgram\Validation\Validators\DateValidator; use jrosset\CliProgram\Validation\Validators\EnumValidator; use jrosset\CliProgram\Validation\Validators\IntegerValidator; use jrosset\Tests\Lang; @@ -32,7 +35,16 @@ class Hello extends CommandWithValidation { parent::configure(); $this->addArgument( + 'day', + InputArgument::OPTIONAL, + 'The day', + new DateTimeImmutable(), + new DateValidator() + ); + + $this->addOption( 'lang', + 'l', InputArgument::OPTIONAL, 'The lang', Lang::English, @@ -54,11 +66,17 @@ class Hello extends CommandWithValidation { protected function execute (InputInterface $input, OutputInterface $output): int { $output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG); - /** @var Lang $lang */ - $lang = $input->getArgument('lang'); + $text = $input->getOption('lang')->value; $repeat = $input->getOption('repeat'); + + /** @var DateTimeInterface $day */ + $day = $input->getArgument('day'); + if ((new DateTimeImmutable())->diff($day)->days === 0) { + $text .= ' today'; + } + for ($curr = 0; $curr < $repeat; $curr++) { - $output->writeln($lang->value); + $output->writeln($text); } $output->writeln('FIN', ConsoleOutputWithMonolog::OPTION_SKIP_MONOLOG); return Command::SUCCESS;