定制 zlob/any-json-tester 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zlob/any-json-tester

Composer 安装命令:

composer require --dev zlob/any-json-tester

包简介

Help to test JSON with changeable values, like timestamps, count etc

README 文档

README

Extension for PhpUnit, that helps you to test JSON or Array with changeable values, like timestamps, count etc. It can be very helpful if you decide to test your API. All you need - is to define data structure with set of helper classes.

Installation

via composer

composer require zlob/any-json-tester

Example

Lets test JSON like

{
      "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"
        }
      ]
  }'

Test code:

<?php

    //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);
        }
    }

Usage

After install, use AnyJsonTester trait in your test class. It gives you two methods: seeJsonLike to test JSON, and seeArrayLike to test arrays.

Some sugar for Laravel 5.1

In my favorite framework, you can use AnyJsonTesterLaravel trait instead of AnyJsonTester trait, that will automatically retrieves data from response, so you can chain seeJsonLike method with request method, like

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

##Interface Trait has only two public methods: seeJsonLike and seeArrayLike, that takes as argument AnyObject or AnyArray object (and tested JSON string in case of non-Laravel trait). AnyObject is used to test JSON object like '{"name" : "Zlob"}' and can use additional types described bellow. AnyArray is used to test JSON array of similar objects, such as AnyObject, AnyInteger and etc. or even another AnyArray. ##Supported types ####AnyObject - help to test JSON objects #####Arguments:

  • options - array, avaible options are:
    • hasFields - array of fields and values, that Object should contain. Instead of explicit value, you may use one of supportd types
    • hasNoFields - array of fields, that Object should not contain.
    • strictMode - boolean. In strict mode, object can contain only fields, described in hasFields option
    • nullable - boolean. If true, object can be null.

#####Example:

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

####AnyArray - helps to test array #####Arguments:

  • expectedElement - AnyObject, AnyBoolean, AnyDateTime, AnyFloat, AnyInteger, AnyString or another AnyArray object, that describe content of array items
  • options - array, avaible options are:
    • min - int, minimum array length
    • max - int, maximum array length
    • nullable - boolean. If true, array can be null.

#####Example:

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

####AnyString - helps to test strings #####Arguments:

  • options - array, avaible options are:
    • min - int, minimum string length
    • max - int, maximum string length
    • regex - string, regex pattern
    • enum - array, defines possible string values
    • nullable - boolean. If true, string can be null.

#####Example:

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

####AnyInteger - helps to test integers #####Arguments:

  • options - array, avaible options are:
    • min - int, minimum integer value
    • max - int, maximum integer value
    • nullable - boolean. If true, integer can be null.

#####Example:

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

####AnyFloat - helps to test floats #####Arguments:

  • options - array, avaible options are:
    • min - int, minimum float value
    • max - int, maximum float value
    • precision - int, float precision
    • nullable - boolean. If true, float can be null.

#####Example:

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

####AnyDateTime - helps to test date and time #####Arguments:

  • options - array, avaible options are:
    • min - int, minimum date/time value
    • max - int, maximum date/time value
    • format - string, DateTime format
    • nullable - boolean. If true, value can be null.

#####Example:

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

####AnyBoolean - helps to test boolean #####Arguments:

  • options - array, avaible options are:
    • strictMode - bool, if true - only 'true' and 'false' strings are boolean. Otherwise, 'true', 'false', '0', '1', 'on', 'off', 'yes' and 'no'.
    • nullable - boolean. If true, value can be null.

#####Example:

$anyBoolean = new AnyBoolean(
            [
                  'strictMode' => false,
                  'nullable' => true
            ]
      );

License

MIT - Zlob

zlob/any-json-tester 适用场景与选型建议

zlob/any-json-tester 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「testing」 「test」 「REST API」 「RESTful API」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zlob/any-json-tester 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zlob/any-json-tester 我们能提供哪些服务?
定制开发 / 二次开发

基于 zlob/any-json-tester 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-21