Skip to content

Commit

Permalink
chore: Convert some parts of Date validator to php8 syntax and add so…
Browse files Browse the repository at this point in the history
…me tests to check userland retrocompatibility.

Signed-off-by: codisart <[email protected]>
  • Loading branch information
codisart committed Feb 21, 2024
1 parent 5c3fc8c commit cba497b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct($options = [])
$options = iterator_to_array($options);
} elseif (! is_array($options)) {
$options = func_get_args();
$temp = [];
$temp['format'] = array_shift($options);
$options = $temp;
}
Expand Down Expand Up @@ -152,22 +153,23 @@ protected function convertToDateTime($param, $addErrors = true)
return DateTime::createFromImmutable($param);
}

$type = gettype($param);
switch ($type) {
case 'string':
return $this->convertString($param, $addErrors);
case 'integer':
return $this->convertInteger($param);
case 'double':
return $this->convertDouble($param);
case 'array':
return $this->convertArray($param, $addErrors);
}
return match (gettype($param)) {
'string' => $this->convertString($param, $addErrors),
'integer' => $this->convertInteger($param),
'double' => $this->convertDouble($param),
'array' => $this->convertArray($param),
default => $this->addInvalidTypeError($addErrors),
};
}

/**
* @return false
*/
private function addInvalidTypeError(bool $addErrors)
{
if ($addErrors) {
$this->error(self::INVALID);
}

return false;
}

Expand Down
17 changes: 17 additions & 0 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laminas\Validator\Date;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use LaminasTest\Validator\TestAsset\CustomDate;

Check failure on line 12 in test/DateTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Use statements should be sorted alphabetically. The first wrong one is LaminasTest\Validator\TestAsset\CustomDate.
use PHPUnit\Framework\TestCase;
use stdClass;

Expand Down Expand Up @@ -195,4 +196,20 @@ public function testConstructorWithFormatParameter(): void

self::assertSame($format, $validator->getFormat());
}

public function testAddErrorsParam(): void
{
$customDateValidator = new Date();
self::assertFalse($customDateValidator->isValid(new stdClass()));

Check failure on line 203 in test/DateTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidArgument

test/DateTest.php:203:57: InvalidArgument: Argument 1 of Laminas\Validator\Date::isValid expects DateTimeInterface|array<array-key, mixed>|numeric|string, but stdClass provided (see https://psalm.dev/004)
self::assertArrayHasKey('dateInvalid', $customDateValidator->getMessages());
}

public function testExtensionDateValidator(): void
{
$customDateValidator = new CustomDate();
self::assertTrue($customDateValidator->isValid(16757802.07));

self::assertFalse($customDateValidator->isValid(new stdClass()));

Check failure on line 212 in test/DateTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidArgument

test/DateTest.php:212:57: InvalidArgument: Argument 1 of LaminasTest\Validator\TestAsset\CustomDate::isValid expects LaminasTest\Validator\TestAsset\DateTimeInterface|array<array-key, mixed>|numeric|string, but stdClass provided (see https://psalm.dev/004)
self::assertArrayNotHasKey('dateInvalid', $customDateValidator->getMessages());
}
}
53 changes: 53 additions & 0 deletions test/TestAsset/CustomDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Validator\TestAsset;

use Laminas\Validator\Date;

final class CustomDate extends Date
{
/** @var string */
protected $format = self::FORMAT_DEFAULT;

/** @var bool */
protected $strict = false;

/**
* @param string|numeric|array|DateTimeInterface $value
* @return bool
*/
public function isValid($value)
{
return parent::isValid($value);
}

/**
* @param string|numeric|array|DateTimeInterface $param
* @param bool $addErrors
* @return bool|DateTime
*/
protected function convertToDateTime($param, $addErrors = true)
{
return parent::convertToDateTime($param, false);
}

/**
* @param integer $value
* @return false|DateTime
*/
protected function convertInteger($value)
{
return parent::convertInteger($value);
}

/**
* @param double $value
* @return false|DateTime
*/
protected function convertDouble($value)
{
return parent::convertDouble($value * 100);
}
}

0 comments on commit cba497b

Please sign in to comment.