SplCasterTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Test\VarDumperTestTrait;
  13. /**
  14. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  15. */
  16. class SplCasterTest extends TestCase
  17. {
  18. use VarDumperTestTrait;
  19. public function getCastFileInfoTests()
  20. {
  21. return [
  22. [__FILE__, <<<'EOTXT'
  23. SplFileInfo {
  24. %Apath: "%sCaster"
  25. filename: "SplCasterTest.php"
  26. basename: "SplCasterTest.php"
  27. pathname: "%sSplCasterTest.php"
  28. extension: "php"
  29. realPath: "%sSplCasterTest.php"
  30. aTime: %s-%s-%d %d:%d:%d
  31. mTime: %s-%s-%d %d:%d:%d
  32. cTime: %s-%s-%d %d:%d:%d
  33. inode: %i
  34. size: %d
  35. perms: 0%d
  36. owner: %d
  37. group: %d
  38. type: "file"
  39. writable: true
  40. readable: true
  41. executable: false
  42. file: true
  43. dir: false
  44. link: false
  45. %A}
  46. EOTXT
  47. ],
  48. ['https://google.com/about', <<<'EOTXT'
  49. SplFileInfo {
  50. %Apath: "https://google.com"
  51. filename: "about"
  52. basename: "about"
  53. pathname: "https://google.com/about"
  54. extension: ""
  55. realPath: false
  56. %A}
  57. EOTXT
  58. ],
  59. ];
  60. }
  61. /** @dataProvider getCastFileInfoTests */
  62. public function testCastFileInfo($file, $dump)
  63. {
  64. $this->assertDumpMatchesFormat($dump, new \SplFileInfo($file));
  65. }
  66. public function testCastFileObject()
  67. {
  68. $var = new \SplFileObject(__FILE__);
  69. $var->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
  70. $dump = <<<'EOTXT'
  71. SplFileObject {
  72. %Apath: "%sCaster"
  73. filename: "SplCasterTest.php"
  74. basename: "SplCasterTest.php"
  75. pathname: "%sSplCasterTest.php"
  76. extension: "php"
  77. realPath: "%sSplCasterTest.php"
  78. aTime: %s-%s-%d %d:%d:%d
  79. mTime: %s-%s-%d %d:%d:%d
  80. cTime: %s-%s-%d %d:%d:%d
  81. inode: %i
  82. size: %d
  83. perms: 0%d
  84. owner: %d
  85. group: %d
  86. type: "file"
  87. writable: true
  88. readable: true
  89. executable: false
  90. file: true
  91. dir: false
  92. link: false
  93. %AcsvControl: array:%d [
  94. 0 => ","
  95. 1 => """
  96. %A]
  97. flags: DROP_NEW_LINE|SKIP_EMPTY
  98. maxLineLen: 0
  99. fstat: array:26 [
  100. "dev" => %d
  101. "ino" => %i
  102. "nlink" => %d
  103. "rdev" => 0
  104. "blksize" => %i
  105. "blocks" => %i
  106. …20
  107. ]
  108. eof: false
  109. key: 0
  110. }
  111. EOTXT;
  112. $this->assertDumpMatchesFormat($dump, $var);
  113. }
  114. /**
  115. * @dataProvider provideCastSplDoublyLinkedList
  116. */
  117. public function testCastSplDoublyLinkedList($modeValue, $modeDump)
  118. {
  119. $var = new \SplDoublyLinkedList();
  120. $var->setIteratorMode($modeValue);
  121. $dump = <<<EOTXT
  122. SplDoublyLinkedList {
  123. %Amode: $modeDump
  124. dllist: []
  125. }
  126. EOTXT;
  127. $this->assertDumpMatchesFormat($dump, $var);
  128. }
  129. public function provideCastSplDoublyLinkedList()
  130. {
  131. return [
  132. [\SplDoublyLinkedList::IT_MODE_FIFO, 'IT_MODE_FIFO | IT_MODE_KEEP'],
  133. [\SplDoublyLinkedList::IT_MODE_LIFO, 'IT_MODE_LIFO | IT_MODE_KEEP'],
  134. [\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_DELETE, 'IT_MODE_FIFO | IT_MODE_DELETE'],
  135. [\SplDoublyLinkedList::IT_MODE_LIFO | \SplDoublyLinkedList::IT_MODE_DELETE, 'IT_MODE_LIFO | IT_MODE_DELETE'],
  136. ];
  137. }
  138. public function testCastObjectStorageIsntModified()
  139. {
  140. $var = new \SplObjectStorage();
  141. $var->attach(new \stdClass());
  142. $var->rewind();
  143. $current = $var->current();
  144. $this->assertDumpMatchesFormat('%A', $var);
  145. $this->assertSame($current, $var->current());
  146. }
  147. public function testCastObjectStorageDumpsInfo()
  148. {
  149. $var = new \SplObjectStorage();
  150. $var->attach(new \stdClass(), new \DateTime());
  151. $this->assertDumpMatchesFormat('%ADateTime%A', $var);
  152. }
  153. public function testCastArrayObject()
  154. {
  155. $var = new \ArrayObject([123]);
  156. $var->foo = 234;
  157. $expected = <<<EOTXT
  158. ArrayObject {
  159. +"foo": 234
  160. flag::STD_PROP_LIST: false
  161. flag::ARRAY_AS_PROPS: false
  162. iteratorClass: "ArrayIterator"
  163. storage: array:1 [
  164. 0 => 123
  165. ]
  166. }
  167. EOTXT;
  168. $this->assertDumpEquals($expected, $var);
  169. }
  170. public function testArrayIterator()
  171. {
  172. $var = new MyArrayIterator([234]);
  173. $expected = <<<EOTXT
  174. Symfony\Component\VarDumper\Tests\Caster\MyArrayIterator {
  175. -foo: 123
  176. flag::STD_PROP_LIST: false
  177. flag::ARRAY_AS_PROPS: false
  178. storage: array:1 [
  179. 0 => 234
  180. ]
  181. }
  182. EOTXT;
  183. $this->assertDumpEquals($expected, $var);
  184. }
  185. }
  186. class MyArrayIterator extends \ArrayIterator
  187. {
  188. private $foo = 123;
  189. }