dentelis/phpunit-json-assert

使用 dentelis/php-structure-validator 断言 JSON 结构的 PhpUnit 扩展

dev-main 2024-02-20 17:42 UTC

This package is auto-updated.

Last update: 2024-09-03 17:28:51 UTC


README

PHPUnit JSON 文档数据结构验证

安装

使用包管理器 composer 安装 Validator。

composer require dentelis/phpunit-json-assert:dev-master

用法

将 JsonAssertions 特性添加到您的测试文件中

<?php
declare(strict_types=1);

use Dentelis\PhpUnitJsonAssert\JsonAssertions;
use Dentelis\StructureValidator\Type\ObjectType;
use Dentelis\StructureValidator\Type\StringType;
use PHPUnit\Framework\TestCase;

final class CustomTest extends TestCase
{
    use JsonAssertions;
    
    public function test(): void
    {
        $this->assertJsonStructure(
            '{"name":"user","email":"user@example.com"}',
             (new ObjectType())
                 ->addProperty('name', (new StringType())->assertNotEmpty())
                 ->addProperty('email', (new StringType())->assertEmail())
        );
    }
}