2017-05-12 5 views
0

seeResponseMatchesJsonType 메서드를 통해 유효성 검사 API 응답에 대해 일부 사용자 정의 JsonType을 정의 할 수 있는지 궁금합니다. 내 말은,이 전 구조와 반응을 가정하자 :Codeception 새 JsonTypes 추가

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

분명히이 구조는 복합 형 address 임베디드하고 전체 응용 프로그램에 이러한 유형은 그래서 간단하게 작성하려합니다 여러 번 사용할 수 있습니다

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

이 임베디드 구조를 항상 다시 작성하지 않아도됩니다. 나는 어떻게 그것을 Codeception에서 얻을 수 있습니까?

+0

변수를 대신 사용하는 것은 불가능합니다. 다른 테스트에서 액세스하려는 경우 도우미 클래스의 정적 속성으로 설정하십시오. – Naktibalda

+0

헬퍼에서 이미 상수를 사용했지만 이러한 기능을 사용할 수 있는지 물어보고 싶습니다. :) – Moby04

답변

1

예, \ 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; 
    }