dotNotationUtilTest.php 1.1 KB

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