2012-03-14 34 views
1

我的問題是方法,我需要列出每次調用模擬對象,因爲我需要檢查他們。在SimpleTest文檔中我沒有發現任何有關此功能的信息。 :SPHP - SimpleTest的 - 列出每次調用模擬對象

也許還有另一種方式來測試我的代碼:

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; 
    } 

} 

這是等待一個嵌套的數組從我打算創造一個Map對象樹解析器。我注入實際的樹setTree(Clean_Collection_Tree_MapTreeInterface $樹)和地圖樹界面:

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方法都會告訴我解析器中出了什麼問題。 (如果SimpleMock不具備這個功能,我可以創造出我們實現接口我自己模仿的對象,我就可以了。)

回答

2

的問題是在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,因爲在調用方法的方法名轉換爲小寫的第一行,所以通過延伸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()); 
    } 

}