class ConditionExpressionTest
@coversDefaultClass \Drupal\rules\Plugin\RulesExpression\ConditionExpression @group Rules
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait
- class \Drupal\Tests\rules\Unit\ConditionExpressionTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ConditionExpressionTest
File
-
tests/
src/ Unit/ ConditionExpressionTest.php, line 21
Namespace
Drupal\Tests\rules\UnitView source
class ConditionExpressionTest extends UnitTestCase {
/**
* The mocked condition manager.
*
* @var \Drupal\rules\Core\ConditionManager|\Prophecy\Prophecy\ProphecyInterface
*/
protected $conditionManager;
/**
* The mocked data processor manager.
*
* @var \Drupal\rules\Context\DataProcessorManager|\Prophecy\Prophecy\ProphecyInterface
*/
protected $processorManager;
/**
* The condition object being tested.
*
* @var \Drupal\rules\Plugin\RulesExpression\ConditionExpression
*/
protected $conditionExpression;
/**
* A condition plugin that always evaluates to TRUE.
*
* @var \Drupal\rules\Core\RulesConditionInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $trueCondition;
/**
* The mocked expression manager object.
*
* @var \Drupal\rules\src\Logger\|\Prophecy\Prophecy\ProphecyInterface
*/
protected $rulesDebugLogger;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Create a test condition plugin that always evaluates to TRUE.
$this->trueCondition = $this->prophesize(RulesConditionInterface::class);
$this->trueCondition
->execute()
->willReturn(TRUE);
$this->trueCondition
->evaluate()
->willReturn(TRUE);
$this->conditionManager = $this->prophesize(ConditionManager::class);
$this->processorManager = $this->prophesize(DataProcessorManager::class);
$this->rulesDebugLogger = $this->prophesize(LoggerChannelInterface::class);
$this->conditionExpression = new ConditionExpression([
'condition_id' => 'test_condition',
], '', [
'label' => 'Test Condition',
], $this->conditionManager
->reveal(), $this->processorManager
->reveal(), $this->rulesDebugLogger
->reveal());
}
/**
* Tests that context values get data processed with processor mappings.
*/
public function testDataProcessor() {
$this->conditionManager
->createInstance('test_condition', [
'negate' => FALSE,
])
->willReturn($this->trueCondition
->reveal())
->shouldBeCalledTimes(1);
$this->conditionManager
->getDefinition('test_condition')
->willReturn([
'label' => 'Test Condition',
])
->shouldBeCalledTimes(1);
$condition = new ConditionExpression([
'condition_id' => 'test_condition',
] + ContextConfig::create()->process('test', 'foo', [])
->toArray(), '', [
'label' => 'Test Condition',
], $this->conditionManager
->reveal(), $this->processorManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->trueCondition
->getContextDefinitions()
->willReturn([
'test' => $this->prophesize(ContextDefinitionInterface::class)
->reveal(),
])
->shouldBeCalledTimes(1);
$this->trueCondition
->getContextDefinition('test')
->willReturn($this->prophesize(ContextDefinitionInterface::class)
->reveal())
->shouldBeCalledTimes(1);
$this->trueCondition
->getProvidedContextDefinitions()
->willReturn([])
->shouldBeCalledTimes(1);
// Mock some original old value that will be replaced by the data processor.
$this->trueCondition
->getContextValue('test')
->willReturn('old_value')
->shouldBeCalled();
// The outcome of the data processor needs to get set on the condition.
$this->trueCondition
->setContextValue('test', 'new_value')
->shouldBeCalledTimes(1);
$this->trueCondition
->refineContextDefinitions([])
->shouldBeCalledTimes(1);
$data_processor = $this->prophesize(DataProcessorInterface::class);
$data_processor->process('old_value', Argument::any())
->willReturn('new_value')
->shouldBeCalledTimes(1);
$this->processorManager
->createInstance('foo', [])
->willReturn($data_processor->reveal())
->shouldBeCalledTimes(1);
// Build some mocked execution state.
$state = $this->prophesize(ExecutionStateInterface::class);
$prophecy = $state->getVariable('test');
/** @var \Prophecy\Prophecy\MethodProphecy $prophecy */
$prophecy->willReturn('old_value');
$this->assertTrue($condition->executeWithState($state->reveal()));
}
/**
* Tests that negating a condition works.
*/
public function testNegation() {
$this->trueCondition
->getContextDefinitions()
->willReturn([]);
$this->trueCondition
->refineContextDefinitions([])
->shouldBeCalledTimes(1);
$this->trueCondition
->getProvidedContextDefinitions()
->willReturn([])
->shouldBeCalledTimes(1);
$this->conditionManager
->createInstance('test_condition', [
'negate' => TRUE,
])
->willReturn($this->trueCondition
->reveal())
->shouldBeCalledTimes(1);
$this->conditionManager
->getDefinition('test_condition')
->willReturn([
'label' => 'Test Condition',
])
->shouldBeCalledTimes(1);
// Create a condition which is negated.
$condition_expression = new ConditionExpression([
'condition_id' => 'test_condition',
'negate' => TRUE,
], '', [
'label' => 'Test Condition',
], $this->conditionManager
->reveal(), $this->processorManager
->reveal(), $this->rulesDebugLogger
->reveal());
$condition_expression->setStringTranslation($this->getStringTranslationStub());
$this->assertFalse($condition_expression->execute());
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
ConditionExpressionTest::$conditionExpression | protected | property | The condition object being tested. | |||
ConditionExpressionTest::$conditionManager | protected | property | The mocked condition manager. | |||
ConditionExpressionTest::$processorManager | protected | property | The mocked data processor manager. | |||
ConditionExpressionTest::$rulesDebugLogger | protected | property | The mocked expression manager object. | |||
ConditionExpressionTest::$trueCondition | protected | property | A condition plugin that always evaluates to TRUE. | |||
ConditionExpressionTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
ConditionExpressionTest::testDataProcessor | public | function | Tests that context values get data processed with processor mappings. | |||
ConditionExpressionTest::testNegation | public | function | Tests that negating a condition works. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
UnitTestCase::$randomGenerator | protected | property | The random generator. | |||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::assertArrayEquals | Deprecated | protected | function | Asserts if two arrays are equal by sorting them first. | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. | |||
UnitTestCase::setUpBeforeClass | public static | function |