dotpathUtilTest.php 1009 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. declare(strict_types=1);
  3. class dotpathUtilTest extends PHPUnit\Framework\TestCase {
  4. /**
  5. * @return Traversable<array{array<string,mixed>,string,string}>
  6. */
  7. public function provideJsonDots(): Traversable {
  8. $json = <<<json
  9. {
  10. "hello": "world",
  11. "deeper": {
  12. "hello": "again"
  13. },
  14. "items": [
  15. {
  16. "meta": {"title": "first"}
  17. },
  18. {
  19. "meta": {"title": "second"}
  20. }
  21. ]
  22. }
  23. json;
  24. $array = json_decode($json, true);
  25. yield [$array, 'hello', 'world'];
  26. yield [$array, 'deeper.hello', 'again'];
  27. yield [$array, 'items.0.meta.title', 'first'];
  28. yield [$array, 'items[0].meta.title', 'first'];
  29. yield [$array, 'items.1.meta.title', 'second'];
  30. yield [$array, 'items[1].meta.title', 'second'];
  31. }
  32. /**
  33. * @dataProvider provideJsonDots
  34. * @param array<string,mixed> $array
  35. */
  36. public function testJsonDots(array $array, string $key, string $expected): void {
  37. $value = FreshRSS_dotpath_Util::get($array, $key);
  38. self::assertEquals($expected, $value);
  39. }
  40. }