class DataConvertTest

@coversDefaultClass \Drupal\rules\Plugin\RulesAction\DataConvert @group RulesAction

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\Integration\RulesIntegrationTestBase extends \Drupal\Tests\UnitTestCase
      • class \Drupal\Tests\rules\Unit\Integration\RulesAction\DataConvertTest extends \Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase

Expanded class hierarchy of DataConvertTest

File

tests/src/Unit/Integration/RulesAction/DataConvertTest.php, line 12

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction
View source
class DataConvertTest extends RulesIntegrationTestBase {
    
    /**
     * The action to be tested.
     *
     * @var \Drupal\rules\Core\RulesActionInterface
     */
    protected $action;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->action = $this->actionManager
            ->createInstance('rules_data_convert');
    }
    
    /**
     * Test the conversion and rounding to integer.
     *
     * @covers ::execute
     */
    public function testConvertToInteger() {
        $value = 1.5;
        // Test the conversion to integer.
        $converted = $this->executeAction($value, 'integer');
        $this->assertIsInt($converted->getValue());
        $this->assertEquals('integer', $converted->getDataDefinition()
            ->getDataType());
        // Test the conversion to integer and floor down.
        $converted = $this->executeAction($value, 'integer', 'down');
        $this->assertIsInt($converted->getValue());
        $this->assertEquals(1, $converted->getValue());
        $this->assertEquals('integer', $converted->getDataDefinition()
            ->getDataType());
        // Test the conversion to integer and ceil up.
        $converted = $this->executeAction($value, 'integer', 'up');
        $this->assertIsInt($converted->getValue());
        $this->assertEquals('integer', $converted->getDataDefinition()
            ->getDataType());
        $this->assertEquals(2, $converted->getValue());
        // Test the conversion to integer and round.
        $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());
    }
    
    /**
     * Test the conversion to float.
     *
     * @covers ::execute
     */
    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());
    }
    
    /**
     * Test the conversion to text.
     *
     * @covers ::execute
     */
    public function testConvertToString() {
        // Test the conversion to test/string.
        $value = 1.5;
        $converted = $this->executeAction($value, 'string');
        $this->assertIsString($converted->getValue());
        $this->assertEquals('string', $converted->getDataDefinition()
            ->getDataType());
        $this->assertEquals('1.5', $converted->getValue());
    }
    
    /**
     * Test the behavior if nonsense context values is set.
     *
     * @covers ::execute
     */
    public function testInvalidValueException() {
        // Set the expected exception class and message.
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Only scalar values are supported.');
        $this->executeAction([
            'some-array',
        ], 'integer');
    }
    
    /**
     * Test the behavior if rounding behavior is used with non integers.
     *
     * @covers ::execute
     */
    public function testInvalidRoundingBehavior() {
        // Set the expected exception class and message.
        $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());
    }
    
    /**
     * Test the behavior if nonsense rounding_behaviors is set.
     *
     * @covers ::execute
     */
    public function testInvalidRoundingBehaviorException() {
        // Set the expected exception class and message.
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Unknown rounding behavior: invalid rounding');
        $value = 5.5;
        $rounding_behavior = 'invalid rounding';
        $this->executeAction($value, 'integer', $rounding_behavior);
    }
    
    /**
     * Test the behavior if nonsense target_type is set.
     *
     * @covers ::execute
     */
    public function testInvalidTargetTypeException() {
        // Set the expected exception class and message.
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Unknown target type: invalid type');
        $value = 5.5;
        $target_type = 'invalid type';
        $this->executeAction($value, $target_type);
    }
    
    /**
     * Test that the provided context variable is the correct type.
     *
     * @covers ::refineContextDefinitions
     */
    public function testRefiningContextDefinitions() {
        // Before context refinement, conversion_result data type defaults to 'any'.
        $this->assertEquals('any', $this->action
            ->getProvidedContextDefinition('conversion_result')
            ->getDataType());
        $this->action
            ->setContextValue('target_type', 'date_iso8601');
        $this->action
            ->refineContextDefinitions([]);
        // After context refinement, data type is whatever we set target_type to.
        $this->assertEquals('date_iso8601', $this->action
            ->getProvidedContextDefinition('conversion_result')
            ->getDataType());
    }
    
    /**
     * Shortcut method to execute the convert action and to avoid duplicate code.
     *
     * @param mixed $value
     *   The value to be converted.
     * @param string $target_type
     *   The target type $value should be converted to.
     * @param null|string $rounding_behavior
     *   Definition for the rounding direction.
     *
     * @return \Drupal\Core\TypedData\TypedDataInterface
     *   The raw conversion result as a typed data object.
     */
    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();
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DataConvertTest::$action protected property The action to be tested.
DataConvertTest::executeAction protected function Shortcut method to execute the convert action and to avoid duplicate code.
DataConvertTest::setUp protected function Overrides RulesIntegrationTestBase::setUp
DataConvertTest::testConvertToFloat public function Test the conversion to float.
DataConvertTest::testConvertToInteger public function Test the conversion and rounding to integer.
DataConvertTest::testConvertToString public function Test the conversion to text.
DataConvertTest::testInvalidRoundingBehavior public function Test the behavior if rounding behavior is used with non integers.
DataConvertTest::testInvalidRoundingBehaviorException public function Test the behavior if nonsense rounding_behaviors is set.
DataConvertTest::testInvalidTargetTypeException public function Test the behavior if nonsense target_type is set.
DataConvertTest::testInvalidValueException public function Test the behavior if nonsense context values is set.
DataConvertTest::testRefiningContextDefinitions public function Test that the provided context variable is the correct type.
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.
RulesIntegrationTestBase::$actionManager protected property
RulesIntegrationTestBase::$cacheBackend protected property
RulesIntegrationTestBase::$classResolver protected property The class resolver mock for the typed data manager.
RulesIntegrationTestBase::$conditionManager protected property
RulesIntegrationTestBase::$container protected property The Drupal service container.
RulesIntegrationTestBase::$dataFetcher protected property The data fetcher service.
RulesIntegrationTestBase::$dataFilterManager protected property The data filter manager.
RulesIntegrationTestBase::$enabledModules protected property Array object keyed with module names and TRUE as value.
RulesIntegrationTestBase::$entityFieldManager protected property
RulesIntegrationTestBase::$entityTypeBundleInfo protected property
RulesIntegrationTestBase::$entityTypeManager protected property
RulesIntegrationTestBase::$fieldTypeCategoryManager protected property The field type category info plugin manager.
RulesIntegrationTestBase::$logger protected property A mocked Rules logger.channel.rules_debug service. 6
RulesIntegrationTestBase::$messenger protected property The messenger service.
RulesIntegrationTestBase::$moduleHandler protected property
RulesIntegrationTestBase::$namespaces protected property All setup'ed namespaces.
RulesIntegrationTestBase::$placeholderResolver protected property The placeholder resolver service.
RulesIntegrationTestBase::$rulesDataProcessorManager protected property
RulesIntegrationTestBase::$rulesExpressionManager protected property
RulesIntegrationTestBase::$typedDataManager protected property
RulesIntegrationTestBase::constructModulePath protected function Determines the path to a module's class files.
RulesIntegrationTestBase::enableModule protected function Fakes the enabling of a module and adds its namespace for plugin loading.
RulesIntegrationTestBase::getTypedData protected function Returns a typed data object.
RulesIntegrationTestBase::prophesizeEntity protected function Helper method to mock irrelevant cache methods on entities.
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