PHPUnit data structure validation for JSON documents
Use the package manager composer to install Validator.
composer require dentelis/phpunit-json-assert:dev-masterAdd JsonAssertions trait to your test file
<?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())
);
}
}