mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-01-25 05:37:56 -05:00
Inspired by [JSONata syntax](https://docs.jsonata.org/expressions). fix https://github.com/FreshRSS/FreshRSS/issues/6565
50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
class dotNotationUtilTest extends PHPUnit\Framework\TestCase {
|
|
|
|
/**
|
|
* @return Traversable<array{array<string,mixed>,string,string}>
|
|
*/
|
|
public static function provideJsonDots(): Traversable {
|
|
$json = <<<json
|
|
{
|
|
"hello": "world",
|
|
"deeper": {
|
|
"hello": "again"
|
|
},
|
|
"items": [
|
|
{
|
|
"meta": {"title": "first"}
|
|
},
|
|
{
|
|
"meta": {"title": "second"}
|
|
}
|
|
]
|
|
}
|
|
json;
|
|
$array = json_decode($json, true);
|
|
|
|
yield [$array, 'hello', 'world'];
|
|
yield [$array, 'deeper.hello', 'again'];
|
|
yield [$array, 'items.0.meta.title', 'first'];
|
|
yield [$array, 'items[0].meta.title', 'first'];
|
|
yield [$array, 'items.1.meta.title', 'second'];
|
|
yield [$array, 'items[1].meta.title', 'second'];
|
|
yield [$array, '"Hello " & hello & \'!\'', 'Hello world!'];
|
|
yield [$array, '"Hello & goodbye " & hello & \'!\'', 'Hello & goodbye world!'];
|
|
yield [$array, '"Hello " & hello & deeper.hello & "!"', 'Hello worldagain!'];
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $array
|
|
*/
|
|
#[DataProvider('provideJsonDots')]
|
|
public static function testJsonDots(array $array, string $key, string $expected): void {
|
|
$value = FreshRSS_dotNotation_Util::get($array, $key);
|
|
self::assertSame($expected, $value);
|
|
}
|
|
}
|