Skip to content

Commit

Permalink
Converted some unit test mocking to Prophecy for better readability/m…
Browse files Browse the repository at this point in the history
…aintainability
  • Loading branch information
klausi committed Aug 10, 2015
1 parent d17fa05 commit 31e90bb
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 481 deletions.
4 changes: 2 additions & 2 deletions src/Engine/ConditionExpressionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ abstract class ConditionExpressionContainer extends ExpressionBase implements Co
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\rules\Engine\ExpressionPluginManager $expression_manager
* @param \Drupal\rules\Engine\ExpressionPluginManagerInterface $expression_manager
* The rules expression plugin manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ExpressionPluginManager $expression_manager) {
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ExpressionPluginManagerInterface $expression_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->expressionManager = $expression_manager;

Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/RulesExpression/ActionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Drupal\rules\Engine\ActionExpressionContainerInterface;
use Drupal\rules\Engine\ActionExpressionInterface;
use Drupal\rules\Engine\ExpressionInterface;
use Drupal\rules\Engine\ExpressionPluginManager;
use Drupal\rules\Engine\ExpressionPluginManagerInterface;
use Drupal\rules\Engine\RulesStateInterface;
use Drupal\rules\Exception\InvalidExpressionException;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -44,10 +44,10 @@ class ActionSet extends ExpressionBase implements ActionExpressionContainerInter
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\rules\Engine\ExpressionPluginManager $expression_manager
* @param \Drupal\rules\Engine\ExpressionPluginManagerInterface $expression_manager
* The rules expression plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ExpressionPluginManager $expression_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, ExpressionPluginManagerInterface $expression_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->expressionManager = $expression_manager;

Expand Down
28 changes: 15 additions & 13 deletions tests/src/Unit/ActionSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Drupal\Tests\rules\Unit;

use Drupal\rules\Plugin\RulesExpression\ActionSet;
use Drupal\rules\Engine\RulesStateInterface;
use Prophecy\Argument;

/**
* @coversDefaultClass \Drupal\rules\Plugin\RulesExpression\ActionSet
Expand All @@ -28,30 +30,30 @@ class ActionSetTest extends RulesUnitTestBase {
public function setUp() {
parent::setUp();

$this->actionSet = new ActionSet([], '', [], $this->expressionManager);
$this->actionSet = new ActionSet([], '', [], $this->expressionManager->reveal());
}

/**
* Tests that an action in the set fires.
*/
public function testActionExecution() {
// The method on the test action must be called once.
$this->testActionExpression->expects($this->once())
->method('executeWithState');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(1);

$this->actionSet->addExpressionObject($this->testActionExpression)->execute();
$this->actionSet->addExpressionObject($this->testActionExpression->reveal())->execute();
}

/**
* Tests that two actions in the set fire both.
*/
public function testTwoActionExecution() {
// The method on the test action must be called twice.
$this->testActionExpression->expects($this->exactly(2))
->method('executeWithState');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(2);

$this->actionSet->addExpressionObject($this->testActionExpression)
->addExpressionObject($this->testActionExpression)
$this->actionSet->addExpressionObject($this->testActionExpression->reveal())
->addExpressionObject($this->testActionExpression->reveal())
->execute();
}

Expand All @@ -60,13 +62,13 @@ public function testTwoActionExecution() {
*/
public function testNestedActionExecution() {
// The method on the test action must be called twice.
$this->testActionExpression->expects($this->exactly(2))
->method('executeWithState');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(2);

$inner = $this->getMockActionSet()
->addExpressionObject($this->testActionExpression);
$inner = new ActionSet([], '', [], $this->expressionManager->reveal());
$inner->addExpressionObject($this->testActionExpression->reveal());

$this->actionSet->addExpressionObject($this->testActionExpression)
$this->actionSet->addExpressionObject($this->testActionExpression->reveal())
->addExpressionObject($inner)
->execute();
}
Expand Down
96 changes: 46 additions & 50 deletions tests/src/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
namespace Drupal\Tests\rules\Unit;

use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Engine\ExpressionPluginManager;
use Drupal\rules\Engine\RulesStateInterface;
use Drupal\rules\Plugin\RulesExpression\Rule;
use Drupal\rules\Plugin\RulesExpression\RulesAnd;
use Drupal\rules\Plugin\RulesExpression\RulesOr;
use Drupal\rules\Plugin\RulesExpression\ActionSet;
use Prophecy\Argument;

/**
* @coversDefaultClass \Drupal\rules\Plugin\RulesExpression\Rule
* @group rules
*/
class RuleTest extends RulesUnitTestBase {

/**
* The rules expression plugin manager.
*
* @var \Drupal\rules\Engine\ExpressionPluginManager
*/
protected $expressionManager;

/**
* The rule being tested.
*
Expand Down Expand Up @@ -50,23 +49,15 @@ class RuleTest extends RulesUnitTestBase {
public function setUp() {
parent::setUp();

$this->expressionManager = $this->getMockBuilder('Drupal\rules\Engine\ExpressionPluginManager')
->disableOriginalConstructor()
->getMock();
$this->expressionManager = $this->prophesize(ExpressionPluginManager::class);

$this->conditions = $this->getMockAnd();
$this->expressionManager->expects($this->at(0))
->method('createInstance')
->with('rules_and')
->will($this->returnValue($this->conditions));
$this->conditions = new RulesAnd([], 'rules_and', [], $this->expressionManager->reveal());
$this->expressionManager->createInstance('rules_and', [])->willReturn($this->conditions);

$this->actions = $this->getMockActionSet();
$this->expressionManager->expects($this->at(1))
->method('createInstance')
->with('rules_action_set')
->will($this->returnValue($this->actions));
$this->actions = new ActionSet([], 'rules_action_set', [], $this->expressionManager->reveal());
$this->expressionManager->createInstance('rules_action_set', [])->willReturn($this->actions);

$this->rule = new Rule([], 'rules_rule', [], $this->expressionManager);
$this->rule = new Rule([], 'rules_rule', [], $this->expressionManager->reveal());
}

/**
Expand All @@ -86,11 +77,11 @@ public function testContainersOnConstruct() {
* @covers ::getConditions
*/
public function testSetConditionsGetConditions() {
$or = $this->getMockOr();
$or = new RulesOr([], 'rules_or', [], $this->expressionManager->reveal());
$this->rule->setConditions($or);
$this->assertSame($or, $this->rule->getConditions());

$and = $this->getMockAnd();
$and = new RulesAnd([], 'rules_and', [], $this->expressionManager->reveal());
$this->rule->setConditions($and);
$this->assertSame($and, $this->rule->getConditions());
}
Expand All @@ -102,7 +93,7 @@ public function testSetConditionsGetConditions() {
* @covers ::getActions
*/
public function testSetActionsGetActions() {
$action_set = $this->getMockActionSet();
$action_set = new ActionSet([], '', [], $this->expressionManager->reveal());
$this->rule->setActions($action_set);
$this->assertSame($action_set, $this->rule->getActions());
}
Expand All @@ -114,12 +105,12 @@ public function testSetActionsGetActions() {
*/
public function testActionExecution() {
// The method on the test action must be called once.
$this->testActionExpression->expects($this->once())
->method('executeWithState');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(1);

$this->rule
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->testActionExpression)
->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($this->testActionExpression->reveal())
->execute();
}

Expand All @@ -130,12 +121,12 @@ public function testActionExecution() {
*/
public function testConditionFails() {
// The execute method on the action must never be called.
$this->testActionExpression->expects($this->never())
->method('execute');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldNotBeCalled();

$this->rule
->addExpressionObject($this->falseConditionExpression)
->addExpressionObject($this->testActionExpression)
->addExpressionObject($this->falseConditionExpression->reveal())
->addExpressionObject($this->testActionExpression->reveal())
->execute();
}

Expand All @@ -146,13 +137,13 @@ public function testConditionFails() {
*/
public function testTwoConditionsTrue() {
// The method on the test action must be called once.
$this->testActionExpression->expects($this->once())
->method('executeWithState');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(1);

$this->rule
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->testActionExpression)
->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($this->testActionExpression->reveal())
->execute();
}

Expand All @@ -163,13 +154,13 @@ public function testTwoConditionsTrue() {
*/
public function testTwoConditionsFalse() {
// The execute method on the action must never be called.
$this->testActionExpression->expects($this->never())
->method('execute');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldNotBeCalled();

$this->rule
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->falseConditionExpression)
->addExpressionObject($this->testActionExpression)
->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($this->falseConditionExpression->reveal())
->addExpressionObject($this->testActionExpression->reveal())
->execute();
}

Expand All @@ -179,15 +170,20 @@ public function testTwoConditionsFalse() {
* @covers ::execute
*/
public function testNestedRules() {
$this->testActionExpression->expects($this->once())
->method('executeWithState');
$this->testActionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(1);

$nested = new Rule([], 'rules_rule', [], $this->expressionManager->reveal());
// We need to replace the action and conditon container to not have the same
// instances as in the outer rule.
$nested->setConditions(new RulesAnd([], 'rules_and', [], $this->expressionManager->reveal()));
$nested->setActions(new ActionSet([], 'rules_action_set', [], $this->expressionManager->reveal()));

$nested = $this->getMockRule()
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->testActionExpression);
$nested->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($this->testActionExpression->reveal());

$this->rule
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($nested)
->execute();
}
Expand All @@ -202,7 +198,7 @@ public function testContextDefinitionFromConfig() {
->setLabel('node')
->toArray()
],
], 'rules_rule', [], $this->expressionManager);
], 'rules_rule', [], $this->expressionManager->reveal());
$context_definition = $rule->getContextDefinition('node');
$this->assertSame($context_definition->getDataType(), 'entity:node');
}
Expand All @@ -217,7 +213,7 @@ public function testProvidedDefinitionFromConfig() {
->setLabel('node')
->toArray()
],
], 'rules_rule', [], $this->expressionManager);
], 'rules_rule', [], $this->expressionManager->reveal());
$provided_definition = $rule->getProvidedContextDefinition('node');
$this->assertSame($provided_definition->getDataType(), 'entity:node');
}
Expand Down
29 changes: 15 additions & 14 deletions tests/src/Unit/RulesAndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace Drupal\Tests\rules\Unit;

use Drupal\rules\Engine\RulesStateInterface;
use Drupal\rules\Plugin\RulesExpression\RulesAnd;
use Prophecy\Argument;

/**
* @coversDefaultClass \Drupal\rules\Plugin\RulesExpression\RulesAnd
Expand All @@ -28,18 +30,17 @@ class RulesAndTest extends RulesUnitTestBase {
public function setUp() {
parent::setUp();

$this->and = new RulesAnd([], '', [], $this->expressionManager);
$this->and = new RulesAnd([], '', [], $this->expressionManager->reveal());
}

/**
* Tests one condition.
*/
public function testOneCondition() {
// The method on the test condition must be called once.
$this->trueConditionExpression->expects($this->once())
->method('executeWithState');

$this->and->addExpressionObject($this->trueConditionExpression);
$this->trueConditionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(1);
$this->and->addExpressionObject($this->trueConditionExpression->reveal());
$this->assertTrue($this->and->execute(), 'Single condition returns TRUE.');
}

Expand All @@ -58,13 +59,13 @@ public function testEmptyAnd() {
* Tests two true conditions.
*/
public function testTwoConditions() {
// The method on the test condition must be called once.
$this->trueConditionExpression->expects($this->exactly(2))
->method('executeWithState');
// The method on the test condition must be called twice.
$this->trueConditionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(2);

$this->and
->addExpressionObject($this->trueConditionExpression)
->addExpressionObject($this->trueConditionExpression);
->addExpressionObject($this->trueConditionExpression->reveal())
->addExpressionObject($this->trueConditionExpression->reveal());

$this->assertTrue($this->and->execute(), 'Two conditions returns TRUE.');
}
Expand All @@ -74,12 +75,12 @@ public function testTwoConditions() {
*/
public function testTwoFalseConditions() {
// The method on the test condition must be called once.
$this->falseConditionExpression->expects($this->once())
->method('executeWithState');
$this->falseConditionExpression->executeWithState(
Argument::type(RulesStateInterface::class))->shouldBeCalledTimes(1);

$this->and
->addExpressionObject($this->falseConditionExpression)
->addExpressionObject($this->falseConditionExpression);
->addExpressionObject($this->falseConditionExpression->reveal())
->addExpressionObject($this->falseConditionExpression->reveal());

$this->assertFalse($this->and->execute(), 'Two false conditions return FALSE.');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Unit/RulesConditionContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ protected function getMockConditionContainer(array $methods = [], $class = 'Rule
*/
public function testAddExpressionObject() {
$container = $this->getMockConditionContainer();
$container->addExpressionObject($this->trueConditionExpression);
$container->addExpressionObject($this->trueConditionExpression->reveal());

$property = new \ReflectionProperty($container, 'conditions');
$property->setAccessible(TRUE);

$this->assertArrayEquals([$this->trueConditionExpression], $property->getValue($container));
$this->assertArrayEquals([$this->trueConditionExpression->reveal()], $property->getValue($container));
}

/**
Expand Down
Loading

0 comments on commit 31e90bb

Please sign in to comment.