Skip to content

tgrelka/associative-json-assertions

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON - Associaive Array assertions for PHPUnit

Latest Stable Version Minimum PHP Version Codacy Badge Codacy Badge Build Status

The intentions of this library is provide easy/intuitive assertions when we are a dealing with nested associative
arrays or JSON responses. It based on PHPUnit

Installation

$ composer require --dev nordin74/associative-json-assertions

Usage

Add the trait use AssociativeAssertions\AssociativeArrayTrait or extend the class AbstractAssociativeArray and use the defined assertions AssociativeAssertions\AssociativeAssertions in your test case

<?php

use PHPUnit\Framework\TestCase;
use AssociativeAssertions\AssociativeArrayTrait;
use AssociativeAssertions\AssociativeAssertions as AA;

class MyTestCase extends TestCase
{
    use AssociativeArrayTrait;

    public function testSuccessfulJSONResponse()
        {
            $json =
                    '{
                        "id"       : 11,
                        "extId"    : "111",
                        "isActive" : true,
                        "value"    : 12,
                        "firstname": "Seiya",
                        "surname"  : "unknown",
                        "height"   : 1.70,
                        "cloth"    : "Pegasus",
                        "deeper"   : {
                            "key1": "val1",
                            "key2": "2018-02-28 11:11:11",
                            "key3": ["val1", "val2", 11]
                        }
                     }';
    
            $expected = [
                'id'        => AA::assertInt(),
                'extId'     => AA::assertDigit(),
                'isActive'  => AA::assertBoolean(),
                'value'     => 12,
                'firstname' => 'Seiya',
                'surname'   => AA::assertRegExp('~\w+known~'),
                'height'    => AA::assertFloat(),
                'cloth'     => AA::assertScalar(),
                'deeper'    => [
                    'key1' => 'val1',
                    'key2' => AA::assertDateTimeStr('Y-m-d H:i:s'),
                    'key3' => function () {
                        $actual = func_get_args()[0];
                        $this::assertCount(3, $actual);
                    }
                ]
            ];
    
            $this->assertAssociativeArray($expected, json_decode($json, true));
        }
}

You can define your own assertions. Example:

<?php
    [
        'key' => function () {
            $valueForBeingTested = func_get_args()[0];
            // Some awesome assertion
        }
    ];

If the assertion logic is complex you can also extend the class AssociativeAssertions.

Copyright

This library is MIT-licensed.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%