PHP Classes

File: tests/JSONPathSliceAccessTest.php

Recommend this page to a friend!
  Classes of Sascha Greuel   PHP JSON Path   tests/JSONPathSliceAccessTest.php   Download  
File: tests/JSONPathSliceAccessTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP JSON Path
Query values from data structures like XPATH
Author: By
Last change:
Date: 3 years ago
Size: 2,657 bytes
 

Contents

Class file image Download
<?php

/**
 * JSONPath implementation for PHP.
 *
 * @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
 */

declare(strict_types=1);

namespace
Flow\JSONPath\Test;

use
Flow\JSONPath\{JSONPath, JSONPathException};
use
PHPUnit\Framework\TestCase;

class
JSONPathSliceAccessTest extends TestCase
{
    public function
sliceDataProvider(): array
    {
        return [
            [
               
'$.data[1:3]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo1', 'foo2']
            ],
            [
               
'$.data[4:]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo4', 'foo5']
            ],
            [
               
'$.data[:2]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo0', 'foo1']
            ],
            [
               
'$.data[:]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']
            ],
            [
               
'$.data[-1]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo5']
            ],
            [
               
'$.data[-2:]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo4', 'foo5']
            ],
            [
               
'$.data[:-2]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo0', 'foo1', 'foo2', 'foo3']
            ],
            [
               
'$.data[::2]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo0', 'foo2', 'foo4']
            ],
            [
               
'$.data[2::2]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo2', 'foo4']
            ],
            [
               
'$.data[:-2:2]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo0', 'foo2']
            ],
            [
               
'$.data[1:5:2]',
                [
'data' => ['foo0', 'foo1', 'foo2', 'foo3', 'foo4', 'foo5']],
                [
'foo1', 'foo3']
            ]
        ];
    }

   
/**
     * @dataProvider sliceDataProvider
     *
     * @throws JSONPathException
     */
   
public function testSlice(string $path, array $data, array $expected): void
   
{
       
$result = (new JSONPath($data))
            ->
find($path);

       
self::assertEquals($expected, $result->getData());
    }
}