function DataConvertTest::testConvertToInteger

Test the conversion and rounding to integer.

@covers ::execute

File

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

Class

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

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

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());
}