sdobreff/json-response-test 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

sdobreff/json-response-test

Composer 安装命令:

composer require sdobreff/json-response-test

包简介

Test JSON response against provided pattern in file

README 文档

README

JsonApiTest is a PHPUnit TestCase that will make your life as a PHP API developer much easier.

Thanks to PHP-Matcher you can, test you API JSON responses against provided schema.

Sandbox

You can use PHP-Matcher Sandbox, to create your JSON schema.

Installation

Assuming you already have Composer installed globally:

$ composer require sdobreff/json-response-test

Usage

With this in place, any string under key message will match the pattern. More complicated expected response could look like this:

{
    "users":[
      {
        "firstName": "Norbert",
        "lastName": "Orzechowicz",
        "created": "2014-01-01",
        "roles":["ROLE_USER", "ROLE_DEVELOPER"],
        "attributes": {
          "isAdmin": false,
          "dateOfBirth": null,
          "hasEmailVerified": true
        },
        "avatar": {
          "url": "http://avatar-image.com/avatar.png"
        }
      },
      {
        "firstName": "Michał",
        "lastName": "Dąbrowski",
        "created": "2014-01-01",
        "roles":["ROLE_USER", "ROLE_DEVELOPER", "ROLE_ADMIN"],
        "attributes": {
          "isAdmin": true,
          "dateOfBirth": null,
          "hasEmailVerified": true
        },
        "avatar": null
      }
    ]
  }

And will match the following list of products:

{
    "users":[
      {
        "firstName": "@string@",
        "lastName": "@string@",
        "created": "@string@.isDateTime()",
        "roles": [
            "ROLE_USER",
            "@...@"
        ],
        "attributes": {
          "isAdmin": @boolean@,
          "@*@": "@*@"
        },
        "avatar": "@json@.match({\"url\":\"@string@.isUrl()\"})"
      }
      ,
      @...@
    ]
  }

Available patterns

@string@
@integer@
@number@
@double@
@boolean@
@array@
@...@ - unbounded array
@null@
@*@ || @wildcard@
expr(expression) - optional, requires symfony/expression-language: ^2.3|^3.0|^4.0|^5.0 to be present
@uuid@
@json@
@string@||@integer@ - string OR integer

Available pattern expanders

startsWith($stringBeginning, $ignoreCase = false)
endsWith($stringEnding, $ignoreCase = false)
contains($string, $ignoreCase = false)
notContains($string, $ignoreCase = false)
isDateTime()
isEmail()
isUrl()
isIp()
isEmpty()
isNotEmpty()
lowerThan($boundry)
greaterThan($boundry)
inArray($value)
hasProperty($propertyName) - example "@json@.hasProperty(\"property_name\")"
oneOf(...$expanders) - example "@string@.oneOf(contains('foo'), contains('bar'), contains('baz'))"
matchRegex($regex) - example "@string@.matchRegex('/^lorem.+/')"
optional() - work's only with ArrayMatcher, JsonMatcher and XmlMatcher
count() - work's only with ArrayMatcher - example "@array@.count(5)"
repeat($pattern, $isStrict = true) - example '@array@.repeat({"name": "foe"})' or "@array@.repeat('@string@')"
match($pattern) - example {"image":"@json@.match({\"url\":\"@string@.isUrl()\"})"}

Example usage

Your test class

<?php
declare(strict_types=1);

class LocationTest extends BaseApiTest {

    public function testGetLocation() {
 
        $addHead = [
            'headers' => [
                'Authorization' => 'Bearer some-key',
            ],
        ];
        
        $response = $this->request(
            '/api/v1/search',
            'POST',
            [
                'q' => 'sp',
                'filters' => '{}',
            ],
            $addHead
        );

        $this->assertResponse( $response, 'location/tst.json' );
    }
}

BaseApiTest for initializing the properties

<?php
declare(strict_types=1);

use JsonResponseTest\APITest\JsonApiTest;

class BaseApiTest extends JsonApiTest {

    public function setUp() {
        $dir = __DIR__ . '/responses/';

        parent::setResponseDir( $dir );
        parent::setClient();
        parent::setBaseUrl( 'https://local-test.com' );
    }
}

sdobreff/json-response-test 适用场景与选型建议

sdobreff/json-response-test 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 sdobreff/json-response-test 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-06