Skip to content

Commit

Permalink
fix: unit tests eror
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 6, 2021
1 parent 1eef199 commit 80052ec
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 187 deletions.
39 changes: 21 additions & 18 deletions src/Rule/DateRangeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace Swoft\Validator\Rule;

use DateTime;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\DateRange;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
use function strtotime;

/**
* Class AlphaRule
Expand All @@ -26,37 +26,40 @@
class DateRangeRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param null $default
* @param array $data
* @param string $propertyName
* @param object|DateRange $item
* @param null $default
* @param bool $strict
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null, $strict = false): array
{
/* @var DateRange $item */
$start = $item->getStart();
$end = $item->getEnd();
$endTs = strtotime($end = $item->getEnd());
$value = $data[$propertyName];

$startTs = strtotime($start = $item->getStart());
if (is_string($value)) {
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $value);
if (($dt !== false && !array_sum($dt::getLastErrors())) && strtotime($value) >= strtotime($start) && $value <= strtotime($end)) {
// $dt = DateTime::createFromFormat('Y-m-d H:i:s', $value);
$ts = strtotime($value);
if ($ts > 0 && $ts >= $startTs && $ts <= $endTs) {
return $data;
}

// is timestamp
if (ctype_digit($value) && date('Y-m-d', (int)$value) && $value >= $startTs && $value <= $endTs) {
return $data;
} elseif (ctype_digit($value)) {
if (date('Y-m-d', (int)$value) && $value >= strtotime($start) && $value <= strtotime($end)) {
return $data;
}
}
} elseif (filter_var($value, FILTER_VALIDATE_INT)) {
if ($value >= strtotime($start) && $value <= strtotime($end)) {
if ($value >= $startTs && $value <= $endTs) {
return $data;
}
}

$message = $item->getMessage();
$message = (empty($message)) ?
sprintf('%s is invalid date range(start=%s, end=%s)', $propertyName, $start, $end) : $message;
$message = $message ?: sprintf('%s is invalid date range(start=%s, end=%s)', $propertyName, $start, $end);

throw new ValidatorException($message);
}
}
43 changes: 22 additions & 21 deletions test/unit/ValidatorRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testAfterDateError(): void
$data = [
'dataAfterDate' => '2019-07-06'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('dataAfterDate must be after 2019-07-08');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAfterDate'));
}
Expand All @@ -43,7 +43,7 @@ public function testAlphaError(): void
$data = [
'dataAlpha' => 'abcde0123'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('alpha message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAlpha'));
}
Expand All @@ -65,7 +65,7 @@ public function testAlphaDashError(): void
$data = [
'dataAlphaDash' => '.='
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('alphadash message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAlphaDash'));
}
Expand All @@ -88,7 +88,7 @@ public function testAlphaNumError(): void
'dataAlphaNum' => 'abcde-'
];

$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('alphanum message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAlphaNum'));
}
Expand All @@ -110,7 +110,7 @@ public function testBeforeDateError(): void
$data = [
'dataBeforeDate' => '2019-07-10'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('before date message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testBeforeDate'));
}
Expand All @@ -132,7 +132,7 @@ public function testChsError(): void
$data = [
'dataChs' => 'english'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('chs message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChs'));
}
Expand All @@ -154,7 +154,7 @@ public function testChsAlphaError(): void
$data = [
'dataChsAlpha' => '-_'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('chsalpha message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChsAlpha'));
}
Expand All @@ -176,7 +176,7 @@ public function testChsAlphaDashError(): void
$data = [
'dataChsAlphaDash' => '>?'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('chsalphadash message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChsAlphaDash'));
}
Expand All @@ -198,7 +198,7 @@ public function testChsAlphaNumError(): void
$data = [
'dataChsAlphaNum' => '-_'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('chsalphanum message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChsAlphaNum'));
}
Expand All @@ -221,7 +221,7 @@ public function testConfirmError(): void
'dataConfirm' => '123',
'confirm' => '456'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('confirm message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testConfirm'));
}
Expand All @@ -245,7 +245,7 @@ public function testDifferentError(): void
'dataDifferent' => '123',
'different' => '123'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('different message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDifferent'));
}
Expand All @@ -269,7 +269,7 @@ public function testGreaterThanError(): void
'dataGreaterThan' => '12',
'gt' => '123'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('greaterthan message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testGreaterThan'));
}
Expand All @@ -293,7 +293,7 @@ public function testLessThanError(): void
'dataLessThan' => '124',
'lt' => '123'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('lessthan message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testLessThan'));
}
Expand All @@ -316,7 +316,7 @@ public function testDateError(): void
$data = [
'dataDate' => '2019f'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('date message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDate'));
}
Expand All @@ -338,7 +338,7 @@ public function testDateRangeError(): void
$data = [
'dataDateRange' => '2019-06-18'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('daterange message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDateRange'));
}
Expand All @@ -352,6 +352,7 @@ public function testDateRangeSuccess(): void
$data,
$this->getValidates(ValidatorRule::class, 'testDateRange')
);

$this->assertEquals($data, $result);
}

Expand All @@ -360,7 +361,7 @@ public function testDnsError(): void
$data = [
'dataDns' => 'swoft.con'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('dns message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDns'));
}
Expand Down Expand Up @@ -412,7 +413,7 @@ public function testLowError(): void
$data = [
'dataLow' => 'swofT'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('low message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testLow'));
}
Expand All @@ -434,7 +435,7 @@ public function testNotInEnumError(): void
$data = [
'dataNotInEnum' => '1'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('notinenum message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testNotInEnum'));
}
Expand All @@ -456,7 +457,7 @@ public function testNotInRangeError(): void
$data = [
'dataNotInRange' => '1'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('notinrange message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testNotInRange'));
}
Expand All @@ -478,7 +479,7 @@ public function testUpperError(): void
$data = [
'dataUpper' => 'sWOFT'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('upper message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testUpper'));
}
Expand All @@ -500,7 +501,7 @@ public function testUrlError(): void
$data = [
'dataUrl' => 'baidu.com'
];
$this->expectException(\Swoft\Validator\Exception\ValidatorException::class);
$this->expectException(ValidatorException::class);
$this->expectExceptionMessage('url message');
(new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testUrl'));
}
Expand Down
Loading

0 comments on commit 80052ec

Please sign in to comment.