Drupal investigation

CasterTest.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Tests\Caster;
  11. use Symfony\Component\VarDumper\Caster\Caster;
  12. use Symfony\Component\VarDumper\Test\VarDumperTestCase;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class CasterTest extends VarDumperTestCase
  17. {
  18. private $referenceArray = array(
  19. 'null' => null,
  20. 'empty' => false,
  21. 'public' => 'pub',
  22. "\0~\0virtual" => 'virt',
  23. "\0+\0dynamic" => 'dyn',
  24. "\0*\0protected" => 'prot',
  25. "\0Foo\0private" => 'priv',
  26. );
  27. /**
  28. * @dataProvider provideFilter
  29. */
  30. public function testFilter($filter, $expectedDiff, $listedProperties = null)
  31. {
  32. if (null === $listedProperties) {
  33. $filteredArray = Caster::filter($this->referenceArray, $filter);
  34. } else {
  35. $filteredArray = Caster::filter($this->referenceArray, $filter, $listedProperties);
  36. }
  37. $this->assertSame($expectedDiff, array_diff_assoc($this->referenceArray, $filteredArray));
  38. }
  39. public function provideFilter()
  40. {
  41. return array(
  42. array(
  43. 0,
  44. array(),
  45. ),
  46. array(
  47. Caster::EXCLUDE_PUBLIC,
  48. array(
  49. 'null' => null,
  50. 'empty' => false,
  51. 'public' => 'pub',
  52. ),
  53. ),
  54. array(
  55. Caster::EXCLUDE_NULL,
  56. array(
  57. 'null' => null,
  58. ),
  59. ),
  60. array(
  61. Caster::EXCLUDE_EMPTY,
  62. array(
  63. 'null' => null,
  64. 'empty' => false,
  65. ),
  66. ),
  67. array(
  68. Caster::EXCLUDE_VIRTUAL,
  69. array(
  70. "\0~\0virtual" => 'virt',
  71. ),
  72. ),
  73. array(
  74. Caster::EXCLUDE_DYNAMIC,
  75. array(
  76. "\0+\0dynamic" => 'dyn',
  77. ),
  78. ),
  79. array(
  80. Caster::EXCLUDE_PROTECTED,
  81. array(
  82. "\0*\0protected" => 'prot',
  83. ),
  84. ),
  85. array(
  86. Caster::EXCLUDE_PRIVATE,
  87. array(
  88. "\0Foo\0private" => 'priv',
  89. ),
  90. ),
  91. array(
  92. Caster::EXCLUDE_VERBOSE,
  93. array(
  94. 'public' => 'pub',
  95. "\0*\0protected" => 'prot',
  96. ),
  97. array('public', "\0*\0protected"),
  98. ),
  99. array(
  100. Caster::EXCLUDE_NOT_IMPORTANT,
  101. array(
  102. 'null' => null,
  103. 'empty' => false,
  104. "\0~\0virtual" => 'virt',
  105. "\0+\0dynamic" => 'dyn',
  106. "\0Foo\0private" => 'priv',
  107. ),
  108. array('public', "\0*\0protected"),
  109. ),
  110. array(
  111. Caster::EXCLUDE_VIRTUAL | Caster::EXCLUDE_DYNAMIC,
  112. array(
  113. "\0~\0virtual" => 'virt',
  114. "\0+\0dynamic" => 'dyn',
  115. ),
  116. ),
  117. array(
  118. Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_VERBOSE,
  119. $this->referenceArray,
  120. array('public', "\0*\0protected"),
  121. ),
  122. array(
  123. Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_EMPTY,
  124. array(
  125. 'null' => null,
  126. 'empty' => false,
  127. "\0~\0virtual" => 'virt',
  128. "\0+\0dynamic" => 'dyn',
  129. "\0*\0protected" => 'prot',
  130. "\0Foo\0private" => 'priv',
  131. ),
  132. array('public', 'empty'),
  133. ),
  134. array(
  135. Caster::EXCLUDE_VERBOSE | Caster::EXCLUDE_EMPTY | Caster::EXCLUDE_STRICT,
  136. array(
  137. 'empty' => false,
  138. ),
  139. array('public', 'empty'),
  140. ),
  141. );
  142. }
  143. /**
  144. * @requires PHP 7.0
  145. */
  146. public function testAnonymousClass()
  147. {
  148. $c = eval('return new class extends stdClass { private $foo = "foo"; };');
  149. $this->assertDumpMatchesFormat(
  150. <<<'EOTXT'
  151. stdClass@anonymous {
  152. -foo: "foo"
  153. }
  154. EOTXT
  155. , $c
  156. );
  157. $c = eval('return new class { private $foo = "foo"; };');
  158. $this->assertDumpMatchesFormat(
  159. <<<'EOTXT'
  160. @anonymous {
  161. -foo: "foo"
  162. }
  163. EOTXT
  164. , $c
  165. );
  166. }
  167. }