-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb47e21
commit 1ea49ae
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
application/libraries/Ilch/Validation/Validators/Timezone.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* @copyright Ilch 2 | ||
*/ | ||
|
||
namespace Ilch\Validation\Validators; | ||
|
||
/** | ||
* Timezone validation class. | ||
*/ | ||
class Timezone extends Base | ||
{ | ||
/** | ||
* Default error key for this validator. | ||
* | ||
* @var string | ||
*/ | ||
protected $errorKey = 'validation.errors.timezone.notAValidTimezone'; | ||
|
||
/** | ||
* Runs the validation. | ||
* | ||
* @return self | ||
*/ | ||
public function run(): self | ||
{ | ||
$includeOutdated = $this->getParameter(1) === 'backwardsCompatible'; | ||
|
||
$this->setIsValid($this->getValue() === '' || in_array($this->getValue(), \DateTimeZone::listIdentifiers($includeOutdated ? \DateTimeZone::ALL_WITH_BC : \DateTimeZone::ALL)) !== false); | ||
|
||
return $this; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
tests/libraries/ilch/Validation/Validators/TimezoneTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Ilch 2 | ||
* @package ilch_phpunit | ||
*/ | ||
|
||
namespace Ilch\Validation\Validators; | ||
|
||
use PHPUnit\Ilch\TestCase; | ||
use stdClass; | ||
|
||
/** | ||
* Tests for the timezone validator | ||
*/ | ||
class TimezoneTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider dpForTestValidator | ||
* | ||
* @param stdClass $data | ||
* @param bool $expectedIsValid | ||
* @param string $expectedErrorKey | ||
* @param array $expectedErrorParameters | ||
*/ | ||
public function testValidator(stdClass $data, bool $expectedIsValid, string $expectedErrorKey = '', array $expectedErrorParameters = []) | ||
{ | ||
$validator = new Timezone($data); | ||
$validator->run(); | ||
self::assertSame($expectedIsValid, $validator->isValid()); | ||
if (!empty($expectedErrorKey)) { | ||
self::assertSame($expectedErrorKey, $validator->getErrorKey()); | ||
self::assertSame($expectedErrorParameters, $validator->getErrorParameters()); | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function dpForTestValidator(): array | ||
{ | ||
return [ | ||
// timezone validations | ||
'timezone valid' => [ | ||
'data' => $this->createData('Europe/Kyiv'), | ||
'expectedIsValid' => true | ||
], | ||
'timezone valid including outdated' => [ | ||
'data' => $this->createData('America/Shiprock', true), | ||
'expectedIsValid' => true | ||
], | ||
'empty timezone valid' => [ | ||
'data' => $this->createData(''), | ||
'expectedIsValid' => true | ||
], | ||
'timezone invalid' => [ | ||
'data' => $this->createData('test'), | ||
'expectedIsValid' => false, | ||
'expectedErrorKey' => 'validation.errors.timezone.notAValidTimezone', | ||
'expectedErrorParameters' => [] | ||
], | ||
'timezone invalid completely removed' => [ | ||
'data' => $this->createData('Canada/East-Saskatchewan'), | ||
'expectedIsValid' => false, | ||
'expectedErrorKey' => 'validation.errors.timezone.notAValidTimezone', | ||
'expectedErrorParameters' => [] | ||
], | ||
'timezone invalid outdated' => [ | ||
'data' => $this->createData('America/Shiprock'), | ||
'expectedIsValid' => false, | ||
'expectedErrorKey' => 'validation.errors.timezone.notAValidTimezone', | ||
'expectedErrorParameters' => [] | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Helper function for creating data object | ||
* | ||
* @param string $value | ||
* @return stdClass | ||
*/ | ||
private function createData(string $value, bool $includeOutdated = false): stdClass | ||
{ | ||
$data = new stdClass(); | ||
$data->field = 'fieldName'; | ||
$data->parameters = ['']; | ||
$data->input = ['fieldName' => $value]; | ||
if ($includeOutdated) { | ||
$data->parameters[] = 'backwardsCompatible'; | ||
} | ||
return $data; | ||
} | ||
} |