Testing Codeception添加新的JsonTypes

Testing Codeception添加新的JsonTypes,testing,codeception,web-api-testing,Testing,Codeception,Web Api Testing,我想知道是否可以通过参见responseMatchesJSONType方法为验证API响应定义一些自定义的JsonType。我的意思是,假设我有一个结构的响应: [ 'id' => 'integer', 'name' => 'string', 'address' => [ 'street' => 'string', 'city' => 'string' ] ] 显然,该结构嵌入了复杂的类型address,在整个应用程

我想知道是否可以通过
参见responseMatchesJSONType
方法为验证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;
    }
/**
*将自定义筛选器添加到JsonType列表。
*您应该指定过滤器的名称和参数。
*
*例如:
*
*``` php
* 
* ```
*
*@param$name
*@param callable$callable
*/
公共静态函数addCustomFilter($name,callable$callable)
{
静态::$customFilters[$name]=$callable;
}

目前这是不可能的,改用变量怎么样?如果您想从不同的测试中访问它,请将其设置为helper类的静态属性。我已经在helpers中使用了常量,但想询问是否有此功能可用。:)