You are here

class FieldValueTest in Twig Field Value 8

Same name in this branch
  1. 8 tests/src/Kernel/FieldValueTest.php \Drupal\Tests\twig_field_value\Kernel\FieldValueTest
  2. 8 tests/src/Unit/FieldValue/FieldValueTest.php \Drupal\Tests\twig_field_value\Unit\FieldValue\FieldValueTest
Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/FieldValue/FieldValueTest.php \Drupal\Tests\twig_field_value\Unit\FieldValue\FieldValueTest

@coversDefaultClass \Drupal\twig_field_value\Twig\Extension\FieldValueExtension @group twig_field_value

Hierarchy

Expanded class hierarchy of FieldValueTest

File

tests/src/Unit/FieldValue/FieldValueTest.php, line 12

Namespace

Drupal\Tests\twig_field_value\Unit\FieldValue
View source
class FieldValueTest extends UnitTestCase {

  /**
   * The Twig extension under test.
   *
   * @var \Drupal\twig_field_value\Twig\Extension\FieldValueExtension
   */
  protected $extension;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->extension = new FieldValueExtension();
  }

  /**
   * Asserts the twig field_label filter.
   *
   * @dataProvider providerTestFieldLabel
   * @covers ::getFieldLabel
   *
   * @param $expected_result
   * @param $render_array
   */
  public function testFieldLabel($expected_result, $render_array) {
    $result = $this->extension
      ->getFieldLabel($render_array);
    $this
      ->assertSame($expected_result, $result);
  }

  /**
   * Provides data and expected results for the test method.
   *
   * @return array
   *   Data and expected results.
   */
  public function providerTestFieldLabel() {
    return [
      [
        NULL,
        [],
      ],
      [
        NULL,
        [
          '#title' => 'foo',
        ],
      ],
      [
        NULL,
        [
          '#theme' => 'item_list',
        ],
      ],
      [
        NULL,
        [
          '#theme' => 'field',
        ],
      ],
      [
        'My title',
        [
          '#theme' => 'field',
          '#title' => 'My title',
        ],
      ],
    ];
  }

  /**
   * Asserts the twig field_value filter.
   *
   * @dataProvider providerTestFieldValue
   * @covers ::getFieldValue
   *
   * @param $expected_result
   * @param $render_array
   */
  public function testFieldValue($expected_result, $render_array) {
    $result = $this->extension
      ->getFieldValue($render_array);
    $this
      ->assertSame($expected_result, $result);
  }

  /**
   * Provides data and expected results for the test method.
   *
   * @return array
   *   Data and expected results.
   */
  public function providerTestFieldValue() {
    return [
      [
        NULL,
        NULL,
      ],
      [
        NULL,
        [],
      ],
      [
        NULL,
        [
          '#items' => 'foo',
        ],
      ],
      [
        NULL,
        [
          '#theme' => 'item_list',
        ],
      ],
      [
        NULL,
        [
          '#theme' => 'field',
        ],
      ],
      [
        [
          0 => [
            '#markup' => 'this value',
          ],
        ],
        [
          '#theme' => 'field',
          '#items' => [
            0 => 'dummy',
          ],
          0 => [
            '#markup' => 'this value',
          ],
        ],
      ],
      [
        [
          0 => [
            '#markup' => 'zero',
          ],
          2 => [
            '#markup' => 'two',
          ],
          3 => [
            '#markup' => 'three',
          ],
        ],
        [
          '#theme' => 'field',
          '#items' => [
            0 => 'dummy',
            1 => 'dummy',
            2 => 'dummy',
            3 => 'dummy',
          ],
          0 => [
            '#markup' => 'zero',
          ],
          2 => [
            '#markup' => 'two',
          ],
          3 => [
            '#markup' => 'three',
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldValueTest::$extension protected property The Twig extension under test.
FieldValueTest::providerTestFieldLabel public function Provides data and expected results for the test method.
FieldValueTest::providerTestFieldValue public function Provides data and expected results for the test method.
FieldValueTest::setUp protected function Overrides UnitTestCase::setUp
FieldValueTest::testFieldLabel public function Asserts the twig field_label filter.
FieldValueTest::testFieldValue public function Asserts the twig field_value filter.
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.
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.