VariableTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Component\Utility\VariableTest.
  5. */
  6. namespace Drupal\Tests\Component\Utility;
  7. use Drupal\Component\Utility\Variable;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * Test variable export functionality in Variable component.
  11. *
  12. * @group Variable
  13. * @group Utility
  14. *
  15. * @coversDefaultClass \Drupal\Component\Utility\Variable
  16. */
  17. class VariableTest extends TestCase {
  18. /**
  19. * Data provider for testExport().
  20. *
  21. * @return array
  22. * An array containing:
  23. * - The expected export string.
  24. * - The variable to export.
  25. */
  26. public function providerTestExport() {
  27. return [
  28. // Array.
  29. [
  30. 'array()',
  31. [],
  32. ],
  33. [
  34. // non-associative.
  35. "array(\n 1,\n 2,\n 3,\n 4,\n)",
  36. [1, 2, 3, 4],
  37. ],
  38. [
  39. // associative.
  40. "array(\n 'a' => 1,\n)",
  41. ['a' => 1],
  42. ],
  43. // Bool.
  44. [
  45. 'TRUE',
  46. TRUE,
  47. ],
  48. [
  49. 'FALSE',
  50. FALSE,
  51. ],
  52. // Strings.
  53. [
  54. "'string'",
  55. 'string',
  56. ],
  57. [
  58. '"\n\r\t"',
  59. "\n\r\t",
  60. ],
  61. [
  62. // 2 backslashes. \\
  63. "'\\'",
  64. '\\',
  65. ],
  66. [
  67. // Double-quote "
  68. "'\"'",
  69. "\"",
  70. ],
  71. [
  72. // Single-quote '
  73. '"\'"',
  74. "'",
  75. ],
  76. [
  77. // Quotes with $ symbols.
  78. '"\$settings[\'foo\']"',
  79. '$settings[\'foo\']',
  80. ],
  81. // Object.
  82. [
  83. // A stdClass object.
  84. '(object) array()',
  85. new \stdClass(),
  86. ],
  87. [
  88. // A not-stdClass object.
  89. "Drupal\Tests\Component\Utility\StubVariableTestClass::__set_state(array(\n))",
  90. new StubVariableTestClass(),
  91. ],
  92. ];
  93. }
  94. /**
  95. * Tests exporting variables.
  96. *
  97. * @dataProvider providerTestExport
  98. * @covers ::export
  99. *
  100. * @param string $expected
  101. * The expected exported variable.
  102. * @param mixed $variable
  103. * The variable to be exported.
  104. */
  105. public function testExport($expected, $variable) {
  106. $this->assertEquals($expected, Variable::export($variable));
  107. }
  108. }
  109. /**
  110. * No-op test class for VariableTest::testExport().
  111. *
  112. * @see Drupal\Tests\Component\Utility\VariableTest::testExport()
  113. * @see Drupal\Tests\Component\Utility\VariableTest::providerTestExport()
  114. */
  115. class StubVariableTestClass {
  116. }