jdslv/atoum-json-extension

atoum json 扩展允许您对 JSON 进行断言

1.0.0 2021-09-06 02:05 UTC

This package is auto-updated.

Last update: 2024-09-06 07:00:48 UTC


README

atoum json 扩展允许您对 JSON 进行断言

GitLab Latest stable version Build status Coverage status Minimal PHP version License

此 atoum 扩展允许您使用 atoum 测试 JSON。

安装它

使用 composer 安装扩展

composer require --dev jdslv/atoum-json-extension

使用它

扩展会自动添加到 atoum 配置中。

您可以直接在安装后使用它。

<?php // tests example

namespace my\project\tests\unit;

use atoum;

class MyClass extends atoum\atoum\test
{
    public function testJSON()
    {
        $this
            ->if($json = '<<<JSON
{
    "name": "jdslv/atoum-json-extension",
    "keywords": [
        "TDD",
        "atoum",
        "test",
        "unit testing",
        "json",
        "extension",
        "atoum-extension"
    ],
}
JSON')
            ->then
                ->json($json)
                    ->isObject
                    ->hasKeys(['keywords', 'name'])

                    ->array['keywords']
                        ->contains('atoum')
                        ->contains('json')

                    ->string['name']
                        ->isIdenticalTo('jdslv/atoum-json-extension')
        ;
    }
}

断言器

每个断言器都允许一个最终的 string 参数来个性化错误消息。

它们都是流式的,您可以链式调用断言,我们会自动找到您断言的最佳上下文。

您还应该知道,所有不带参数的断言都可以带或不带括号书写。所以 $this->integer(0)->isZero()$this->integer(0)->isZero 是相同的。

json

这是针对 JSON 的断言。

hasKey

hasKey 检查是否存在具有给定名称的属性。

仅适用于基于对象的 JSON。

public function hasKey(string $key, string $message = null): self
<?php

$json1 = '{ "foo": "bar" }';
$json2 = '["foo", 123]';

$this
    ->json($json1)
        ->hasKey('foo')  // succeed
        ->hasKey('bar')  // failed
    ->json($json2)
        ->hasKey(0)      // failed
;

hasKeys

hasKeys 检查是否存在具有给定名称的属性。

仅适用于基于对象的 JSON。

public function hasKeys(array $keys, string $message = null): self
<?php

$json1 = '{ "foo": "bar" }';
$json2 = '["foo", 123]';

$this
    ->json($json1)
        ->hasKeys(['foo'])         // succeed
        ->hasKeys(['bar'])         // failed
        ->hasKeys(['foo', 'bar'])  // failed
    ->json($json2)
        ->hasKeys([0])             // failed
;

hasSize

hasSize 检查 JSON 的大小。

仅适用于基于数组的 JSON。

public function hasSize(int $count, string $message = null): self
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->hasSize(2)  // succeed
        ->hasSize(3)  // failed
    ->json($json2)
        ->hasSize(1)  // failed
    ->json($json3)
        ->hasSize(1)  // failed
;

is

is 表示 JSON 是否是给定类型。

public function is(string $type, string $message = null): self
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->is('null')     // failed
        ->is('boolean')  // failed
        ->is('integer')  // failed
        ->is('float')    // failed
        ->is('string')   // failed
        ->is('array')    // succeed
        ->is('object')   // failed
    ->json($json2)
        ->is('null')     // failed
        ->is('boolean')  // failed
        ->is('integer')  // failed
        ->is('float')    // failed
        ->is('string')   // succeed
        ->is('array')    // failed
        ->is('object')   // failed
    ->json($json3)
        ->is('null')     // failed
        ->is('boolean')  // failed
        ->is('integer')  // failed
        ->is('float')    // failed
        ->is('string')   // failed
        ->is('array')    // failed
        ->is('object')   // succeed
;

isArray

isArray 表示 JSON 是否是数组。

public function isArray(string $message = null): self
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isArray()  // succeed
    ->json($json2)
        ->isArray()  // failed
    ->json($json3)
        ->isArray()  // failed
;

isBool

isBool 表示 JSON 是否是布尔值。

public function isBool(string $message = null): self
<?php

$json1 = 'true';
$json2 = 'false';
$json3 = 'null';
$json4 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isBool()  // succeed
    ->json($json2)
        ->isBool()  // succeed
    ->json($json3)
        ->isBool()  // failed
    ->json($json4)
        ->isBool()  // failed
;

isBoolean

isBoolean 表示 JSON 是否不是布尔值。

public function isBoolean(string $message = null): self
<?php

$json1 = 'true';
$json2 = 'false';
$json3 = 'null';
$json4 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isBoolean()  // succeed
    ->json($json2)
        ->isBoolean()  // succeed
    ->json($json3)
        ->isBoolean()  // failed
    ->json($json4)
        ->isBoolean()  // failed
;

isEqualTo

public function isEqualTo($value, string $message = null)

isEqualTo 是从 variable 断言器继承的方法。有关更多信息,请参阅 variable::isEqualTo 的文档。

isFloat

isFloat 表示 JSON 是否是浮点数。

public function isFloat(string $message = null): self
<?php

$json1 = '3.14';
$json2 = '123';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isFloat()  // succeed
    ->json($json2)
        ->isFloat()  // failed
    ->json($json3)
        ->isFloat()  // failed
;

isIdenticalTo

public function isIdenticalTo($value, string $message = null)

isIdenticalTo 是从 variable 断言器继承的方法。有关更多信息,请参阅 variable::isIdenticalTo 的文档。

isInteger

isInteger 表示 JSON 是否是整数。

public function isInteger(string $message = null): self
<?php

$json1 = '123';
$json2 = '3.14';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isInteger()  // succeed
    ->json($json2)
        ->isInteger()  // failed
    ->json($json3)
        ->isInteger()  // failed
;

isNot

isNot 表示 JSON 是否不是给定类型。

public function isNot(string $type, string $message = null): self
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNot('null')     // succeed
        ->isNot('boolean')  // succeed
        ->isNot('integer')  // succeed
        ->isNot('float')    // succeed
        ->isNot('string')   // succeed
        ->isNot('array')    // failed
        ->isNot('object')   // succeed
    ->json($json2)
        ->isNot('null')     // succeed
        ->isNot('boolean')  // succeed
        ->isNot('integer')  // succeed
        ->isNot('float')    // succeed
        ->isNot('string')   // failed
        ->isNot('array')    // succeed
        ->isNot('object')   // succeed
    ->json($json3)
        ->isNot('null')     // succeed
        ->isNot('boolean')  // succeed
        ->isNot('integer')  // succeed
        ->isNot('float')    // succeed
        ->isNot('string')   // succeed
        ->isNot('array')    // succeed
        ->isNot('object')   // failed
;

isNotArray

isNotArray 表示 JSON 是否不是数组。

public function isNotArray(string $message = null): self
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNotArray()  // failed
    ->json($json2)
        ->isNotArray()  // succeed
    ->json($json3)
        ->isNotArray()  // succeed
;

isNotBool

isNotBool 表示 JSON 是否不是布尔值。

public function isNotBool(string $message = null): self
<?php

$json1 = 'true';
$json2 = 'false';
$json3 = 'null';
$json4 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNotBool()  // failed
    ->json($json2)
        ->isNotBool()  // failed
    ->json($json3)
        ->isNotBool()  // succeed
    ->json($json4)
        ->isNotBool()  // succeed
;

isNotBoolean

isNotBoolean 表示 JSON 是否不是布尔值。

public function isNotBoolean(string $message = null): self
<?php

$json1 = 'true';
$json2 = 'false';
$json3 = 'null';
$json4 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNotBoolean()  // failed
    ->json($json2)
        ->isNotBoolean()  // failed
    ->json($json3)
        ->isNotBoolean()  // succeed
    ->json($json4)
        ->isNotBoolean()  // succeed
;

isNotEqualTo

public function isNotEqualTo($value, string $message = null)

isNotEqualTo 是从 variable 断言器继承的方法。有关更多信息,请参阅 variable::isNotEqualTo 的文档。

isNotFloat

isNotFloat 表示 JSON 是否不是浮点数。

public function isNotFloat(string $message = null): self
<?php

$json1 = '3.14';
$json2 = '123';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNotFloat()  // failed
    ->json($json2)
        ->isNotFloat()  // succeed
    ->json($json3)
        ->isNotFloat()  // succeed
;

isNotIdenticalTo

public function isNotIdenticalTo($value, string $message = null)

isNotIdenticalTo 是从 variable 断言器继承的方法。有关更多信息,请参阅 variable::isNotIdenticalTo 的文档。

isNotInteger

isNotInteger 表示 JSON 是否不是整数。

public function isNotInteger(string $message = null): self
<?php

$json1 = '123';
$json2 = '3.14';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNotInteger()  // failed
    ->json($json2)
        ->isNotInteger()  // succeed
    ->json($json3)
        ->isNotInteger()  // succeed
;

isNotNull

isNotNull 表示 JSON 是否不是 null。

public function isNotNull(string $message = null): self
<?php

$json1 = 'null';
$json2 = '3.14';
$json3 = '["foo", 123]';

$this
    ->json($json1)
        ->isNotNull()  // failed
    ->json($json2)
        ->isNotNull()  // succeed
    ->json($json3)
        ->isNotNull()  // succeed
;

isNotObject

isNotObject 表示 JSON 是否不是对象。

public function isNotObject(string $message = null): self
<?php

$json1 = '{ "foo": "bar" }';
$json2 = '["foo", 123]';
$json3 = '123';

$this
    ->json($json1)
        ->isNotObject()  // failed
    ->json($json2)
        ->isNotObject()  // succeed
    ->json($json3)
        ->isNotObject()  // succeed
;

isNotString

isNotString 表示 JSON 是否不是字符串。

public function isNotString(string $message = null): self
<?php

$json1 = '"foo"';
$json2 = '["foo", 123]';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isNotString()  // failed
    ->json($json2)
        ->isNotString()  // succeed
    ->json($json3)
        ->isNotString()  // succeed
;

isNull

isNull 表示 JSON 是否是 null。

public function isNull(string $message = null): self
<?php

$json1 = 'null';
$json2 = '3.14';
$json3 = '["foo", 123]';

$this
    ->json($json1)
        ->isNull()  // succeed
    ->json($json2)
        ->isNull()  // failed
    ->json($json3)
        ->isNull()  // failed
;

isObject

isObject 表示 JSON 是否是对象。

public function isObject(string $message = null): self
<?php

$json1 = '{ "foo": "bar" }';
$json2 = '["foo", 123]';
$json3 = '123';

$this
    ->json($json1)
        ->isObject()  // succeed
    ->json($json2)
        ->isObject()  // failed
    ->json($json3)
        ->isObject()  // failed
;

isString

isString 表示 JSON 是否是字符串。

public function isString(string $message = null): self
<?php

$json1 = '"foo"';
$json2 = '["foo", 123]';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->isString()  // succeed
    ->json($json2)
        ->isString()  // failed
    ->json($json3)
        ->isString()  // failed
;

notHasKey

notHasKey 检查指定名称的属性是否存在。

仅适用于基于对象的 JSON。

public function notHasKey(string $key, string $message = null): self
<?php

$json1 = '{ "foo": "bar" }';
$json2 = '["foo", 123]';

$this
    ->json($json1)
        ->notHasKey('foo')  // failed
        ->notHasKey('bar')  // succeed
    ->json($json2)
        ->notHasKey(0)      // failed
;

notHasKeys

notHasKeys 检查指定名称的属性是否存在。

仅适用于基于对象的 JSON。

public function notHasKeys(array $keys, string $message = null): self
<?php

$json1 = '{ "foo": "bar" }';
$json2 = '["foo", 123]';

$this
    ->json($json1)
        ->notHasKeys(['foo'])         // failed
        ->notHasKeys(['bar'])         // succeed
        ->notHasKeys(['foo', 'bar'])  // failed
    ->json($json2)
        ->notHasKeys([0])             // failed
;

notHasSize

notHasSize 检查JSON的大小。

仅适用于基于数组的 JSON。

public function notHasSize(int $count, string $message = null): self
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->notHasSize(2)  // failed
        ->notHasSize(3)  // succeed
    ->json($json2)
        ->notHasSize(1)  // failed
    ->json($json3)
        ->notHasSize(1)  // failed
;

size

size 根据数组大小返回一个整数断言。

仅适用于基于数组的 JSON。

public function size(): integer
<?php

$json1 = '["foo", 123]';
$json2 = '"foo"';
$json3 = '{ "foo": "bar" }';

$this
    ->json($json1)
        ->size()
            ->isEqualTo(2)  // succeed
            ->isEqualTo(3)  // failed
    ->json($json2)
        ->size()            // failed
    ->json($json3)
        ->size()            // failed
;

许可证

jdslv/atoum-json-extension 在MIT许可证下发布。有关详细信息,请参阅打包的 LICENSE 文件。

atoum