Skip to content

Commit

Permalink
rework transition conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolineus committed Nov 5, 2014
1 parent 005f60b commit 4559d3c
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,4 @@ public function match(Transition $transition, Item $item, Context $context)
{
// TODO: Implement match() method.
}

/**
* Describes an failed condition.
*
* It returns an array with 2 parameters. First one is the error message code. The second one are the params to
* be replaced in the message.
*
* Example return array('transition.condition.example', array('name', 'value'));
*
* @param Transition $transition The transition being in.
* @param Item $item The entity being transits.
* @param Context $context The transition context.
*
* @return array
*/
public function describeError(Transition $transition, Item $item, Context $context)
{
// TODO: Implement describeError() method.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function it_does_not_match_if_one_child_does_not(
) {
$conditionA->match($transition, $item, $context)->willReturn(true);
$conditionB->match($transition, $item, $context)->willReturn(false);
$conditionB->getError()->shouldBeCalled();

$this->addCondition($conditionA);
$this->addCondition($conditionB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,6 @@ function it_throws_if_invalid_condition_passed()
{
$this->shouldThrow('Assert\InvalidArgumentException')->duringAddConditions(array('test'));
}

function it_provides_a_describe_child_condition_errors_method(Transition $transition, Item $item, Context $context, Condition $condition, Condition $condition2)
{
$this->addCondition($condition);
$this->addCondition($condition2);

$condition->describeError($transition, $item, $context)->shouldBeCalled();
$condition->describeError($transition, $item, $context)->willReturn('test');

$condition2->describeError($transition, $item, $context)->shouldBeCalled();
$condition2->describeError($transition, $item, $context)->willReturn('test2');

$this->describeChildConditionErrors($transition, $item, $context)->shouldReturn('test, test2');
}
}

class ConditionCollection extends AbstractConditionCollection
Expand Down Expand Up @@ -102,23 +88,4 @@ public function describeError(Transition $transition, Item $item, Context $conte
{
// TODO: Implement describeError() method.
}

/**
* Describes an failed condition.
*
* It returns an array with 2 parameters. First one is the error message code. The second one are the params to
* be replaced in the message.
*
* Example return array('transition.condition.example', array('name', 'value'));
*
* @param Transition $transition The transition being in.
* @param Item $item The entity being transits.
* @param Context $context The transition context.
*
* @return array
*/
public function describeChildConditionErrors(Transition $transition, Item $item, Context $context)
{
return parent::describeChildConditionErrors($transition, $item, $context);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ function it_matches_if_any_child_matches(
Context $context
) {
$conditionA->match($transition, $item, $context)->willReturn(false);
$conditionA->getError()->shouldBeCalled();

$conditionB->match($transition, $item, $context)->willReturn(true);

$this->addCondition($conditionA);
$this->addCondition($conditionB);

$this->match($transition, $item, $context)->shouldReturn(true);
$this->shouldNotHaveError();
}

function it_does_not_match_if_all_children_does_not(
Expand All @@ -51,16 +54,21 @@ function it_does_not_match_if_all_children_does_not(
Context $context
) {
$conditionA->match($transition, $item, $context)->willReturn(false);
$conditionA->getError()->shouldBeCalled();

$conditionB->match($transition, $item, $context)->willReturn(false);
$conditionB->getError()->shouldBeCalled();

$this->addCondition($conditionA);
$this->addCondition($conditionB);

$this->match($transition, $item, $context)->shouldReturn(false);
$this->shouldHaveError();
}

function it_matches_if_no_children_exists(Transition $transition, Item $item, Context $context)
{
$this->match($transition, $item, $context)->shouldReturn(true);
$this->shouldNotHaveError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,19 @@ function it_matches_if_comparison_is_true(Transition $transition, Item $item, Co
$entity->getProperty('test')->willReturn(10);

$this->match($transition, $item, $context)->shouldReturn(true);
$this->shouldNotHaveError();
}

function it_creates_an_error_if_it_fails(Transition $transition, Item $item, Context $context, Entity $entity)
function it_does_not_match_if_comparison_does(Transition $transition, Item $item, Context $context, Entity $entity)
{
$this->setOperator(Comparison::LESSER_THAN);
$this->setProperty('test');
$this->setValue(5);

$item->getEntity()->willReturn($entity);
$this->describeError($transition, $item, $context)->shouldBeArray();
$entity->getProperty('test')->willReturn(10);

$this->match($transition, $item, $context)->shouldReturn(false);
$this->shouldHaveError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,22 @@ function it_matches_if_workflow_not_started_by_default(Transition $transition, I
$item->isWorkflowStarted()->willReturn(false);

$this->match($transition, $item, $context)->shouldReturn(true);
$this->shouldNotHaveError();
}

function it_does_match_if_workflow_not_started_when_disabled(Transition $transition, Item $item, Context $context)
{
$transition->getName()->shouldBeCalled();

$item->isWorkflowStarted()->willReturn(false);
$this->disallowStartTransition()->shouldReturn($this);

$this->match($transition, $item, $context)->shouldReturn(false);
$this->getError()->shouldHaveCount(2);

$this->allowStartTransition()->shouldReturn($this);
$this->match($transition, $item, $context)->shouldReturn(true);
$this->shouldNotHaveError();
}

function it_matches_if_step_role_is_granted(Transition $transition, Item $item, Context $context, User $user, Role $role)
Expand All @@ -76,6 +81,7 @@ function it_does_not_match_if_step_has_no_assigned_role(Transition $transition,
$step->getRole()->willReturn(null);

$this->match($transition, $item, $context)->shouldReturn(false);
$this->shouldHaveError();
}

function it_does_not_match_if_step_role_is_not_granted(Transition $transition, Item $item, Context $context, User $user, Role $role)
Expand All @@ -86,10 +92,6 @@ function it_does_not_match_if_step_role_is_not_granted(Transition $transition, I
$user->isGranted($role)->willReturn(false);

$this->match($transition, $item, $context)->shouldReturn(false);
}

function it_creates_an_error_if_it_fails(Transition $transition, Item $item, Context $context, User $user, Role $role)
{
$this->describeError($transition, $item, $context)->shouldBeArray();
$this->shouldHaveError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function it_matches_if_transition_any_of_the_transition_roles_matches(
$transition->getRoles()->willReturn(array($notGranted, $role));

$this->match($transition, $item, $context)->shouldReturn(true);

$this->shouldNotHaveError();
}

function it_does_not_match_if_transition_has_no_role(
Expand All @@ -62,14 +62,6 @@ function it_does_not_match_if_transition_has_no_role(
$transition->getRoles()->willReturn(array());

$this->match($transition, $item, $context)->shouldReturn(false);
}

function it_describes_the_error(
Transition $transition,
Item $item,
Context $context
)
{
$this->describeError($transition, $item, $context)->shouldBeArray();
$this->shouldHaveError();
}
}
9 changes: 9 additions & 0 deletions spec/Netzmacht/Workflow/Flow/TransitionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function it_checks_a_precondition_failing(
Context $context
) {
$condition->match($this, $item, $context)->willReturn(false);
$condition->getError()->shouldBeCalled();

$this->addPreCondition($condition)->shouldReturn($this);
$this->checkPreCondition($item, $context)->shouldReturn(false);
Expand Down Expand Up @@ -136,6 +137,7 @@ function it_checks_a_condition_failing(
Context $context
) {
$condition->match($this, $item, $context)->willReturn(false);
$condition->getError()->shouldBeCalled();

$this->addCondition($condition)->shouldReturn($this);
$this->checkCondition($item, $context)->shouldReturn(false);
Expand Down Expand Up @@ -164,6 +166,7 @@ function it_is_not_allowed_by_failing_pre_condition(
) {
$condition->match($this, $item, $context)->willReturn(true);
$preCondition->match($this, $item, $context)->willReturn(false);
$preCondition->getError()->shouldBeCalled();

$this->addCondition($condition);
$this->addPreCondition($preCondition);
Expand All @@ -178,6 +181,8 @@ function it_is_not_allowed_by_failing_condition(
Context $context
) {
$condition->match($this, $item, $context)->willReturn(false);
$condition->getError()->shouldBeCalled();

$preCondition->match($this, $item, $context)->willReturn(true);

$this->addCondition($condition);
Expand Down Expand Up @@ -211,6 +216,8 @@ function it_is_not_available_when_condition_fails(
->match($this, $item, $context)
->willReturn(false);

$condition->getError()->shouldBeCalled();

$preCondition
->match($this, $item, $context)
->willReturn(true);
Expand All @@ -237,6 +244,8 @@ function it_is_not_available_when_precondition_fails(
->match($this, $item, $context)
->willReturn(false);

$preCondition->getError()->shouldBeCalled();

$this->addCondition($condition);
$this->addPreCondition($preCondition);

Expand Down

0 comments on commit 4559d3c

Please sign in to comment.