StructuredDataDescriptorTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace Test\Michel\Debug;
  3. use Michel\Debug\Util\DataDescriptor;
  4. use Michel\UniTester\TestCase;
  5. class StructuredDataDescriptorTest extends TestCase
  6. {
  7. protected function setUp(): void
  8. {
  9. // TODO: Implement setUp() method.
  10. }
  11. protected function tearDown(): void
  12. {
  13. // TODO: Implement tearDown() method.
  14. }
  15. protected function execute(): void
  16. {
  17. $result = DataDescriptor::describe([
  18. 'name' => 'John Doe',
  19. 'email' => 'john.doe@example.com',
  20. 'active' => true,
  21. 'roles' => ['admin', 'user'],
  22. 'age' => 30
  23. ]);
  24. $this->assertEquals($result, array(
  25. 'type' => 'array',
  26. 'depth' => 0,
  27. 'truncated' => false,
  28. 'count' => 5,
  29. 'value' => 'array(5)',
  30. 'items' =>
  31. array(
  32. 'name' =>
  33. array(
  34. 'type' => 'string',
  35. 'depth' => 1,
  36. 'truncated' => false,
  37. 'length' => 8,
  38. 'value' => '"John Doe"',
  39. ),
  40. 'email' =>
  41. array(
  42. 'type' => 'string',
  43. 'depth' => 1,
  44. 'truncated' => false,
  45. 'length' => 20,
  46. 'value' => '"john.doe@example.com"',
  47. ),
  48. 'active' =>
  49. array(
  50. 'type' => 'boolean',
  51. 'depth' => 1,
  52. 'truncated' => false,
  53. 'value' => 'true',
  54. ),
  55. 'roles' =>
  56. array(
  57. 'type' => 'array',
  58. 'depth' => 1,
  59. 'truncated' => false,
  60. 'count' => 2,
  61. 'value' => 'array(2)',
  62. 'items' =>
  63. array(
  64. 0 =>
  65. array(
  66. 'type' => 'string',
  67. 'depth' => 2,
  68. 'truncated' => false,
  69. 'length' => 5,
  70. 'value' => '"admin"',
  71. ),
  72. 1 =>
  73. array(
  74. 'type' => 'string',
  75. 'depth' => 2,
  76. 'truncated' => false,
  77. 'length' => 4,
  78. 'value' => '"user"',
  79. ),
  80. ),
  81. ),
  82. 'age' =>
  83. array(
  84. 'type' => 'int',
  85. 'depth' => 1,
  86. 'truncated' => false,
  87. 'value' => 30,
  88. ),
  89. ),
  90. ));
  91. $result = DataDescriptor::describe([
  92. 'string' => 'Hello world',
  93. 'int' => 42,
  94. 'float' => 3.14,
  95. 'boolTrue' => true,
  96. 'boolFalse' => false,
  97. 'nullValue' => null,
  98. 'arraySimple' => [1, 2, 3],
  99. 'arrayNested' => [
  100. 'level1' => [
  101. 'level2' => [
  102. 'level3a' => 'deep',
  103. 'level3b' => [4, 5, 6]
  104. ],
  105. 'level2b' => 'mid'
  106. ],
  107. 'anotherKey' => 'value'
  108. ],
  109. 'objectSimple' => (object)['foo' => 'bar', 'baz' => 123],
  110. ]);
  111. $this->assertEquals($result, array(
  112. 'type' => 'array',
  113. 'depth' => 0,
  114. 'truncated' => false,
  115. 'count' => 9,
  116. 'value' => 'array(9)',
  117. 'items' =>
  118. array(
  119. 'string' =>
  120. array(
  121. 'type' => 'string',
  122. 'depth' => 1,
  123. 'truncated' => false,
  124. 'length' => 11,
  125. 'value' => '"Hello world"',
  126. ),
  127. 'int' =>
  128. array(
  129. 'type' => 'int',
  130. 'depth' => 1,
  131. 'truncated' => false,
  132. 'value' => 42,
  133. ),
  134. 'float' =>
  135. array(
  136. 'type' => 'float',
  137. 'depth' => 1,
  138. 'truncated' => false,
  139. 'value' => 3.14,
  140. ),
  141. 'boolTrue' =>
  142. array(
  143. 'type' => 'boolean',
  144. 'depth' => 1,
  145. 'truncated' => false,
  146. 'value' => 'true',
  147. ),
  148. 'boolFalse' =>
  149. array(
  150. 'type' => 'boolean',
  151. 'depth' => 1,
  152. 'truncated' => false,
  153. 'value' => 'false',
  154. ),
  155. 'nullValue' =>
  156. array(
  157. 'type' => 'NULL',
  158. 'depth' => 1,
  159. 'truncated' => false,
  160. 'value' => 'null',
  161. ),
  162. 'arraySimple' =>
  163. array(
  164. 'type' => 'array',
  165. 'depth' => 1,
  166. 'truncated' => false,
  167. 'count' => 3,
  168. 'value' => 'array(3)',
  169. 'items' =>
  170. array(
  171. 0 =>
  172. array(
  173. 'type' => 'int',
  174. 'depth' => 2,
  175. 'truncated' => false,
  176. 'value' => 1,
  177. ),
  178. 1 =>
  179. array(
  180. 'type' => 'int',
  181. 'depth' => 2,
  182. 'truncated' => false,
  183. 'value' => 2,
  184. ),
  185. 2 =>
  186. array(
  187. 'type' => 'int',
  188. 'depth' => 2,
  189. 'truncated' => false,
  190. 'value' => 3,
  191. ),
  192. ),
  193. ),
  194. 'arrayNested' =>
  195. array(
  196. 'type' => 'array',
  197. 'depth' => 1,
  198. 'truncated' => false,
  199. 'count' => 2,
  200. 'value' => 'array(2)',
  201. 'items' =>
  202. array(
  203. 'level1' =>
  204. array(
  205. 'type' => 'array',
  206. 'depth' => 2,
  207. 'truncated' => false,
  208. 'count' => 2,
  209. 'value' => 'array(2)',
  210. 'items' =>
  211. array(
  212. 'level2' =>
  213. array(
  214. 'type' => 'array',
  215. 'depth' => 3,
  216. 'truncated' => false,
  217. 'count' => 2,
  218. 'value' => 'array(2)',
  219. 'items' =>
  220. array(
  221. 'level3a' =>
  222. array(
  223. 'type' => 'string',
  224. 'depth' => 4,
  225. 'truncated' => false,
  226. 'length' => 4,
  227. 'value' => '"deep"',
  228. ),
  229. 'level3b' =>
  230. array(
  231. 'type' => 'array',
  232. 'depth' => 4,
  233. 'truncated' => false,
  234. 'count' => 3,
  235. 'value' => 'array(3)',
  236. 'items' =>
  237. array(
  238. 0 =>
  239. array(
  240. 'type' => 'integer',
  241. 'depth' => 5,
  242. 'truncated' => true,
  243. 'value' => '…',
  244. ),
  245. 1 =>
  246. array(
  247. 'type' => 'integer',
  248. 'depth' => 5,
  249. 'truncated' => true,
  250. 'value' => '…',
  251. ),
  252. 2 =>
  253. array(
  254. 'type' => 'integer',
  255. 'depth' => 5,
  256. 'truncated' => true,
  257. 'value' => '…',
  258. ),
  259. ),
  260. ),
  261. ),
  262. ),
  263. 'level2b' =>
  264. array(
  265. 'type' => 'string',
  266. 'depth' => 3,
  267. 'truncated' => false,
  268. 'length' => 3,
  269. 'value' => '"mid"',
  270. ),
  271. ),
  272. ),
  273. 'anotherKey' =>
  274. array(
  275. 'type' => 'string',
  276. 'depth' => 2,
  277. 'truncated' => false,
  278. 'length' => 5,
  279. 'value' => '"value"',
  280. ),
  281. ),
  282. ),
  283. 'objectSimple' =>
  284. array(
  285. 'type' => 'object',
  286. 'depth' => 1,
  287. 'truncated' => false,
  288. 'class' => 'stdClass',
  289. 'object_id' => 13,
  290. 'value' => 'stdClass#13',
  291. 'properties' =>
  292. array(
  293. 'foo' =>
  294. array(
  295. 'type' => 'string',
  296. 'depth' => 2,
  297. 'truncated' => false,
  298. 'length' => 3,
  299. 'value' => '"bar"',
  300. ),
  301. 'baz' =>
  302. array(
  303. 'type' => 'int',
  304. 'depth' => 2,
  305. 'truncated' => false,
  306. 'value' => 123,
  307. ),
  308. ),
  309. ),
  310. ),
  311. ));
  312. }
  313. }