estahn/phpunit-json-assertions 问题修复 & 功能扩展

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

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

estahn/phpunit-json-assertions

Composer 安装命令:

composer require estahn/phpunit-json-assertions

包简介

JSON assertions for PHPUnit (including JSON Schema)

README 文档

README

Latest Stable Version Total Downloads Test StyleCI Average time to resolve an issue Percentage of issues still open

JSON assertions for PHPUnit includes traits/methods to help validate your JSON data through various methods.

Features

  • Validate your JSON data via JSON Schema
    • describes your existing data format
    • clear, human- and machine-readable documentation
    • complete structural validation, useful for
      • automated testing
      • validating client-submitted data
    • See more details here
  • Access JSON data through expressions (e.g. foo.bar[3])
    • See more details here

Install

$ composer require estahn/phpunit-json-assertions --dev

or in your composer.json:

{
    "require-dev": {
        "estahn/phpunit-json-assertions": "@stable"
    }
}

Asserts

Assert Description Available in
assertJsonMatchesSchema Asserts that json content is valid according to the provided schema file All
assertJsonMatchesSchemaString Asserts that json content is valid according to the provided schema string All
assertJsonValueEquals Asserts if the value retrieved with the expression equals the expected value All
assertJsonValueEquals Asserts if the value retrieved with the expression equals the expected value All
assertJsonResponse Asserts that a response is successful and of type json Symfony

Usage

You can either use the trait or class version.

Trait

<?php

namespace EnricoStahn\JsonAssert\Tests;

use EnricoStahn\JsonAssert\Assert as JsonAssert;

class MyTestCase extends \PHPUnit_Framework_TestCase
{
    use JsonAssert;

    public function testJsonDocumentIsValid()
    {
        // my-schema.json
        //
        // {
        //   "type" : "object",
        //   "properties" : {
        //     "foo" : {
        //       "type" : "integer"
        //     }
        //   },
        //   "required" : [ "foo" ]
        // }

        $json = json_decode('{"foo":1}');

        $this->assertJsonMatchesSchema($json, './my-schema.json');
        $this->assertJsonValueEquals(1, '* | [0]', $json);
    }
}

Class

In case you don't want to use the trait you can use the provided class wich extends from \PHPUnit_Framework_TestCase. You can either extend your test case or use the static methods like below.

<?php

namespace EnricoStahn\JsonAssert\Tests;

use EnricoStahn\JsonAssert\AssertClass as JsonAssert;

class MyTestCase extends \PHPUnit_Framework_TestCase
{
    public function testJsonDocumentIsValid()
    {
        // my-schema.json
        //
        // {
        //   "type" : "object",
        //   "properties" : {
        //     "foo" : {
        //       "type" : "integer"
        //     }
        //   },
        //   "required" : [ "foo" ]
        // }

        $json = json_decode('{"foo":1}');

        JsonAssert::assertJsonMatchesSchema($json, './my-schema.json');
        JsonAssert::assertJsonValueEquals(1, '* | [0]', $json);
    }
}

Schema storage

The schema storage of justinrainbow/json-schema allows to register schemas which will effectively override the actual schema location.

Example:

{"$ref" : "https://iglu.foobar.com/myschema.json#/definitions/positiveInteger"}

The resolver will fetch the schema from this endpoint and match the JSON document against it. Using schema storage you're able to override this behaviour.

$schemastorage->addSchema('https://iglu.foobar.com/myschema.json', (object)['type' => 'string']);

With this in place the resolver will take the schema that is already in place without downloading it again.

<?php

namespace EnricoStahn\JsonAssert\Tests;

use EnricoStahn\JsonAssert\AssertClass as JsonAssert;

class MyTestCase extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        self::$schemaStorage = new SchemaStorage();
        
        self::$schemaStorage->addSchema('<id>', obj);
        ...
    }
    
    public function testJsonDocumentIsValid()
    {
        // my-schema.json
        //
        // {
        //   "type" : "object",
        //   "properties" : {
        //     "foo" : {
        //       "type" : "integer"
        //     }
        //   },
        //   "required" : [ "foo" ]
        // }

        $json = json_decode('{"foo":1}');

        JsonAssert::assertJsonMatchesSchema($json, './my-schema.json');
        JsonAssert::assertJsonValueEquals(1, '* | [0]', $json);
    }
}

Extensions

phpunit-json-assertions provides extensions for simpler handling in different use cases.

Symfony HttpFoundation Component

The extension EnricoStahn\JsonAssert\Extension\Symfony allows to pass in the actual response object generated by the symfony framework and takes care of the decoding part.

BEFORE:

use EnricoStahn\JsonAssert\Assert as JsonAssert;

// ...

$content = $response->getContent();
$json = json_decode($content);
JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $json);

AFTER:

use EnricoStahn\JsonAssert\Extension\Symfony as JsonAssert;

// ...

JsonAssert::assertJsonMatchesSchemaString('./my-schema.json', $response);

Tests

To run the test suite, you need composer.

$ composer install
$ bin/phpunit

Badge Mania

Build Status Scrutinizer Code Quality Code Coverage Codacy Badge Codacy Badge

Alternatives

License

The phpunit-json-assertions library is licensed under the MIT.

Stargazers over time

Stargazers over time

estahn/phpunit-json-assertions 适用场景与选型建议

estahn/phpunit-json-assertions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.05M 次下载、GitHub Stars 达 36, 最近一次更新时间为 2016 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 estahn/phpunit-json-assertions 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.05M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 12
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 3
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-16