PHP code example of zlob / any-json-tester

1. Go to this page and download the library: Download zlob/any-json-tester library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zlob / any-json-tester example snippets




    //import classes
    use AnyJsonTester\Types\AnyArray;
    use AnyJsonTester\Types\AnyBoolean;
    use AnyJsonTester\Types\AnyDateTime;
    use AnyJsonTester\Types\AnyFloat;
    use AnyJsonTester\Types\AnyInteger;
    use AnyJsonTester\Types\AnyObject;
    use AnyJsonTester\Types\AnyString;

    class ExampleTest extends PHPUnit_Framework_TestCase
    {
        //use AnyJsonTester trait
        use \AnyJsonTester\AnyJsonTester;

        public function testSeeJsonLikeSimple()
        {
            //JSON, that we wont to test
            $JSON = '{
                          "id"       : "1",
                          "author"   : "Zlob",
                          "project"  : "AnyJsonTester",
                          "date"     : "21-09-2015",
                          "rating"   : "9.9",
                          "url"      : "https://github.com/Zlob/AnyJsonTester",
                          "comments" : [
                            {
                              "text" : "awesome",
                              "like" : "true"
                            },
                            {
                              "text" : "Very good",
                              "like" : "true"
                            }
                          ]
                      }';
            
            //describe JSON schema
            $expectedJson = new AnyObject([
                    'id'       => new AnyInteger(),           // you can set restrictions for type, like min, max
                    'author'   => new AnyString(),            // string length or regex
                    'date'     => new AnyDateTime(),          // date period or format
                    'rating'   => new AnyFloat(),             // precision
                    'project'  => 'AnyJsonTester',            // or you can just set explicit value
                                                              // or skip some fields, that you don`t wont to test
                    'comments' => new AnyArray(new AnyObject( //you can test array of objects, set min and max array length
                            [
                                'text' => new AnyString(),
                                'like' => new AnyBoolean()    //test boolean variables
                            ]
                        )
                    )
                ]
            );
            
            //call seeJsonLike function to test JSON against schema
            static::seeJsonLike($JSON, $expectedJson, false);
        }
    }

$this->post('/user', ['name' => 'Sally'])
     ->seeJsonLike( new AnyObject( [ 'name' => AnyJsonString() ] ) );

$anyObject = new AnyObject(
            [
                  'hasFields' => ['id' => '1', 'name' => new AnyString()],
                  'hasNoFields' => ['password'],
                  'strictMode' => true,
                  'nullable' => false
            ]
      );

$anyArray = new AnyArray(
            new AnyObject(['name' => 'Zlob']),
            [
                  'min' => 1,
                  'max' => 100,
                  'nullable' => true
            ]
      );

$anyString = new AnyString(
            [
                  'min' => 1,
                  'max' => 20,
                  'regex' => '/rin/',
                  'enum' => ['value 1', 'value 2'],
                  'nullable' => true
            ]
      );

$anyInteger = new AnyInteger(
            [
                  'min' => 1,
                  'max' => 2000,
                  'nullable' => true
            ]
      );

$anyFloat = new AnyFloat(
            [
                  'min' => 1,
                  'max' => 99.99,
                  'precision' => 2
                  'nullable' => true
            ]
      );

$anyDateTime = new AnyDateTime(
            [
                  'min' => '14-12-1987 23:57',
                  'max' => '22-09-2015 14:30',
                  'format' => 'd-m-Y H:i'
                  'nullable' => true
            ]
      );

$anyBoolean = new AnyBoolean(
            [
                  'strictMode' => false,
                  'nullable' => true
            ]
      );
json
{
      "id"       : "1",
      "author"   : "Zlob",
      "project"  : "AnyJsonTester",
      "date"     : "21-09-2015",
      "rating"   : "9.9",
      "url"      : "https://github.com/Zlob/AnyJsonTester",
      "comments" : [
        {
          "text" : "awesome",
          "like" : "true"
        },
        {
          "text" : "Very good",
          "like" : "true"
        }
      ]
  }'