View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
use Drupal\rules\Exception\InvalidArgumentException;
class DataConvertTest extends RulesIntegrationTestBase {
protected $action;
protected function setUp() : void {
parent::setUp();
$this->action = $this->actionManager
->createInstance('rules_data_convert');
}
public function testConvertToInteger() {
$value = 1.5;
$converted = $this
->executeAction($value, 'integer');
$this
->assertIsInt($converted
->getValue());
$this
->assertEquals('integer', $converted
->getDataDefinition()
->getDataType());
$converted = $this
->executeAction($value, 'integer', 'down');
$this
->assertIsInt($converted
->getValue());
$this
->assertEquals(1, $converted
->getValue());
$this
->assertEquals('integer', $converted
->getDataDefinition()
->getDataType());
$converted = $this
->executeAction($value, 'integer', 'up');
$this
->assertIsInt($converted
->getValue());
$this
->assertEquals('integer', $converted
->getDataDefinition()
->getDataType());
$this
->assertEquals(2, $converted
->getValue());
$converted = $this
->executeAction($value, 'integer', 'round');
$this
->assertIsInt($converted
->getValue());
$this
->assertEquals('integer', $converted
->getDataDefinition()
->getDataType());
$this
->assertEquals(2, $converted
->getValue());
$converted = $this
->executeAction('+123', 'integer');
$this
->assertIsInt($converted
->getValue());
$this
->assertEquals('integer', $converted
->getDataDefinition()
->getDataType());
$this
->assertEquals(123, $converted
->getValue());
}
public function testConvertToFloat() {
$value = '1.5';
$converted = $this
->executeAction($value, 'float');
$this
->assertIsFloat($converted
->getValue());
$this
->assertEquals('float', $converted
->getDataDefinition()
->getDataType());
$this
->assertEquals(1.5, $converted
->getValue());
$converted = $this
->executeAction('+1.5', 'float');
$this
->assertIsFloat($converted
->getValue());
$this
->assertEquals('float', $converted
->getDataDefinition()
->getDataType());
$this
->assertEquals(1.5, $converted
->getValue());
}
public function testConvertToString() {
$value = 1.5;
$converted = $this
->executeAction($value, 'string');
$this
->assertIsString($converted
->getValue());
$this
->assertEquals('string', $converted
->getDataDefinition()
->getDataType());
$this
->assertEquals('1.5', $converted
->getValue());
}
public function testInvalidValueException() {
$this
->expectException(InvalidArgumentException::class);
$this
->expectExceptionMessage('Only scalar values are supported.');
$this
->executeAction([
'some-array',
], 'integer');
}
public function testInvalidRoundingBehavior() {
$this
->expectException(InvalidArgumentException::class);
$this
->expectExceptionMessage('A rounding behavior only makes sense with an integer target type.');
$converted = $this
->executeAction('some', 'decimal', 'down');
$this
->assertIsFloat($converted
->getValue());
$this
->assertEquals('float', $converted
->getDataDefinition()
->getDataType());
}
public function testInvalidRoundingBehaviorException() {
$this
->expectException(InvalidArgumentException::class);
$this
->expectExceptionMessage('Unknown rounding behavior: invalid rounding');
$value = 5.5;
$rounding_behavior = 'invalid rounding';
$this
->executeAction($value, 'integer', $rounding_behavior);
}
public function testInvalidTargetTypeException() {
$this
->expectException(InvalidArgumentException::class);
$this
->expectExceptionMessage('Unknown target type: invalid type');
$value = 5.5;
$target_type = 'invalid type';
$this
->executeAction($value, $target_type);
}
public function testRefiningContextDefinitions() {
$this
->assertEquals('any', $this->action
->getProvidedContextDefinition('conversion_result')
->getDataType());
$this->action
->setContextValue('target_type', 'date_iso8601');
$this->action
->refineContextDefinitions([]);
$this
->assertEquals('date_iso8601', $this->action
->getProvidedContextDefinition('conversion_result')
->getDataType());
}
protected function executeAction($value, $target_type, $rounding_behavior = NULL) {
$this->action
->setContextValue('value', $value);
$this->action
->setContextValue('target_type', $target_type);
if (!empty($rounding_behavior)) {
$this->action
->setContextValue('rounding_behavior', $rounding_behavior);
}
$this->action
->refineContextDefinitions([]);
$this->action
->execute();
$result = $this->action
->getProvidedContext('conversion_result');
return $result
->getContextData();
}
}