gpt4 book ai didi

testing - Codeception 添加新的 JsonTypes

转载 作者:行者123 更新时间:2023-11-28 21:24:30 25 4
gpt4 key购买 nike

我想知道是否可以通过 seeResponseMatchesJsonType 方法为验证 API 响应定义一些自定义 JsonType。我的意思是,假设我有一个结构的响应:

[
'id' => 'integer',
'name' => 'string',
'address' => [
'street' => 'string',
'city' => 'string'
]
]

显然这个结构嵌入了复杂类型 address 并且在整个应用程序中这种类型可能会被多次使用所以我想简单地写一下:

$I->seeResponseMatchesJsonType([
'id' => 'integer',
'name' => 'string',
'address' => 'addressType'
]);

无需一直重写这个嵌入式结构。我如何在 Codeception 中实现它?

最佳答案

是的,您可以使用\Codeception\Util\JsonType 类中的方法 addCustomFilter 来执行此操作。

/**
* Adds custom filter to JsonType list.
* You should specify a name and parameters of a filter.
*
* Example:
*
* ```php
* <?php
* JsonType::addCustomFilter('email', function($value) {
* return strpos('@', $value) !== false;
* });
* // => use it as 'string:email'

*
* // add custom function to matcher with `len($val)` syntax
* // parameter matching patterns should be valid regex and start with `/` char
* JsonType::addCustomFilter('/len\((.*?)\)/', function($value, $len) {
* return strlen($value) == $len;
* });
* // use it as 'string:len(5)'
* ?>
* ```
*
* @param $name
* @param callable $callable
*/
public static function addCustomFilter($name, callable $callable)
{
static::$customFilters[$name] = $callable;
}

关于testing - Codeception 添加新的 JsonTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43931250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com