Skip to content

Commit 39a885d

Browse files
authored
Add validation condition (#10)
Add validation condition
2 parents 91b770f + 5238e0e commit 39a885d

19 files changed

+167
-25
lines changed

config/conditional-actions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
'conditions' => [
55
'AllOfCondition' => ConditionalActions\Entities\Conditions\AllOfCondition::class,
66
'OneOfCondition' => ConditionalActions\Entities\Conditions\OneOfCondition::class,
7-
'TrueCondition' => ConditionalActions\Entities\Conditions\TrueCondition::class,
7+
'TrueCondition' => ConditionalActions\Entities\Conditions\TrueCondition::class,
8+
'ValidationCondition' => ConditionalActions\Entities\Conditions\ValidationCondition::class,
89
],
910
'actions' => [
1011
'UpdateStateAttributeAction' => ConditionalActions\Entities\Actions\UpdateStateAttributeAction::class,

src/ConditionalActionException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace ConditionalActions;
4+
5+
use Exception;
6+
7+
class ConditionalActionException extends Exception
8+
{
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ConditionalActions\Contracts\TargetProviders;
4+
5+
interface ProvidesValidationData
6+
{
7+
public function getValidationData(): array;
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace ConditionalActions\Entities\Conditions;
4+
5+
use ConditionalActions\ConditionalActionException;
6+
use ConditionalActions\Contracts\StateContract;
7+
use ConditionalActions\Contracts\TargetContract;
8+
use ConditionalActions\Contracts\TargetProviders\ProvidesValidationData;
9+
use Illuminate\Support\Facades\Validator;
10+
11+
class ValidationCondition extends BaseCondition
12+
{
13+
/**
14+
* Runs condition check.
15+
*
16+
* @param TargetContract|ProvidesValidationData $target
17+
* @param StateContract $state
18+
*
19+
* @throws ConditionalActionException
20+
*
21+
* @return bool
22+
*/
23+
public function check(TargetContract $target, StateContract $state): bool
24+
{
25+
if (!($target instanceof ProvidesValidationData)) {
26+
throw new ConditionalActionException(
27+
'The target does not implemented ProvidesValidationData contract',
28+
400
29+
);
30+
}
31+
32+
$validator = Validator::make($target->getValidationData(), $this->parameters ?? []);
33+
34+
return !$validator->fails();
35+
}
36+
}

tests/TestCase.php renamed to tests/ConditionalActionsTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Tests\Helpers\Dummy\DummySucceedCondition;
1111
use Tests\Helpers\Dummy\DummyTestHelper;
1212

13-
class TestCase extends OrchestraTestCase
13+
class ConditionalActionsTestCase extends OrchestraTestCase
1414
{
1515
use DummyTestHelper;
1616

tests/EloquentTestCase.php renamed to tests/EloquentConditionalActionsTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Tests;
44

5-
class EloquentTestCase extends TestCase
5+
class EloquentConditionalActionsTestCase extends ConditionalActionsTestCase
66
{
77
protected function setUp()
88
{

tests/Feature/ConditionalActionManagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use ConditionalActions\Entities\Eloquent\Action;
66
use ConditionalActions\Entities\Eloquent\Condition;
7-
use Tests\EloquentTestCase;
7+
use Tests\EloquentConditionalActionsTestCase;
88
use Tests\Helpers\Dummy\DummyEloquentModel;
99
use Tests\Helpers\Dummy\DummyEloquentTarget;
1010

11-
class ConditionalActionManagerTest extends EloquentTestCase
11+
class ConditionalActionManagerTest extends EloquentConditionalActionsTestCase
1212
{
1313
public function test_run_conditional_actions_succeeded()
1414
{

tests/Feature/Entities/Eloquent/ConditionActionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use Illuminate\Support\Carbon;
99
use Illuminate\Validation\ValidationException;
1010
use Symfony\Component\HttpFoundation\Response;
11-
use Tests\EloquentTestCase;
11+
use Tests\EloquentConditionalActionsTestCase;
1212

13-
class ConditionActionTest extends EloquentTestCase
13+
class ConditionActionTest extends EloquentConditionalActionsTestCase
1414
{
1515
/** @var Carbon */
1616
private $now = '2019-01-05 10:00:00';

tests/Feature/Entities/Eloquent/ConditionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Illuminate\Http\Response;
1010
use Illuminate\Support\Carbon;
1111
use Illuminate\Validation\ValidationException;
12-
use Tests\EloquentTestCase;
12+
use Tests\EloquentConditionalActionsTestCase;
1313

14-
class ConditionTest extends EloquentTestCase
14+
class ConditionTest extends EloquentConditionalActionsTestCase
1515
{
1616
/** @var Carbon */
1717
private $now = '2019-01-05 10:00:00';

tests/Feature/Http/Conditions/ActionsControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Illuminate\Support\Carbon;
1212
use Mockery;
1313
use Symfony\Component\HttpFoundation\Response;
14-
use Tests\TestCase;
14+
use Tests\ConditionalActionsTestCase;
1515

16-
class ActionsControllerTest extends TestCase
16+
class ActionsControllerTest extends ConditionalActionsTestCase
1717
{
1818
/** @var Mockery\MockInterface */
1919
private $actionRepositoryMock;

0 commit comments

Comments
 (0)