菜谱项目

CasterTest.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = array(
  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 array(
  44. array(
  45. 0,
  46. array(),
  47. ),
  48. array(
  49. Caster::EXCLUDE_PUBLIC,
  50. array(
  51. 'null' => null,
  52. 'empty' => false,
  53. 'public' => 'pub',
  54. ),
  55. ),
  56. array(
  57. Caster::EXCLUDE_NULL,
  58. array(
  59. 'null' => null,
  60. ),
  61. ),
  62. array(
  63. Caster::EXCLUDE_EMPTY,
  64. array(
  65. 'null' => null,
  66. 'empty' => false,
  67. ),
  68. ),
  69. array(
  70. Caster::EXCLUDE_VIRTUAL,
  71. array(
  72. "\0~\0virtual" => 'virt',
  73. ),
  74. ),
  75. array(
  76. Caster::EXCLUDE_DYNAMIC,
  77. array(
  78. "\0+\0dynamic" => 'dyn',
  79. ),
  80. ),
  81. array(
  82. Caster::EXCLUDE_PROTECTED,
  83. array(
  84. "\0*\0protected" => 'prot',
  85. ),
  86. ),
  87. array(
  88. Caster::EXCLUDE_PRIVATE,
  89. array(
  90. "\0Foo\0private" => 'priv',
  91. ),
  92. ),
  93. array(
  94. Caster::EXCLUDE_VERBOSE,
  95. array(
  96. 'public' => 'pub',
  97. "\0*\0protected" => 'prot',
  98. ),
  99. array('public', "\0*\0protected"),
  100. ),
  101. array(
  102. Caster::EXCLUDE_NOT_IMPORTANT,
  103. array(
  104. 'null' => null,
  105. 'empty' => false,
  106. "\0~\0virtual" => 'virt',
  107. "\0+\0dynamic" => 'dyn',
  108. "\0Foo\0private" => 'priv',
  109. ),
  110. array('public', "\0*\0protected"),
  111. ),
  112. array(
  113. Caster::EXCLUDE_VIRTUAL | Caster::EXCLUDE_DYNAMIC,
  114. array(
  115. "\0~\0virtual" => 'virt',
  116. "\0+\0dynamic" => 'dyn',
  117. ),
  118. ),
  119. array(
  120. Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_VERBOSE,
  121. $this->referenceArray,
  122. array('public', "\0*\0protected"),
  123. ),
  124. array(
  125. Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_EMPTY,
  126. array(
  127. 'null' => null,
  128. 'empty' => false,
  129. "\0~\0virtual" => 'virt',
  130. "\0+\0dynamic" => 'dyn',
  131. "\0*\0protected" => 'prot',
  132. "\0Foo\0private" => 'priv',
  133. ),
  134. array('public', 'empty'),
  135. ),
  136. array(
  137. Caster::EXCLUDE_VERBOSE | Caster::EXCLUDE_EMPTY | Caster::EXCLUDE_STRICT,
  138. array(
  139. 'empty' => false,
  140. ),
  141. array('public', 'empty'),
  142. ),
  143. );
  144. }
  145. /**
  146. * @requires PHP 7.0
  147. */
  148. public function testAnonymousClass()
  149. {
  150. $c = eval('return new class extends stdClass { private $foo = "foo"; };');
  151. $this->assertDumpMatchesFormat(
  152. <<<'EOTXT'
  153. stdClass@anonymous {
  154. -foo: "foo"
  155. }
  156. EOTXT
  157. , $c
  158. );
  159. $c = eval('return new class { private $foo = "foo"; };');
  160. $this->assertDumpMatchesFormat(
  161. <<<'EOTXT'
  162. @anonymous {
  163. -foo: "foo"
  164. }
  165. EOTXT
  166. , $c
  167. );
  168. }
  169. }