-
Notifications
You must be signed in to change notification settings - Fork 1
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
Aleksandr Paramonov
committed
May 30, 2019
1 parent
91b770f
commit 5238e0e
Showing
19 changed files
with
167 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace ConditionalActions; | ||
|
||
use Exception; | ||
|
||
class ConditionalActionException extends Exception | ||
{ | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace ConditionalActions\Contracts\TargetProviders; | ||
|
||
interface ProvidesValidationData | ||
{ | ||
public function getValidationData(): array; | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace ConditionalActions\Entities\Conditions; | ||
|
||
use ConditionalActions\ConditionalActionException; | ||
use ConditionalActions\Contracts\StateContract; | ||
use ConditionalActions\Contracts\TargetContract; | ||
use ConditionalActions\Contracts\TargetProviders\ProvidesValidationData; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class ValidationCondition extends BaseCondition | ||
{ | ||
/** | ||
* Runs condition check. | ||
* | ||
* @param TargetContract|ProvidesValidationData $target | ||
* @param StateContract $state | ||
* | ||
* @throws ConditionalActionException | ||
* | ||
* @return bool | ||
*/ | ||
public function check(TargetContract $target, StateContract $state): bool | ||
{ | ||
if (!($target instanceof ProvidesValidationData)) { | ||
throw new ConditionalActionException( | ||
'The target does not implemented ProvidesValidationData contract', | ||
400 | ||
); | ||
} | ||
|
||
$validator = Validator::make($target->getValidationData(), $this->parameters ?? []); | ||
|
||
return !$validator->fails(); | ||
} | ||
} |
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
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
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,22 @@ | ||
<?php | ||
|
||
namespace Tests\Helpers\Dummy; | ||
|
||
use ConditionalActions\Contracts\TargetProviders\ProvidesValidationData; | ||
|
||
class DummyValidationDataTarget extends DummyTarget implements ProvidesValidationData | ||
{ | ||
/** @var array */ | ||
private $validationData; | ||
|
||
public function __construct(array $validationData) | ||
{ | ||
parent::__construct(); | ||
$this->validationData = $validationData; | ||
} | ||
|
||
public function getValidationData(): array | ||
{ | ||
return $this->validationData; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
tests/Unit/Entities/Conditions/ValidationConditionTest.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,66 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Entities\Conditions; | ||
|
||
use ConditionalActions\ConditionalActionException; | ||
use ConditionalActions\Entities\Conditions\ValidationCondition; | ||
use Tests\ConditionalActionsTestCase; | ||
use Tests\Helpers\Dummy\DummyTarget; | ||
use Tests\Helpers\Dummy\DummyValidationDataTarget; | ||
|
||
class ValidationConditionTest extends ConditionalActionsTestCase | ||
{ | ||
/** @var ValidationCondition */ | ||
private $condition; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->condition = new ValidationCondition(); | ||
} | ||
|
||
/** | ||
* @param array $validationData | ||
* @param array $conditionParams | ||
* @param bool $result | ||
* | ||
* @dataProvider provider_test_validation | ||
* @throws ConditionalActionException | ||
*/ | ||
public function test_validation(array $validationData, array $conditionParams, bool $result) | ||
{ | ||
$this->condition->setParameters($conditionParams); | ||
$target = new DummyValidationDataTarget($validationData); | ||
|
||
$this->assertEquals($result, $this->condition->check($target, $target->getInitialState())); | ||
} | ||
|
||
public function provider_test_validation(): array | ||
{ | ||
return [ | ||
'succeeded' => [ | ||
['foo' => ['bar' => 10]], | ||
['foo.bar' => 'required|int|max:9'], | ||
false, | ||
], | ||
'failed' => [ | ||
['foo' => ['bar' => 10]], | ||
['foo.bar' => 'required|int|max:11'], | ||
true, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @throws ConditionalActionException | ||
*/ | ||
public function test_exception_when_provides_validation_data_contract_not_implemented() | ||
{ | ||
$target = new DummyTarget(); | ||
|
||
$this->expectException(ConditionalActionException::class); | ||
$this->expectExceptionCode(400); | ||
|
||
$this->condition->check($target, $target->getInitialState()); | ||
} | ||
} |