-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from spiral/develop
nested checkers
- Loading branch information
Showing
10 changed files
with
371 additions
and
3 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
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,21 @@ | ||
<?php | ||
|
||
namespace Spiral\Validation; | ||
|
||
/** | ||
* Provides ability to execute checker on a certain condition if it is met. | ||
*/ | ||
interface CheckerConditionInterface | ||
{ | ||
/** | ||
* @param ValidatorInterface $validator | ||
* | ||
* @return CheckerConditionInterface | ||
*/ | ||
public function withValidator(ValidatorInterface $validator): CheckerConditionInterface; | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isMet(): bool; | ||
} |
23 changes: 23 additions & 0 deletions
23
source/Spiral/Validation/Prototypes/AbstractCheckerCondition.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,23 @@ | ||
<?php | ||
|
||
namespace Spiral\Validation\Prototypes; | ||
|
||
use Spiral\Validation\CheckerConditionInterface; | ||
use Spiral\Validation\ValidatorInterface; | ||
|
||
abstract class AbstractCheckerCondition implements CheckerConditionInterface | ||
{ | ||
/** @var ValidatorInterface */ | ||
protected $validator; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withValidator(ValidatorInterface $validator): CheckerConditionInterface | ||
{ | ||
$condition = clone $this; | ||
$condition->validator = $validator; | ||
|
||
return $condition; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Valentin | ||
* Date: 02.08.2017 | ||
* Time: 10:58 | ||
*/ | ||
|
||
namespace Spiral\Tests\Validation\Conditions; | ||
|
||
|
||
use Spiral\Tests\BaseTest; | ||
use Spiral\Tests\Validation\Fixtures\IsLoadedCondition; | ||
use Spiral\Validation\Checkers\AddressChecker; | ||
use Spiral\Validation\Checkers\TypeChecker; | ||
use Spiral\Validation\Configs\ValidatorConfig; | ||
use Spiral\Validation\Validator; | ||
use TestApplication\Database\SampleRecord; | ||
|
||
class IsLoadedConditionTest extends BaseTest | ||
{ | ||
protected function getConfig() | ||
{ | ||
return new ValidatorConfig([ | ||
'emptyConditions' => [ | ||
'notEmpty', | ||
'type::notEmpty', | ||
], | ||
'checkers' => [ | ||
'type' => TypeChecker::class, | ||
'address' => AddressChecker::class, | ||
], | ||
'aliases' => [ | ||
'notEmpty' => 'type::notEmpty', | ||
'email' => 'address::email', | ||
'url' => 'address::url', | ||
], | ||
]); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); // TODO: Change the autogenerated stub | ||
|
||
$this->commands->run('orm:schema', [ | ||
'--alter' => true | ||
]); | ||
} | ||
|
||
public function testIsMet() | ||
{ | ||
$validator = new Validator(); | ||
|
||
/** @var \Spiral\Validation\CheckerConditionInterface $condition */ | ||
$condition = $this->container->get(IsLoadedCondition::class)->withValidator($validator); | ||
|
||
$this->assertFalse($condition->isMet()); | ||
|
||
$validator->setContext(['context']); | ||
$this->assertFalse($condition->isMet()); | ||
|
||
$entity = new SampleRecord(); | ||
$validator->setContext($entity); | ||
$this->assertFalse($condition->isMet()); | ||
|
||
$entity->save(); | ||
$this->assertTrue($condition->isMet()); | ||
} | ||
|
||
public function testWithConditions() | ||
{ | ||
//Validator works | ||
$validator = new Validator( | ||
['email' => ['notEmpty', 'address::email']], | ||
['email' => '[email protected]'], | ||
$this->getConfig(), | ||
$this->container | ||
); | ||
$this->assertTrue($validator->isValid()); | ||
|
||
$validator = new Validator( | ||
['email' => ['notEmpty', 'address::email']], | ||
['email' => null], | ||
$this->getConfig(), | ||
$this->container | ||
); | ||
$this->assertFalse($validator->isValid()); | ||
|
||
//Condition will not met because no context | ||
$validator = new Validator( | ||
[ | ||
'email' => [ | ||
['notEmpty', 'condition' => IsLoadedCondition::class], | ||
['address::email', 'condition' => IsLoadedCondition::class], | ||
] | ||
], | ||
['email' => null], | ||
$this->getConfig(), | ||
$this->container | ||
); | ||
$this->assertTrue($validator->isValid()); | ||
|
||
//Condition will not met because context should be entity | ||
$validator = new Validator( | ||
[ | ||
'email' => [ | ||
['notEmpty', 'condition' => IsLoadedCondition::class], | ||
['address::email', 'condition' => IsLoadedCondition::class], | ||
] | ||
], | ||
['email' => null], | ||
$this->getConfig(), | ||
$this->container | ||
); | ||
$validator->setContext(['some', 'context']); | ||
$this->assertTrue($validator->isValid()); | ||
|
||
//Condition will not met because context should be loaded entity | ||
$entity = new SampleRecord(); | ||
$validator->setContext($entity); | ||
$this->assertTrue($validator->isValid()); | ||
|
||
//Condition will met and validator will fail check | ||
$entity = new SampleRecord(); | ||
$entity->save(); | ||
$validator->setContext($entity); | ||
$this->assertFalse($validator->isValid()); | ||
|
||
//Validator will fail because condition should exist and be instance of \Spiral\Validation\CheckerConditionInterface::class | ||
$validator = new Validator( | ||
[ | ||
'email' => [ | ||
['notEmpty', 'condition' => 'Some\Condition'], | ||
['address::email', 'condition' => 'Some\Condition'], | ||
] | ||
], | ||
['email' => null], | ||
$this->getConfig(), | ||
$this->container | ||
); | ||
|
||
$entity = new SampleRecord(); | ||
$entity->save(); | ||
|
||
$validator->setContext($entity); | ||
$this->assertFalse($validator->isValid()); | ||
} | ||
} |
Oops, something went wrong.