atoum

2017-11-28 9 views
0

내가 이전 메소드 호출 (및 인수)에 따라 다른 값을 반환 Atoum와 데이터베이스 ($ DB를) 모의을 설계하려고와 방법의 일련의 비웃음.atoum

을 :

$this->calling($db)->select = function($table, array $bind, $boolOperator = "AND") use ($permissionClientMapper, $db, $permissionsClientRaw){ 
    if($table === $permissionClientMapper->getTableName()){ 
     $this->calling($db)->fetchAll = function() use ($bind, $permissionsClientRaw){ 
      if(array_key_exists('type_service', $bind) && array_key_exists('id_service', $bind) && $bind['type_service'] === 'mutu' && $bind['id_service'] === 4012){ 
       return EXPECTED_RETURN_VALUE; 
      } 
      return null; 
     }; 
    } 
}; 

내가 (인수를) 호출 할 때 EXECTED_RETURN_VALUE를 반환하는 코드를 제외시켰다 것 : 나는 PHP 5.6과 Atoum 3.2 여기

을 사용하고

내가 뭘하려

1/ $db->select() -> This method is called as expected 
2/ $db->fetchAll() -> This one is never called 

나는 Atoum 설명서에이의 예를 찾을 수 없습니다.

사람이 연속 메소드 호출을 조롱하는 올바른 방법입니다 확인 할 수 ?

은 또한 폐쇄

$this->calling($db)->select = function($table, array $bind, $boolOperator = "AND") use ($permissionClientMapper, &$db, $permissionsClientRaw){ 
    if($table === $permissionClientMapper->getTableName()){ 
     $this->calling($db)->fetchAll = function() use ($bind, $permissionsClientRaw){ 
      if(array_key_exists('type_service', $bind) && array_key_exists('id_service', $bind) && $bind['type_service'] === 'mutu' && $bind['id_service'] === 4012){ 
       return EXPECTED_RETURN_VALUE; 
      } 
      return null; 
     }; 
    } 
}; 

의 데이터베이스에 대한 참조를 사용하려고하지만이 중 하나가 작동하지 않습니다.

편집 : 한 가지 해결 방법은 아마 올바른 인수와 함께 불렀다 확인하기 위해 모의를 테스트하기 위해 다음 각 호에 대해 다른 값을 반환 할 atoum 호출 순서를 사용하는 것입니다.

답변

2

나는 당신에게 당신의 질문에 대한 몇 가지 통찰력을 제공하고 희망 당신에게 그것을 해결하는 방법을 찾기 위해 몇 가지 단서를 제공 할 것입니다.

$this->mock($mock)->call('select')->once(); 

당신과 함께 처리하기 위해 모의 답 : 그래서

는 '결코'

$this->mock($mock)->call('fetchAll')->never(); 

그리고 호출 할 함께 사용 '전화'수, 모의 메서드가 호출되지 않았는지 확인합니다 , 당신은이 같은 여러 가지를 사용할 수 있습니다

$this->calling($db)->fetchAll[0] = null; // default answer 
$this->calling($db)->fetchAll[1] = function() {....} // first call to method 

만약 당신이 뭔가를 원한다면 : mocked m ethod select를 호출하고 fetchAll 메소드를 호출하면 대답은 ... atoum은 아직이 동작을 제공하지 않습니다. 가장 좋은 방법은 케이스를 노출하는 issue을 만드는 것입니다.

사용

당신은 모의의 동작을 정의하는 '호출'. 메서드가 호출되었을 때만, atoum이 모든 것을 가져 와서 해결할 것입니다. 당신이 http://docs.atoum.org/en/latest/asserters.html#mockhttp://docs.atoum.org/en/latest/mocking_systems.html 을 읽을 수있는 당신이 더 갈 수 있도록하며, 사용자는 질문에 태그를 할 수 있습니다

$this->calling($db)->fetchAll = function() use ($bind){ 
      if(array_key_exists('type_service', $bind) && array_key_exists('id_service', $bind) && $bind['type_service'] === 'mutu' && $bind['id_service'] === 4012){ 
       return EXPECTED_RETURN_VALUE; 
      } 
      return null; 
     }; 
$this->calling($db)->select = function($table, array $bind, $boolOperator = "AND") use ($permissionClientMapper, $db){ 
    if($table === $permissionClientMapper->getTableName()){ 
     return $db->fetchAll(); 
    } 
}; 
// this is the same as your code. But It a bit more readable 

$this->newTestedInstance; 
$this->testedInstance->setDb($db); 
$this->variable($this->testedInstance->doTheCallThatReturnNull()) 
    ->isEqualTo(null); 
// do some change in the vars to be in the value 
$this->variable($this->testedInstance->doTheCallThatReturnValue()) 
    ->isEqualTo(EXPECTED_RETURN_VALUE); 

추신 : 내가 제대로 질문을 이해한다면 나를 위해 그래서

, 나는 그처럼 쓸 것 'atoum'과 함께.

+0

많은 도움을 주셔서 감사합니다. 태그가 존재하지 않는 것 같고 작성하기에 충분한 담당자가 없어서 atoum 질문에 태그를 달 수 없습니다 ... – Ceyfiroth