sdobreff/json-response-test

测试 JSON 响应是否符合文件中提供的模式

1.3 2021-02-25 14:22 UTC

This package is auto-updated.

Last update: 2024-09-25 22:13:42 UTC


README

JsonApiTest 是一个 PHPUnit 测试用例,可以使你的 PHP API 开发生活变得更加轻松。

感谢 PHP-Matcher,你可以测试你的 API JSON 响应是否符合提供的模式。

沙盒

你可以使用 PHP-Matcher 沙盒 来创建你的 JSON 模式。

安装

假设你已经全局安装了 Composer

$ composer require sdobreff/json-response-test

使用

配置好后,任何位于 key message 下的字符串都将匹配该模式。更复杂的预期响应可能如下所示

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

并将匹配以下产品列表

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

可用的模式

@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

可用的模式扩展器

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()\"})"}

示例用法

你的测试类

<?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 用于初始化属性

<?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' );
    }
}