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));
}
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;
}
/**

@ -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;
}
/**

@ -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;
}
/**

@ -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;
}
/**

@ -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;

Loading…
Cancel
Save