Merge pull request #128 from ghaninia/ValidationTests
Added PHPDoc & unitTest for JalaliValidator
This commit is contained in:
2 files changed
+490
No files matched your search
@@ -6,6 +6,13 @@ use Hekmatinasser\Verta\Verta;
|
||||
|
||||
class JalaliValidator
|
||||
{
|
||||
/**
|
||||
* Determines if an input is a valid Jalali date with the specified format
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDate($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -22,6 +29,14 @@ class JalaliValidator
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali date with the specified format and
|
||||
* it is equal a given date
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -38,6 +53,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali date with the specified format and
|
||||
* it is not equal a given date
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateNotEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -54,6 +77,13 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTime($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -71,6 +101,14 @@ class JalaliValidator
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is equal a given datetime
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTimeEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -87,6 +125,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is not equal a given datetime
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTimeNotEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -103,6 +149,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali date with the specified format and
|
||||
* it is after a given date
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateAfter($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -119,6 +173,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is after or equal a given datetime
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateAfterEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -135,6 +197,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is after a given datetime
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTimeAfter($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -151,6 +221,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is after or equal a given datetime
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTimeAfterEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -167,6 +245,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali date with the specified format and
|
||||
* it is before a given date
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateBefore($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -183,6 +269,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali date with the specified format and
|
||||
* it is before or equal a given date
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateBeforeEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -199,6 +293,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is before a given date-time
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTimeBefore($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -215,6 +317,14 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if an input is a valid Jalali datetime with the specified format and
|
||||
* it is before or equal a given datetime
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function validateDateTimeBeforeEqual($attribute, $value, $parameters)
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
@@ -231,11 +341,27 @@ class JalaliValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* replace date or datetime
|
||||
* @param $message
|
||||
* @param $attribute
|
||||
* @param $rule
|
||||
* @param $parameters
|
||||
* @return string
|
||||
*/
|
||||
public function replaceDateOrDatetime($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* replace date after or before or equal
|
||||
* @param $message
|
||||
* @param $attribute
|
||||
* @param $rule
|
||||
* @param $parameters
|
||||
* @return string
|
||||
*/
|
||||
public function replaceDateAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
|
||||
@@ -249,6 +375,14 @@ class JalaliValidator
|
||||
return str_replace(':date', $date, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* replace date time after or before or equal
|
||||
* @param $message
|
||||
* @param $attribute
|
||||
* @param $rule
|
||||
* @param $parameters
|
||||
* @return string
|
||||
*/
|
||||
public function replaceDateTimeAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
|
||||
|
||||
@@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
namespace Hekmatinasser\Verta\Tests\Laravel;
|
||||
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Hekmatinasser\Verta\Laravel\JalaliValidator;
|
||||
|
||||
class JalaliValidatorTest extends TestCase
|
||||
{
|
||||
|
||||
protected $jalaliValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->jalaliValidator = new JalaliValidator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider correctDateFormatProvider
|
||||
*/
|
||||
public function validateDateByCorrectInput($date, $format = null)
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDate(NULL, $date, !!$format ? [$format] : [])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider incorrectDateFormatProvider
|
||||
*/
|
||||
public function validateDateByIncorrectInput($date, $format = null)
|
||||
{
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDate(NULL, $date, !!$format ? [$format] : [])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider correctDateFormatProvider
|
||||
*/
|
||||
public function validateDateEqualByCorrectInput($date, $format = null)
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateEqual(
|
||||
NULL,
|
||||
$date,
|
||||
!!$format ? [$date, $format] : [$date]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateEqualByIncorrectInput()
|
||||
{
|
||||
|
||||
$result = $this->jalaliValidator->validateDateEqual(
|
||||
NULL,
|
||||
'1397/01/01',
|
||||
['1397/01/02']
|
||||
);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateNotEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateNotEqual(NULL, '1399/01/01', ['1399/01/02'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateNotEqual(NULL, '1399/01/01', ['1399/01/01'])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* provider for tests
|
||||
*/
|
||||
public function correctDateFormatProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
"1398/01/01",
|
||||
"Y/m/d"
|
||||
],
|
||||
[
|
||||
"1398/01/01",
|
||||
],
|
||||
[
|
||||
"1398-01-01",
|
||||
"Y-m-d"
|
||||
],
|
||||
[
|
||||
"1398-01-01 20:00:00",
|
||||
"Y-m-d H:i:s"
|
||||
],
|
||||
[
|
||||
"1398-01-01 20:00",
|
||||
"Y-m-d H:i"
|
||||
],
|
||||
[
|
||||
"1398.12.29",
|
||||
"Y.m.d"
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* provider for tests
|
||||
*/
|
||||
public function incorrectDateFormatProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
"1398",
|
||||
"Y"
|
||||
],
|
||||
[
|
||||
"1398-01",
|
||||
"Y-m"
|
||||
],
|
||||
[
|
||||
"1398/01",
|
||||
"Y/m"
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* provider for tests
|
||||
*/
|
||||
public function jalaliValidatorMethods()
|
||||
{
|
||||
return [
|
||||
["validateDate"],
|
||||
["validateDateEqual"],
|
||||
["validateDateNotEqual"],
|
||||
["validateDateTime"],
|
||||
["validateDateTimeEqual"],
|
||||
["validateDateTimeNotEqual"],
|
||||
["validateDateAfterEqual"],
|
||||
["validateDateTimeAfter"],
|
||||
["validateDateTimeAfterEqual"],
|
||||
["validateDateBeforeEqual"],
|
||||
["validateDateTimeBefore"],
|
||||
["validateDateTimeBeforeEqual"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider jalaliValidatorMethods
|
||||
*/
|
||||
public function whenJalaliValidatorSecoundArgumentIsEmpty($method)
|
||||
{
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->{$method}(NULL, [], [])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTime()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTime(NULL, "1398/01/01 20:00:00", [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTime(NULL, "1398/01/01 20:00:00", ["Y/m/d H:i:s"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTime(NULL, "1398/01/01", ["Y/m/d H:i:s"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTimeEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeEqual(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeEqual(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:01"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTimeNotEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeNotEqual(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:01"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeNotEqual(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateAfter()
|
||||
{
|
||||
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateAfter(NULL, "1398/01/02", ["1398/01/01"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateAfter(NULL, "1398/01/01", ["1398/01/01"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateAfter(NULL, "1398/01/01", ["1398/01/02"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateAfterEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateAfterEqual(NULL, "1398/01/02", ["1398/01/01"])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateAfterEqual(NULL, "1398/01/01", ["1398/01/01"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateAfterEqual(NULL, "1398/01/01", ["1398/01/02"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTimeAfter()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeAfter(NULL, "1398/01/02 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeAfter(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeAfter(NULL, "1398/01/01 20:00:00", ["1398/01/02 20:00:00"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTimeAfterEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeAfterEqual(NULL, "1398/01/02 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeAfterEqual(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeAfterEqual(NULL, "1398/01/01 20:00:00", ["1398/01/02 20:00:00"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateBefore()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateBefore(NULL, "1398/01/01", ["1398/01/02"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateBefore(NULL, "1398/01/01", ["1398/01/01"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateBefore(NULL, "1398/01/02", ["1398/01/01"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateBeforeEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateBeforeEqual(NULL, "1398/01/01", ["1398/01/02"])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateBeforeEqual(NULL, "1398/01/01", ["1398/01/01"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateBeforeEqual(NULL, "1398/01/05", ["1398/01/01"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTimeBefore()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeBefore(NULL, "1398/01/01 20:00:00", ["1398/01/02 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeBefore(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeBefore(NULL, "1398/01/02 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function validateDateTimeBeforeEqual()
|
||||
{
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeBeforeEqual(NULL, "1398/01/01 20:00:00", ["1398/01/02 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->jalaliValidator->validateDateTimeBeforeEqual(NULL, "1398/01/01 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->jalaliValidator->validateDateTimeBeforeEqual(NULL, "1398/01/02 20:00:00", ["1398/01/01 20:00:00"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function replaceDateOrDatetime()
|
||||
{
|
||||
$text = "hello world!";
|
||||
$result = $this->jalaliValidator->replaceDateOrDatetime($text, NULL, NULL, NULL);
|
||||
$this->assertEquals($text, $result);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function replaceDateAfterOrBeforeOrEqual()
|
||||
{
|
||||
$message = "The :attribute is not equal Jalali date :date";
|
||||
Verta::setLocale("en");
|
||||
$result = $this->jalaliValidator->replaceDateAfterOrBeforeOrEqual($message, NULL, NULL, [$date = "1398/01/01", "Y-m-d"]);
|
||||
$this->assertEquals("The :attribute is not equal Jalali date {$date}", $result);
|
||||
Verta::setLocale("fa");
|
||||
$result = $this->jalaliValidator->replaceDateAfterOrBeforeOrEqual($message, NULL, NULL, [$date = "1398/01/01", "Y-m-d"]);
|
||||
$this->assertEquals("The :attribute is not equal Jalali date ۱۳۹۸/۰۱/۰۱", $result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user