Test of DateValidator

2.x
Julien Rosset 2 years ago
parent cf547dee95
commit a51719ae60

@ -212,10 +212,10 @@ trait TCommandWithValidation {
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name)); throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name));
} }
if ($validator === null) { if ($validator === null) {
unset($this->argumentsValidator[$name]); unset($this->optionsValidator[$name]);
} }
else { else {
$this->argumentsValidator[$name] = $validator; $this->optionsValidator[$name] = $validator;
} }
return $this; return $this;
@ -231,7 +231,7 @@ trait TCommandWithValidation {
if (!$this->getDefinition()->hasOption($name)) { if (!$this->getDefinition()->hasOption($name)) {
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name)); throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name));
} }
return $this->argumentsValidator[$name] ?? null; return $this->optionsValidator[$name] ?? null;
} }
/** /**

@ -50,17 +50,16 @@ class DateTimeValidator extends AbstractDateTimeValidator {
*/ */
public function getValue (): DateTimeInterface { public function getValue (): DateTimeInterface {
$value = new DateTimeImmutable(); $value = new DateTimeImmutable();
$value->setDate( $value = $value->setDate(
(int)$this->getInternalValidator()->getMatch('year'), (int)$this->getInternalValidator()->getMatch('year'),
(int)$this->getInternalValidator()->getMatch('month'), (int)$this->getInternalValidator()->getMatch('month'),
(int)$this->getInternalValidator()->getMatch('day'), (int)$this->getInternalValidator()->getMatch('day'),
); );
$value->setTime( return $value->setTime(
(int)($this->getInternalValidator()->getMatch('hour') ?? 0), (int)($this->getInternalValidator()->getMatch('hour') ?? 0),
(int)($this->getInternalValidator()->getMatch('minute') ?? 0), (int)($this->getInternalValidator()->getMatch('minute') ?? 0),
(int)($this->getInternalValidator()->getMatch('second') ?? 0), (int)($this->getInternalValidator()->getMatch('second') ?? 0),
); );
return $value;
} }
/** /**

@ -19,10 +19,10 @@ class DateValidator extends AbstractDateTimeValidator {
/** /**
* Create a validator * Create a validator
* *
* @param DateTimeInterface|null $datePart The time part * @param DateTimeInterface|null $timePart The time part
*/ */
public function __construct (?DateTimeInterface $datePart = null) { public function __construct (?DateTimeInterface $timePart = null) {
$this->setTimePart($datePart ?? (new DateTimeImmutable())->setTimestamp(0)); $this->setTimePart($timePart ?? (new DateTimeImmutable())->setTimestamp(0));
parent::__construct(); parent::__construct();
} }
@ -47,17 +47,16 @@ class DateValidator extends AbstractDateTimeValidator {
*/ */
public function getValue (): DateTimeInterface { public function getValue (): DateTimeInterface {
$value = new DateTimeImmutable(); $value = new DateTimeImmutable();
$value->setDate( $value = $value->setDate(
(int)$this->getInternalValidator()->getMatch('year'), (int)$this->getInternalValidator()->getMatch('year'),
(int)$this->getInternalValidator()->getMatch('month'), (int)$this->getInternalValidator()->getMatch('month'),
(int)$this->getInternalValidator()->getMatch('day'), (int)$this->getInternalValidator()->getMatch('day'),
); );
$value->setTime( return $value->setTime(
(int)$this->getTimePart()->format('%H'), (int)$this->getTimePart()->format('%H'),
(int)$this->getTimePart()->format('%i'), (int)$this->getTimePart()->format('%i'),
(int)$this->getTimePart()->format('%s'), (int)$this->getTimePart()->format('%s'),
); );
return $value;
} }
/** /**

@ -47,17 +47,16 @@ class TimeValidator extends AbstractDateTimeValidator {
*/ */
public function getValue (): DateTimeInterface { public function getValue (): DateTimeInterface {
$value = new DateTimeImmutable(); $value = new DateTimeImmutable();
$value->setDate( $value = $value->setDate(
(int)$this->getDatePart()->format('%Y'), (int)$this->getDatePart()->format('%Y'),
(int)$this->getDatePart()->format('%m'), (int)$this->getDatePart()->format('%m'),
(int)$this->getDatePart()->format('%d'), (int)$this->getDatePart()->format('%d'),
); );
$value->setTime( return $value->setTime(
(int)($this->getInternalValidator()->getMatch('hour') ?? 0), (int)($this->getInternalValidator()->getMatch('hour') ?? 0),
(int)($this->getInternalValidator()->getMatch('minute') ?? 0), (int)($this->getInternalValidator()->getMatch('minute') ?? 0),
(int)($this->getInternalValidator()->getMatch('second') ?? 0), (int)($this->getInternalValidator()->getMatch('second') ?? 0),
); );
return $value;
} }
/** /**

@ -2,8 +2,11 @@
namespace jrosset\Tests\Commands; namespace jrosset\Tests\Commands;
use DateTimeImmutable;
use DateTimeInterface;
use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog; use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog;
use jrosset\CliProgram\Validation\CommandWithValidation; use jrosset\CliProgram\Validation\CommandWithValidation;
use jrosset\CliProgram\Validation\Validators\DateValidator;
use jrosset\CliProgram\Validation\Validators\EnumValidator; use jrosset\CliProgram\Validation\Validators\EnumValidator;
use jrosset\CliProgram\Validation\Validators\IntegerValidator; use jrosset\CliProgram\Validation\Validators\IntegerValidator;
use jrosset\Tests\Lang; use jrosset\Tests\Lang;
@ -32,7 +35,16 @@ class Hello extends CommandWithValidation {
parent::configure(); parent::configure();
$this->addArgument( $this->addArgument(
'day',
InputArgument::OPTIONAL,
'The day',
new DateTimeImmutable(),
new DateValidator()
);
$this->addOption(
'lang', 'lang',
'l',
InputArgument::OPTIONAL, InputArgument::OPTIONAL,
'The lang', 'The lang',
Lang::English, Lang::English,
@ -54,11 +66,17 @@ class Hello extends CommandWithValidation {
protected function execute (InputInterface $input, OutputInterface $output): int { protected function execute (InputInterface $input, OutputInterface $output): int {
$output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG); $output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG);
/** @var Lang $lang */ $text = $input->getOption('lang')->value;
$lang = $input->getArgument('lang');
$repeat = $input->getOption('repeat'); $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++) { for ($curr = 0; $curr < $repeat; $curr++) {
$output->writeln($lang->value); $output->writeln($text);
} }
$output->writeln('FIN', ConsoleOutputWithMonolog::OPTION_SKIP_MONOLOG); $output->writeln('FIN', ConsoleOutputWithMonolog::OPTION_SKIP_MONOLOG);
return Command::SUCCESS; return Command::SUCCESS;

Loading…
Cancel
Save