You are here

class VariableTest in Zircon Profile 8

Same name in this branch
  1. 8 core/tests/Drupal/Tests/Component/Utility/VariableTest.php \Drupal\Tests\Component\Utility\VariableTest
  2. 8 core/modules/migrate_drupal/tests/src/Unit/source/VariableTest.php \Drupal\Tests\migrate_drupal\Unit\source\VariableTest
Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Utility/VariableTest.php \Drupal\Tests\Component\Utility\VariableTest

Test variable export functionality in Variable component.

@group Variable @group Utility

@coversDefaultClass \Drupal\Component\Utility\Variable

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of VariableTest

File

core/tests/Drupal/Tests/Component/Utility/VariableTest.php, line 21
Contains \Drupal\Tests\Component\Utility\VariableTest.

Namespace

Drupal\Tests\Component\Utility
View source
class VariableTest extends UnitTestCase {

  /**
   * Data provider for testExport().
   *
   * @return array
   *   An array containing:
   *     - The expected export string.
   *     - The variable to export.
   */
  public function providerTestExport() {
    return array(
      // Array.
      array(
        'array()',
        array(),
      ),
      array(
        // non-associative.
        "array(\n  1,\n  2,\n  3,\n  4,\n)",
        array(
          1,
          2,
          3,
          4,
        ),
      ),
      array(
        // associative.
        "array(\n  'a' => 1,\n)",
        array(
          'a' => 1,
        ),
      ),
      // Bool.
      array(
        'TRUE',
        TRUE,
      ),
      array(
        'FALSE',
        FALSE,
      ),
      // Strings.
      array(
        "'string'",
        'string',
      ),
      array(
        '"\\n\\r\\t"',
        "\n\r\t",
      ),
      array(
        // 2 backslashes. \\
        "'\\'",
        '\\',
      ),
      array(
        // Double-quote "
        "'\"'",
        "\"",
      ),
      array(
        // Single-quote '
        '"\'"',
        "'",
      ),
      array(
        // Quotes with $ symbols.
        '"\\$settings[\'foo\']"',
        '$settings[\'foo\']',
      ),
      // Object.
      array(
        // A stdClass object.
        '(object) array()',
        new \stdClass(),
      ),
      array(
        // A not-stdClass object.
        "Drupal\\Tests\\Component\\Utility\\StubVariableTestClass::__set_state(array(\n))",
        new StubVariableTestClass(),
      ),
    );
  }

  /**
   * Tests exporting variables.
   *
   * @dataProvider providerTestExport
   * @covers ::export
   *
   * @param string $expected
   *   The expected exported variable.
   * @param mixed $variable
   *   The variable to be exported.
   */
  public function testExport($expected, $variable) {
    $this
      ->assertEquals($expected, Variable::export($variable));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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::setUp protected function 259
VariableTest::providerTestExport public function Data provider for testExport().
VariableTest::testExport public function Tests exporting variables.