You are here

class TwigConvertTest in Twig Tools 8

Tests to ensure conversions filters work correctly.

@group twig_tools

@coversDefaultClass \Drupal\twig_tools\TwigExtension\TwigConvert

Hierarchy

Expanded class hierarchy of TwigConvertTest

File

tests/src/Unit/TwigConvertTest.php, line 16

Namespace

Drupal\Tests\twig_tools\Unit
View source
class TwigConvertTest extends UnitTestCase {

  /**
   * Create a new TwigExtension object.
   */
  public function setUp() {
    parent::setUp();
    $loader = new StringLoader();
    $this->twig = new \Twig_Environment($loader);
    $twigTools = new TwigConvert();
    $this->twig
      ->addExtension($twigTools);
  }

  /**
   * @covers ::booleanValue
   *
   * @dataProvider providerTestBooleanValues
   */
  public function testBooleanValue($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testBooleanValue.
   *
   * @return array
   *   An array of test data and their boolean equivalents.
   */
  public function providerTestBooleanValues() {
    return [
      [
        "{{ 0|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
      [
        "{{ 42|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ 0.0|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
      [
        "{{ -1|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ 4.2|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ ''|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
      [
        "{{ 'string'|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ 'true'|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ 'false'|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ '0'|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
      [
        "{{ '1'|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ [1, 2]|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ [0]|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ [0, 0]|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ []|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
      [
        "{{ false|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
      [
        "{{ true|boolean is same as (false) ? 'false' : 'true' }}",
        "true",
      ],
      [
        "{{ null|boolean is same as (false) ? 'false' : 'true' }}",
        "false",
      ],
    ];
  }

  /**
   * @covers ::integerValue
   *
   * @dataProvider providerTestIntegerValues
   */
  public function testIntegerValue($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testIntegerValue.
   *
   * @return array
   *   An array of test data and their integer equivalents.
   */
  public function providerTestIntegerValues() {
    return [
      [
        "{{ 42|integer }}",
        '42',
      ],
      [
        "{{ 4.2|integer }}",
        '4',
      ],
      [
        "{{ '42'|integer }}",
        '42',
      ],
      [
        "{{ '+42'|integer }}",
        '42',
      ],
      [
        "{{ '-42'|integer }}",
        '-42',
      ],
      [
        "{{ 042|integer }}",
        '42',
      ],
      [
        "{{ '042'|integer }}",
        '42',
      ],
      [
        "{{ 42000000|integer }}",
        '42000000',
      ],
      [
        "{{ []|integer }}",
        '0',
      ],
      [
        "{{ ['foo', 'bar']|integer }}",
        '1',
      ],
      [
        "{{ FALSE|integer }}",
        '0',
      ],
      [
        "{{ TRUE|integer }}",
        '1',
      ],
      [
        "{{ NULL|integer }}",
        '0',
      ],
      [
        "{{ 0|integer }}",
        '0',
      ],
      [
        "{{ 1|integer }}",
        '1',
      ],
      [
        "{{ 0.0|integer }}",
        '0',
      ],
      [
        "{{ 1.0|integer }}",
        '1',
      ],
    ];
  }

  /**
   * @covers ::floatValue
   *
   * @dataProvider providerTestFloatValues
   */
  public function testFloatValue($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testFloatValue.
   *
   * @return array
   *   An array of test data and their float equivalents.
   */
  public function providerTestFloatValues() {
    return [
      [
        "{{ 42|float }}",
        '42',
      ],
      [
        "{{ 4.2|float }}",
        '4.2',
      ],
      [
        "{{ 0.42|float }}",
        '0.42',
      ],
      [
        "{{ 42000000.00|float }}",
        '42000000',
      ],
      [
        "{{ 42.0000000|float }}",
        '42',
      ],
      [
        "{{ -42.0000000|float }}",
        '-42',
      ],
      [
        "{{ +42.0000000|float }}",
        '42',
      ],
      [
        "{{ 42.00000001|float }}",
        '42.00000001',
      ],
      [
        "{{ 0000042.00000001|float }}",
        '42.00000001',
      ],
      [
        "{{ '42.00000001The'|float }}",
        '42.00000001',
      ],
      [
        "{{ 'The42.00000001'|float }}",
        '0',
      ],
      [
        "{{ '42'|float }}",
        '42',
      ],
      [
        "{{ '+42'|float }}",
        '42',
      ],
      [
        "{{ '-42'|float }}",
        '-42',
      ],
      [
        "{{ 042|float }}",
        '42',
      ],
      [
        "{{ '042'|float }}",
        '42',
      ],
      [
        "{{ 42000000|float }}",
        '42000000',
      ],
      [
        "{{ []|float }}",
        '0',
      ],
      [
        "{{ ['foo', 'bar']|float }}",
        '1',
      ],
      [
        "{{ FALSE|float }}",
        '0',
      ],
      [
        "{{ TRUE|float }}",
        '1',
      ],
      [
        "{{ NULL|float }}",
        '0',
      ],
      [
        "{{ 0|float }}",
        '0',
      ],
      [
        "{{ 1|float }}",
        '1',
      ],
      [
        "{{ 0.0|float }}",
        '0',
      ],
      [
        "{{ 1.0|float }}",
        '1',
      ],
    ];
  }

  /**
   * @covers ::stringValue
   *
   * @dataProvider providerTestStringValues
   */
  public function testStringValue($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testStringValue.
   *
   * @return array
   *   An array of test data and their string equivalents.
   */
  public function providerTestStringValues() {
    return [
      [
        "{{ 42|string }}",
        "42",
      ],
      [
        "{{ 4.2|string }}",
        "4.2",
      ],
      [
        "{{ '42'|string }}",
        "42",
      ],
      [
        "{{ '+42'|string }}",
        "+42",
      ],
      [
        "{{ '-42'|string }}",
        "-42",
      ],
      [
        "{{ 042|string }}",
        "42",
      ],
      [
        "{{ '042'|string }}",
        "042",
      ],
      [
        "{{ 42000000|string }}",
        "42000000",
      ],
      [
        "{{ FALSE|string }}",
        "",
      ],
      [
        "{{ TRUE|string }}",
        "1",
      ],
      [
        "{{ NULL|string }}",
        "",
      ],
      [
        "{{ 0|string }}",
        "0",
      ],
      [
        "{{ 1|string }}",
        "1",
      ],
      [
        "{{ 0.0|string }}",
        "0",
      ],
      [
        "{{ 1.0|string }}",
        "1",
      ],
    ];
  }

  /**
   * @covers ::md5Value
   *
   * @dataProvider providerTestMd5Values
   */
  public function testMd5Value($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testMd5Value.
   *
   * @return array
   *   An array of test data and their md5 hash equivalents.
   */
  public function providerTestMd5Values() {
    return [
      [
        "{{ '42'|md5 }}",
        'a1d0c6e83f027327d8461063f4ac58a6',
      ],
      [
        "{{ '4.2'|md5 }}",
        '8653d5c7898950016e5d019df6815626',
      ],
      [
        "{{ '+42'|md5 }}",
        'deda8ddbf790f3682d5cf69d237bb0b2',
      ],
      [
        "{{ '-42'|md5 }}",
        '8dfcb89fd8620e3e7fb6a03a53f307dc',
      ],
      [
        "{{ '42'|md5 }}",
        'a1d0c6e83f027327d8461063f4ac58a6',
      ],
      [
        "{{ 'Test'|md5 }}",
        '0cbc6611f5540bd0809a388dc95a615b',
      ],
      [
        "{{ '0'|md5 }}",
        'cfcd208495d565ef66e7dff9f98764da',
      ],
      [
        "{{ '0'|md5 }}",
        'cfcd208495d565ef66e7dff9f98764da',
      ],
      [
        "{{ '1'|md5 }}",
        'c4ca4238a0b923820dcc509a6f75849b',
      ],
    ];
  }

  /**
   * @covers ::jsonDecode
   *
   * @dataProvider providerTestJsonDecodeValues
   */
  public function testJsonDecode($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for jsonDecode.
   *
   * @return array
   *   An array of test JSON strings and their rendered equivalents.
   */
  public function providerTestJsonDecodeValues() {
    return [
      [
        '{% set json = \'{
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
        "e": 5
      }\'|json_decode(true) %}{{ json|join(",") }}',
        '1,2,3,4,5',
      ],
      [
        '{% set json = \'{
        "aliceblue": "#f0f8ff",
        "antiquewhite": "#faebd7",
        "aqua": "#00ffff",
        "aquamarine": "#7fffd4",
        "azure": "#f0ffff",
        "beige": "#f5f5dc",
        "bisque": "#ffe4c4",
        "black": "#000000",
        "blanchedalmond": "#ffebcd",
        "blue": "#0000ff",
        "blueviolet": "#8a2be2",
        "brown": "#a52a2a"
      }\'|json_decode %}{{ json|join(", ") }}',
        '#f0f8ff, #faebd7, #00ffff, #7fffd4, #f0ffff, #f5f5dc, #ffe4c4, #000000, #ffebcd, #0000ff, #8a2be2, #a52a2a',
      ],
      [
        '{% set json = \'{
        "string": "string",
        "boolean_true": true,
        "boolean_false": false,
        "integer": 42,
        "float": 4.2
      }\'|json_decode %}{{ json|join(", ") }}',
        'string, 1, , 42, 4.2',
      ],
    ];
  }

  /**
   * @covers ::dateFromFormat
   *
   * @dataProvider providerTestDateFromFormat
   */
  public function testDateFromFormat($template, $expected) {
    $result = $this->twig
      ->render($template);
    $this
      ->assertSame($expected, $result);
  }

  /**
   * Provides test data for testDateFromFormat.
   *
   * @return array
   *   An array of test 'from' and 'to' date formats.
   */
  public function providerTestDateFromFormat() {
    return [
      [
        "{{ '2009-Feb-15'|date_from_format('Y-M-j', 'm/d/y') }}",
        "02/15/09",
      ],
      [
        "{{ '2010-04-23 10:29:35'|date_from_format('Y-m-d H:i:s', 'Y-m-d\\\\TH:i:s', 'America/New_York', 'America/New_York') }}",
        "2010-04-23T10:29:35",
      ],
      [
        "{{ '2010-04-23 00:00:00'|date_from_format('Y-m-d H:i:s', 'c', 'America/New_York', 'UTC') }}",
        "2010-04-23T04:00:00+00:00",
      ],
      [
        "{{ '2019-01-01 00:00:00'|date_from_format('Y-m-d H:i:s', 'Y-m-d\\\\TH:i:s', 'America/New_York', 'America/Los_Angeles') }}",
        "2018-12-31T21:00:00",
      ],
      [
        "{{ '1 1 19'|date_from_format('!j n y', 'Y-m-j\\\\TH:i:s', 'America/New_York', 'America/Los_Angeles') }}",
        "2018-12-31T21:00:00",
      ],
      [
        "{{ 0|date_from_format('U', 'Y-m-d') }}",
        "1970-01-01",
      ],
      [
        "{{ '0'|date_from_format('U', 'Y-m-d') }}",
        "1970-01-01",
      ],
      [
        "{{ '0000-00-00 00:00:00'|date_from_format('Y-m-d H:i:s', 'Y-m-d H:i:s', 'UTC', 'UTC') }}",
        "-0001-11-30 00:00:00",
      ],
      [
        "{{ '01/05/1955 10:43:22'|date_from_format('m/d/Y H:i:s', 'Y-m-d\\\\TH:i:s e') }}",
        "1955-01-05T10:43:22 Australia/Sydney",
      ],
      [
        "{{ '2004-11-19 10:25:33'|date_from_format('Y-m-d h:i:s', 'U', 'America/Managua') }}",
        "1100881533",
      ],
      [
        "{{ '2004-12-19 10:19:42'|date_from_format('Y-m-d H:i:s', 'Y-m-d H:i:s e', NULL, 'America/Managua') }}",
        "2004-12-18 17:19:42 America/Managua",
      ],
      [
        "{{ ''|date_from_format('m/d/Y', 'Y-m-d') }}",
        "",
      ],
    ];
  }

  /**
   * Unset the test object.
   */
  public function tearDown() {
    unset($this->twigTools, $this->twig);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TwigConvertTest::providerTestBooleanValues public function Provides test data for testBooleanValue.
TwigConvertTest::providerTestDateFromFormat public function Provides test data for testDateFromFormat.
TwigConvertTest::providerTestFloatValues public function Provides test data for testFloatValue.
TwigConvertTest::providerTestIntegerValues public function Provides test data for testIntegerValue.
TwigConvertTest::providerTestJsonDecodeValues public function Provides test data for jsonDecode.
TwigConvertTest::providerTestMd5Values public function Provides test data for testMd5Value.
TwigConvertTest::providerTestStringValues public function Provides test data for testStringValue.
TwigConvertTest::setUp public function Create a new TwigExtension object. Overrides UnitTestCase::setUp
TwigConvertTest::tearDown public function Unset the test object.
TwigConvertTest::testBooleanValue public function @covers ::booleanValue
TwigConvertTest::testDateFromFormat public function @covers ::dateFromFormat
TwigConvertTest::testFloatValue public function @covers ::floatValue
TwigConvertTest::testIntegerValue public function @covers ::integerValue
TwigConvertTest::testJsonDecode public function @covers ::jsonDecode
TwigConvertTest::testMd5Value public function @covers ::md5Value
TwigConvertTest::testStringValue public function @covers ::stringValue
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.