CasterTest.php 4.2 KB

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