문제

내 문제는 모의 객체에 대한 모든 호출을 나열해야합니다. 왜냐하면 그들을 확인해야하기 때문입니다.나는 간단한 문서 에서이 기능에 대해 아무 것도 찾지 못했습니다.: s

내 코드를 테스트하는 또 다른 방법이 있습니다.

class Clean_Collection_Tree_NestedArrayParser {

    protected $path = array();
    protected $depth = -1;

    /** @var Clean_Collection_Tree_MapTreeInterface */
    protected $tree;

    public function setBasePath(array $path) {
        $this->path = $path;
    }

    public function setTree(Clean_Collection_Tree_MapTreeInterface $tree) {
        $this->tree = $tree;
    }

    public function parse($subject) {
        $this->parseArray($subject);
    }

    public function parseArray(array $array) {
        ++$this->depth;
        foreach ($array as $key => $value) {
            $this->path[$this->depth] = $key;
            if (is_array($value)) {
                $this->tree->put($this->path, new Clean_Collection_Map_Map());
                $this->parseArray($value);
            } else
                $this->tree->put($this->path, $value);
        }
        if (!empty($array))
            array_pop($this->path);
        --$this->depth;
    }

}
.

맵 개체 트리를 만들려는 중첩 된 배열을 기다리고있는 파서입니다. settree (clean_collection_tree_maptreeinterface $ tree) 과 함께 실제 트리를 주입하고 맵 트리 인터페이스는 다음과 같습니다.

interface Clean_Collection_Tree_MapTreeInterface extends Clean_Collection_CollectionInterface {

    public function putAll(array $array);

    public function put(array $path, $value);

    public function get(array $path);

    public function getAll(array $pathes);

    public function removeAll(array $pathes);

    public function remove(array $path);

    public function contains(array $path);
}
.

파서는 put (Array $ Path, $ value) 메소드 만 사용합니다.따라서 모든 PUT 메소드를 나열하면 파서에서 잘못된 것이 무엇인지 보여줍니다.(SimpleMock 에이 기능이없는 경우 인터페이스를 구현하는 자체 모의 객체를 만들 수 있습니다. 나는 그것에 있습니다.)

도움이 되었습니까?

해결책

문제는 SimpleMock 클래스 디자인에 있습니다 :

protected function addCall($method, $args) {

    if (! isset($this->call_counts[$method])) {
        $this->call_counts[$method] = 0;
    }
    $this->call_counts[$method]++;
}
.

SimpleMock에서 속성을 설정하는 대신 호출 속성을 기록하기위한 로거 클래스를 만들어야합니다 ... SimpleMock 클래스를 확장하여 해결 방법을 만들 수 있습니다.

class LoggedMock extends SimpleMock {

    protected $invokes = array();

    public function &invoke($method, $args) {
        $this->invokes[] = array($method, $args);
        return parent::invoke($method, $args);
    }

    public function getMockInvokes() {
        return $this->invokes;
    }

}
.

기본 모의 클래스로 설정하십시오 :

    require_once __DIR__.'simpletest/autorun.php';
    SimpleTest::setMockBaseClass('LoggedMock');
.

$ mockobj-> getMockinvokes () 로 호출 목록을 얻을 수 있습니다.

편집 : 메소드 이름이 소문자로 변환되므로 메소드 이름이 소문자로 확장되어 소문자 형식을 기록 할 수 있으므로 addCall을 확장 할 수 없습니다.캠셀 케이스.(소문자 변환이 실수라고 생각합니다 ...)

시연 테스트를 만들었습니다.

interface Nike {

    public function justDoIt();
}

class NikeUser {

    protected $nike;

    public function setNike(Nike $nike) {
        $this->nike = $nike;
    }

    public function doIt() {
        $this->nike->justDoIt();
    }

}

Mock::generate('Nike', 'MockNike');

class NikeUserTest extends UnitTestCase {

    public function testDoItButWeDontWantJustDoIt() {
        $mockNike = new MockNike();

        $nikeUser = new NikeUser();
        $nikeUser->setNike($mockNike);

        $expectedInvokes = array();

        $nikeUser->doIt();
        $expectedInvokes[] = array('justDoIt', array());
        $this->assertEqual($expectedInvokes, $mockNike->getMockInvokes());
    }

}
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top