zlob / any-json-tester
帮助测试具有可变值的JSON,如时间戳、计数等
Requires
- php: >=5.5.9
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is auto-updated.
Last update: 2024-09-18 20:04:32 UTC
README
PhpUnit的扩展,帮助你测试具有可变值的JSON或数组,如时间戳、计数等。如果你决定测试API,这将非常有帮助。你所需要做的只是定义一组辅助类组成的数据结构。
安装
通过composer
composer require zlob/any-json-tester
示例
让我们测试以下JSON:
{ "id" : "1", "author" : "Zlob", "project" : "AnyJsonTester", "date" : "21-09-2015", "rating" : "9.9", "url" : "https://github.com/Zlob/AnyJsonTester", "comments" : [ { "text" : "awesome", "like" : "true" }, { "text" : "Very good", "like" : "true" } ] }'
测试代码
<?php //import classes use AnyJsonTester\Types\AnyArray; use AnyJsonTester\Types\AnyBoolean; use AnyJsonTester\Types\AnyDateTime; use AnyJsonTester\Types\AnyFloat; use AnyJsonTester\Types\AnyInteger; use AnyJsonTester\Types\AnyObject; use AnyJsonTester\Types\AnyString; class ExampleTest extends PHPUnit_Framework_TestCase { //use AnyJsonTester trait use \AnyJsonTester\AnyJsonTester; public function testSeeJsonLikeSimple() { //JSON, that we wont to test $JSON = '{ "id" : "1", "author" : "Zlob", "project" : "AnyJsonTester", "date" : "21-09-2015", "rating" : "9.9", "url" : "https://github.com/Zlob/AnyJsonTester", "comments" : [ { "text" : "awesome", "like" : "true" }, { "text" : "Very good", "like" : "true" } ] }'; //describe JSON schema $expectedJson = new AnyObject([ 'id' => new AnyInteger(), // you can set restrictions for type, like min, max 'author' => new AnyString(), // string length or regex 'date' => new AnyDateTime(), // date period or format 'rating' => new AnyFloat(), // precision 'project' => 'AnyJsonTester', // or you can just set explicit value // or skip some fields, that you don`t wont to test 'comments' => new AnyArray(new AnyObject( //you can test array of objects, set min and max array length [ 'text' => new AnyString(), 'like' => new AnyBoolean() //test boolean variables ] ) ) ] ); //call seeJsonLike function to test JSON against schema static::seeJsonLike($JSON, $expectedJson, false); } }
用法
安装后,在你的测试类中使用AnyJsonTester特性。它提供了两个方法:seeJsonLike用于测试JSON,以及seeArrayLike用于测试数组。
Laravel 5.1的一些糖
在我最喜欢的框架中,你可以使用AnyJsonTesterLaravel特性而不是AnyJsonTester特性,这将自动从响应中检索数据,因此你可以将seeJsonLike方法与请求方法链接起来,如下所示:
$this->post('/user', ['name' => 'Sally']) ->seeJsonLike( new AnyObject( [ 'name' => AnyJsonString() ] ) );
##接口特性只有两个公开方法:seeJsonLike和seeArrayLike,它们接受AnyObject或AnyArray对象作为参数(如果是非Laravel特性,则为测试的JSON字符串)。AnyObject用于测试JSON对象,例如'{"name" : "Zlob"}',可以使用以下额外类型。AnyArray用于测试类似对象(例如AnyObject、AnyInteger等)的JSON数组,甚至另一个AnyArray。##支持类型 ####AnyObject - 帮助测试JSON对象 #####参数
- options - 数组,可用的选项有
- hasFields - 字段和值的数组,对象应包含这些字段和值。除显式值外,您还可以使用支持的类型之一
- hasNoFields - 应包含的字段数组。
- strictMode - 布尔值。在严格模式下,对象只能包含在hasFields选项中描述的字段
- nullable - 布尔值。如果为true,对象可以为null。
#####示例
$anyObject = new AnyObject( [ 'hasFields' => ['id' => '1', 'name' => new AnyString()], 'hasNoFields' => ['password'], 'strictMode' => true, 'nullable' => false ] );
####AnyArray - 帮助测试数组 #####参数
- expectedElement - AnyObject、AnyBoolean、AnyDateTime、AnyFloat、AnyInteger、AnyString或另一个AnyArray对象,它描述了数组项的内容
- options - 数组,可用的选项有
- min - int,最小数组长度
- max - int,最大数组长度
- nullable - 布尔值。如果为true,数组可以为null。
#####示例
$anyArray = new AnyArray( new AnyObject(['name' => 'Zlob']), [ 'min' => 1, 'max' => 100, 'nullable' => true ] );
####AnyString - 帮助测试字符串 #####参数
- options - 数组,可用的选项有
- min - int,最小字符串长度
- max - int,最大字符串长度
- regex - 字符串,正则表达式模式
- enum - 数组,定义可能的字符串值
- nullable - 布尔值。如果为true,字符串可以为null。
#####示例
$anyString = new AnyString( [ 'min' => 1, 'max' => 20, 'regex' => '/rin/', 'enum' => ['value 1', 'value 2'], 'nullable' => true ] );
####AnyInteger - 帮助测试整数 #####参数
- options - 数组,可用的选项有
- min - int,最小整数值
- max - int,最大整数值
- nullable - 布尔值。如果为true,整数值可以为null。
#####示例
$anyInteger = new AnyInteger( [ 'min' => 1, 'max' => 2000, 'nullable' => true ] );
####AnyFloat - 帮助测试浮点数 #####参数
- options - 数组,可用的选项有
- min - int,最小浮点数值
- max - int,最大浮点数值
- precision - int,浮点数精度
- nullable - 布尔值。如果为true,浮点数值可以为null。
#####示例
$anyFloat = new AnyFloat( [ 'min' => 1, 'max' => 99.99, 'precision' => 2 'nullable' => true ] );
####AnyDateTime - 帮助测试日期和时间 #####参数
- options - 数组,可用的选项有
- min - int,最小日期/时间值
- max - int,最大日期/时间值
- format - 字符串,DateTime格式
- nullable - 布尔值。如果为true,值可以为null。
#####示例
$anyDateTime = new AnyDateTime( [ 'min' => '14-12-1987 23:57', 'max' => '22-09-2015 14:30', 'format' => 'd-m-Y H:i' 'nullable' => true ] );
####AnyBoolean - 帮助测试布尔值 #####参数
- options - 数组,可用的选项有
- strictMode - bool,如果为true,则只有'true'和'false'字符串是布尔值。否则,'true'、'false'、'0'、'1'、'on'、'off'、'yes'和'no'。
- nullable - 布尔值。如果为true,值可以为null。
#####示例
$anyBoolean = new AnyBoolean( [ 'strictMode' => false, 'nullable' => true ] );
许可证
麻省理工学院 - Zlob