diff --git a/config/conditional-actions.php b/config/conditional-actions.php index 349ad5a..182f004 100644 --- a/config/conditional-actions.php +++ b/config/conditional-actions.php @@ -4,7 +4,8 @@ 'conditions' => [ 'AllOfCondition' => ConditionalActions\Entities\Conditions\AllOfCondition::class, 'OneOfCondition' => ConditionalActions\Entities\Conditions\OneOfCondition::class, - 'TrueCondition' => ConditionalActions\Entities\Conditions\TrueCondition::class, + 'TrueCondition' => ConditionalActions\Entities\Conditions\TrueCondition::class, + 'ValidationCondition' => ConditionalActions\Entities\Conditions\ValidationCondition::class, ], 'actions' => [ 'UpdateStateAttributeAction' => ConditionalActions\Entities\Actions\UpdateStateAttributeAction::class, diff --git a/src/ConditionalActionException.php b/src/ConditionalActionException.php new file mode 100644 index 0000000..412ed04 --- /dev/null +++ b/src/ConditionalActionException.php @@ -0,0 +1,9 @@ +getValidationData(), $this->parameters ?? []); + + return !$validator->fails(); + } +} diff --git a/tests/TestCase.php b/tests/ConditionalActionsTestCase.php similarity index 94% rename from tests/TestCase.php rename to tests/ConditionalActionsTestCase.php index ba8ac57..736d723 100644 --- a/tests/TestCase.php +++ b/tests/ConditionalActionsTestCase.php @@ -10,7 +10,7 @@ use Tests\Helpers\Dummy\DummySucceedCondition; use Tests\Helpers\Dummy\DummyTestHelper; -class TestCase extends OrchestraTestCase +class ConditionalActionsTestCase extends OrchestraTestCase { use DummyTestHelper; diff --git a/tests/EloquentTestCase.php b/tests/EloquentConditionalActionsTestCase.php similarity index 82% rename from tests/EloquentTestCase.php rename to tests/EloquentConditionalActionsTestCase.php index 672c526..715e197 100644 --- a/tests/EloquentTestCase.php +++ b/tests/EloquentConditionalActionsTestCase.php @@ -2,7 +2,7 @@ namespace Tests; -class EloquentTestCase extends TestCase +class EloquentConditionalActionsTestCase extends ConditionalActionsTestCase { protected function setUp() { diff --git a/tests/Feature/ConditionalActionManagerTest.php b/tests/Feature/ConditionalActionManagerTest.php index ee53be8..6f6fb2a 100644 --- a/tests/Feature/ConditionalActionManagerTest.php +++ b/tests/Feature/ConditionalActionManagerTest.php @@ -4,11 +4,11 @@ use ConditionalActions\Entities\Eloquent\Action; use ConditionalActions\Entities\Eloquent\Condition; -use Tests\EloquentTestCase; +use Tests\EloquentConditionalActionsTestCase; use Tests\Helpers\Dummy\DummyEloquentModel; use Tests\Helpers\Dummy\DummyEloquentTarget; -class ConditionalActionManagerTest extends EloquentTestCase +class ConditionalActionManagerTest extends EloquentConditionalActionsTestCase { public function test_run_conditional_actions_succeeded() { diff --git a/tests/Feature/Entities/Eloquent/ConditionActionTest.php b/tests/Feature/Entities/Eloquent/ConditionActionTest.php index a10621f..9d3b87a 100644 --- a/tests/Feature/Entities/Eloquent/ConditionActionTest.php +++ b/tests/Feature/Entities/Eloquent/ConditionActionTest.php @@ -8,9 +8,9 @@ use Illuminate\Support\Carbon; use Illuminate\Validation\ValidationException; use Symfony\Component\HttpFoundation\Response; -use Tests\EloquentTestCase; +use Tests\EloquentConditionalActionsTestCase; -class ConditionActionTest extends EloquentTestCase +class ConditionActionTest extends EloquentConditionalActionsTestCase { /** @var Carbon */ private $now = '2019-01-05 10:00:00'; diff --git a/tests/Feature/Entities/Eloquent/ConditionTest.php b/tests/Feature/Entities/Eloquent/ConditionTest.php index 9127a8b..e2af66a 100644 --- a/tests/Feature/Entities/Eloquent/ConditionTest.php +++ b/tests/Feature/Entities/Eloquent/ConditionTest.php @@ -9,9 +9,9 @@ use Illuminate\Http\Response; use Illuminate\Support\Carbon; use Illuminate\Validation\ValidationException; -use Tests\EloquentTestCase; +use Tests\EloquentConditionalActionsTestCase; -class ConditionTest extends EloquentTestCase +class ConditionTest extends EloquentConditionalActionsTestCase { /** @var Carbon */ private $now = '2019-01-05 10:00:00'; diff --git a/tests/Feature/Http/Conditions/ActionsControllerTest.php b/tests/Feature/Http/Conditions/ActionsControllerTest.php index c11db51..e39c785 100644 --- a/tests/Feature/Http/Conditions/ActionsControllerTest.php +++ b/tests/Feature/Http/Conditions/ActionsControllerTest.php @@ -11,9 +11,9 @@ use Illuminate\Support\Carbon; use Mockery; use Symfony\Component\HttpFoundation\Response; -use Tests\TestCase; +use Tests\ConditionalActionsTestCase; -class ActionsControllerTest extends TestCase +class ActionsControllerTest extends ConditionalActionsTestCase { /** @var Mockery\MockInterface */ private $actionRepositoryMock; diff --git a/tests/Feature/Http/Conditions/ConditionsControllerTest.php b/tests/Feature/Http/Conditions/ConditionsControllerTest.php index 14b513b..0d9d760 100644 --- a/tests/Feature/Http/Conditions/ConditionsControllerTest.php +++ b/tests/Feature/Http/Conditions/ConditionsControllerTest.php @@ -11,9 +11,9 @@ use Illuminate\Support\Carbon; use Mockery; use Symfony\Component\HttpFoundation\Response; -use Tests\TestCase; +use Tests\ConditionalActionsTestCase; -class ConditionsControllerTest extends TestCase +class ConditionsControllerTest extends ConditionalActionsTestCase { /** @var Mockery\MockInterface */ private $conditionRepositoryMock; diff --git a/tests/Feature/Repositories/EloquentActionRepositoryTest.php b/tests/Feature/Repositories/EloquentActionRepositoryTest.php index 24a3bac..222b5c8 100644 --- a/tests/Feature/Repositories/EloquentActionRepositoryTest.php +++ b/tests/Feature/Repositories/EloquentActionRepositoryTest.php @@ -8,9 +8,9 @@ use ConditionalActions\Entities\Eloquent\Condition; use ConditionalActions\Exceptions\ActionNotFoundException; use ConditionalActions\Repositories\EloquentActionRepository; -use Tests\EloquentTestCase; +use Tests\EloquentConditionalActionsTestCase; -class EloquentActionRepositoryTest extends EloquentTestCase +class EloquentActionRepositoryTest extends EloquentConditionalActionsTestCase { /** @var EloquentActionRepository */ private $actionsRepository; diff --git a/tests/Feature/Repositories/EloquentConditionRepositoryTest.php b/tests/Feature/Repositories/EloquentConditionRepositoryTest.php index 17fca2f..93e97d8 100644 --- a/tests/Feature/Repositories/EloquentConditionRepositoryTest.php +++ b/tests/Feature/Repositories/EloquentConditionRepositoryTest.php @@ -6,9 +6,9 @@ use ConditionalActions\Entities\Eloquent\Condition; use ConditionalActions\Exceptions\ConditionNotFoundException; use ConditionalActions\Repositories\EloquentConditionRepository; -use Tests\EloquentTestCase; +use Tests\EloquentConditionalActionsTestCase; -class EloquentConditionRepositoryTest extends EloquentTestCase +class EloquentConditionRepositoryTest extends EloquentConditionalActionsTestCase { /** @var EloquentConditionRepository */ private $conditionsRepository; diff --git a/tests/Feature/Traits/EloquentTargetTest.php b/tests/Feature/Traits/EloquentTargetTest.php index 6126412..95c1e3b 100644 --- a/tests/Feature/Traits/EloquentTargetTest.php +++ b/tests/Feature/Traits/EloquentTargetTest.php @@ -5,10 +5,10 @@ use ConditionalActions\Contracts\ConditionContract; use ConditionalActions\Entities\Eloquent\Condition; use Illuminate\Support\Collection; -use Tests\EloquentTestCase; +use Tests\EloquentConditionalActionsTestCase; use Tests\Helpers\Dummy\DummyEloquentModel; -class EloquentTargetTest extends EloquentTestCase +class EloquentTargetTest extends EloquentConditionalActionsTestCase { public function test_get_root_and_children_conditions() { diff --git a/tests/Helpers/Dummy/DummyValidationDataTarget.php b/tests/Helpers/Dummy/DummyValidationDataTarget.php new file mode 100644 index 0000000..3f87c85 --- /dev/null +++ b/tests/Helpers/Dummy/DummyValidationDataTarget.php @@ -0,0 +1,22 @@ +validationData = $validationData; + } + + public function getValidationData(): array + { + return $this->validationData; + } +} diff --git a/tests/Unit/ConditionActionManagerTest.php b/tests/Unit/ConditionActionManagerTest.php index f8190cd..65a5477 100644 --- a/tests/Unit/ConditionActionManagerTest.php +++ b/tests/Unit/ConditionActionManagerTest.php @@ -5,9 +5,9 @@ use ConditionalActions\ConditionalActionManager; use Tests\Helpers\Dummy\DummyAction; use Tests\Helpers\Dummy\DummyTarget; -use Tests\TestCase; +use Tests\ConditionalActionsTestCase; -class ConditionActionManagerTest extends TestCase +class ConditionActionManagerTest extends ConditionalActionsTestCase { /** @var ConditionalActionManager */ private $manager; diff --git a/tests/Unit/Entities/Actions/UpdateStateAttributeActionTest.php b/tests/Unit/Entities/Actions/UpdateStateAttributeActionTest.php index 24e45e0..d0d8ff0 100644 --- a/tests/Unit/Entities/Actions/UpdateStateAttributeActionTest.php +++ b/tests/Unit/Entities/Actions/UpdateStateAttributeActionTest.php @@ -4,9 +4,9 @@ use ConditionalActions\Entities\Actions\UpdateStateAttributeAction; use ConditionalActions\Entities\State; -use Tests\TestCase; +use Tests\ConditionalActionsTestCase; -class UpdateStateAttributeActionTest extends TestCase +class UpdateStateAttributeActionTest extends ConditionalActionsTestCase { public function test_state_attribute_updated() { diff --git a/tests/Unit/Entities/Conditions/HasChildrenConditionsTestCase.php b/tests/Unit/Entities/Conditions/HasChildrenConditionsTestCase.php index 0319560..d37393b 100644 --- a/tests/Unit/Entities/Conditions/HasChildrenConditionsTestCase.php +++ b/tests/Unit/Entities/Conditions/HasChildrenConditionsTestCase.php @@ -7,9 +7,9 @@ use Tests\Helpers\Dummy\DummyAction; use Tests\Helpers\Dummy\DummyCondition; use Tests\Helpers\Dummy\DummyTarget; -use Tests\TestCase; +use Tests\ConditionalActionsTestCase; -class HasChildrenConditionsTestCase extends TestCase +class HasChildrenConditionsTestCase extends ConditionalActionsTestCase { /** @var ConditionContract */ protected $testCondition; diff --git a/tests/Unit/Entities/Conditions/ValidationConditionTest.php b/tests/Unit/Entities/Conditions/ValidationConditionTest.php new file mode 100644 index 0000000..83ddced --- /dev/null +++ b/tests/Unit/Entities/Conditions/ValidationConditionTest.php @@ -0,0 +1,66 @@ +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()); + } +}