CasterTest.php 4.6 KB

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