2022-11-07 23:45:07 +10:00
|
|
|
<?php
|
|
|
|
|
2023-03-20 19:33:16 +10:00
|
|
|
use Cerbero\JsonParser\Parser;
|
|
|
|
|
2023-02-06 18:18:14 +10:00
|
|
|
if (!function_exists('fixture')) {
|
|
|
|
/**
|
|
|
|
* Retrieve the absolute path of the given fixture
|
|
|
|
*
|
|
|
|
* @param string $fixture
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function fixture(string $fixture): string
|
|
|
|
{
|
|
|
|
return __DIR__ . "/fixtures/{$fixture}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 13:34:13 +10:00
|
|
|
/**
|
|
|
|
* Expect that keys and values are parsed correctly
|
|
|
|
*
|
|
|
|
* @param array $expected
|
|
|
|
* @return Expectation
|
|
|
|
*/
|
2022-11-17 23:53:54 +10:00
|
|
|
expect()->extend('toParseTo', function (array $expected) {
|
2022-12-03 13:34:13 +10:00
|
|
|
$actual = [];
|
|
|
|
|
|
|
|
foreach ($this->value as $parsedKey => $parsedValue) {
|
2023-01-14 13:17:15 +10:00
|
|
|
expect($expected)->toHaveKey($parsedKey, $parsedValue);
|
2022-12-03 13:34:13 +10:00
|
|
|
|
|
|
|
$actual[$parsedKey] = $parsedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return expect($actual)->toBe($expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expect that values defined by JSON pointers are parsed correctly
|
|
|
|
*
|
|
|
|
* @param array $expected
|
|
|
|
* @return Expectation
|
|
|
|
*/
|
|
|
|
expect()->extend('toPointTo', function (array $expected) {
|
2022-11-17 23:53:54 +10:00
|
|
|
$actual = $itemsCount = [];
|
|
|
|
|
|
|
|
foreach ($this->value as $parsedKey => $parsedValue) {
|
|
|
|
$itemsCount[$parsedKey] = empty($itemsCount[$parsedKey]) ? 1 : $itemsCount[$parsedKey] + 1;
|
|
|
|
|
2023-02-05 16:12:33 +10:00
|
|
|
// associate $parsedKey to $parsedValue if $parsedKey occurs once
|
|
|
|
// associate $parsedKey to an array of $parsedValue if $parsedKey occurs multiple times
|
2022-11-17 23:53:54 +10:00
|
|
|
$actual[$parsedKey] = match ($itemsCount[$parsedKey]) {
|
|
|
|
1 => $parsedValue,
|
|
|
|
2 => [$actual[$parsedKey], $parsedValue],
|
|
|
|
default => [...$actual[$parsedKey], $parsedValue],
|
|
|
|
};
|
2022-11-07 23:45:07 +10:00
|
|
|
}
|
|
|
|
|
2022-12-03 13:34:13 +10:00
|
|
|
return expect($actual)->toBe($expected);
|
2022-11-07 23:45:07 +10:00
|
|
|
});
|
2023-03-20 19:33:16 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expect that values defined by lazy JSON pointers are parsed correctly
|
|
|
|
*
|
2023-03-21 17:33:16 +10:00
|
|
|
* @param array $keys
|
2023-03-20 19:33:16 +10:00
|
|
|
* @param array $expected
|
|
|
|
* @return Expectation
|
|
|
|
*/
|
|
|
|
expect()->extend('toLazyLoadRecursively', function (array $keys, array $expected) {
|
|
|
|
foreach ($this->value as $key => $value) {
|
|
|
|
expect($value)->toBeInstanceOf(Parser::class);
|
|
|
|
|
|
|
|
if (is_null($expectedKey = array_shift($keys))) {
|
|
|
|
expect($key)->toBeInt()->and($value)->toParseTo($expected[$key]);
|
|
|
|
} else {
|
|
|
|
expect($key)->toBe($expectedKey)->and($value)->toLazyLoadRecursively($keys, $expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|