View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
class DataCalculateValueTest extends RulesIntegrationTestBase {
protected $action;
protected function setUp() : void {
parent::setUp();
$this->action = $this->actionManager
->createInstance('rules_data_calculate_value');
}
public function testSummary() {
$this
->assertEquals('Calculate a numeric value', $this->action
->summary());
}
public function testAdditionAction() {
$input_1 = mt_rand();
$input_2 = mt_rand();
$this->action
->setContextValue('input_1', $this
->getTypedData('float', $input_1))
->setContextValue('operator', $this
->getTypedData('string', '+'))
->setContextValue('input_2', $this
->getTypedData('float', $input_2));
$this->action
->execute();
$result = $this->action
->getProvidedContext('result')
->getContextValue();
$this
->assertEquals($input_1 + $input_2, $result, "Addition calculation correct");
}
public function testSubtractionAction() {
$input_1 = mt_rand();
$input_2 = mt_rand();
$this->action
->setContextValue('input_1', $this
->getTypedData('float', $input_1))
->setContextValue('operator', $this
->getTypedData('string', '-'))
->setContextValue('input_2', $this
->getTypedData('float', $input_2));
$this->action
->execute();
$result = $this->action
->getProvidedContext('result')
->getContextValue();
$this
->assertEquals($input_1 - $input_2, $result, "Subtraction calculation correct");
}
public function testMultiplicationAction() {
$input_1 = mt_rand();
$input_2 = mt_rand();
$this->action
->setContextValue('input_1', $this
->getTypedData('float', $input_1))
->setContextValue('operator', $this
->getTypedData('string', '*'))
->setContextValue('input_2', $this
->getTypedData('float', $input_2));
$this->action
->execute();
$result = $this->action
->getProvidedContext('result')
->getContextValue();
$this
->assertEquals($input_1 * $input_2, $result, "Subtraction calculation correct");
}
public function testDivisionAction() {
$input_1 = mt_rand();
$input_2 = mt_rand();
$this->action
->setContextValue('input_1', $this
->getTypedData('float', $input_1))
->setContextValue('operator', $this
->getTypedData('string', '/'))
->setContextValue('input_2', $this
->getTypedData('float', $input_2));
$this->action
->execute();
$result = $this->action
->getProvidedContext('result')
->getContextValue();
$this
->assertEquals($input_1 / $input_2, $result, "Subtraction calculation correct");
}
public function testMinimumAction() {
$input_1 = mt_rand();
$input_2 = mt_rand();
$this->action
->setContextValue('input_1', $this
->getTypedData('float', $input_1))
->setContextValue('operator', $this
->getTypedData('string', 'min'))
->setContextValue('input_2', $this
->getTypedData('float', $input_2));
$this->action
->execute();
$result = $this->action
->getProvidedContext('result')
->getContextValue();
$this
->assertEquals(min($input_1, $input_2), $result, "Min calculation correct");
}
public function testMaximumAction() {
$input_1 = mt_rand();
$input_2 = mt_rand();
$this->action
->setContextValue('input_1', $this
->getTypedData('float', $input_1))
->setContextValue('operator', $this
->getTypedData('string', 'max'))
->setContextValue('input_2', $this
->getTypedData('float', $input_2));
$this->action
->execute();
$result = $this->action
->getProvidedContext('result')
->getContextValue();
$this
->assertEquals(max($input_1, $input_2), $result, "Max calculation correct");
}
}