PHP Classes

File: tests/JSONPathArrayAccessTest.php

Recommend this page to a friend!
  Classes of Sascha Greuel   PHP JSON Path   tests/JSONPathArrayAccessTest.php   Download  
File: tests/JSONPathArrayAccessTest.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: test: distinction between array and ArrayAccess
Date: 3 years ago
Size: 2,774 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
ArrayObject;
use
Exception;
use
Flow\JSONPath\JSONPath;
use
Flow\JSONPath\Test\Traits\TestDataTrait;
use
PHPUnit\Framework\TestCase;

use function
is_array;

class
JSONPathArrayAccessTest extends TestCase
{
    use
TestDataTrait;

   
/**
     * @throws Exception
     */
   
public function testChaining(): void
   
{
       
$container = new ArrayObject($this->getData('conferences'));
       
$jsonPath = new JSONPath($container);

       
$teams = $jsonPath
           
->find('.conferences.*')
            ->
find('..teams.*');

       
self::assertEquals('Dodger', $teams[0]['name']);
       
self::assertEquals('Mets', $teams[1]['name']);

       
$teams = $jsonPath
           
->find('.conferences.*')
            ->
find('..teams.*');

       
self::assertEquals('Dodger', $teams[0]['name']);
       
self::assertEquals('Mets', $teams[1]['name']);

       
$teams = $jsonPath
           
->find('.conferences..teams.*');

       
self::assertEquals('Dodger', $teams[0]['name']);
       
self::assertEquals('Mets', $teams[1]['name']);
    }

   
/**
     * @throws Exception
     */
   
public function testIterating(): void
   
{
       
$container = new ArrayObject($this->getData('conferences'));

       
$conferences = (new JSONPath($container))
            ->
find('.conferences.*');

       
$names = [];

        foreach (
$conferences as $conference) {
           
$players = $conference
               
->find('.teams.*.players[?(@.active=yes)]');

            foreach (
$players as $player) {
               
$names[] = $player->name;
            }
        }

       
self::assertEquals(['Joe Face', 'something'], $names);
    }

   
/**
     * @param bool $asArray
     * @testWith [false]
     * [true]
     */
   
public function testDifferentStylesOfAccess(bool $asArray): void
   
{
       
$container = new ArrayObject($this->getData('conferences', $asArray));
       
$data = new JSONPath($container);

       
self::assertArrayHasKey('conferences', $data);

       
$conferences = $data->__get('conferences')->getData();

        if (
is_array($conferences[0])) {
           
self::assertEquals('Western Conference', $conferences[0]['name']);
        } else {
           
self::assertEquals('Western Conference', $conferences[0]->name);
        }
    }

    public function
testUpdate(): void
   
{
       
$container = new ArrayObject($this->getData('conferences'));
       
$data = new JSONPath($container);

       
$data->offsetSet('name', 'Major League Football');
       
self::assertEquals('Major League Football', $data->name);
    }
}