CasterTest.php 4.8 KB

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