2012-03-14 32 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()); 
    } 

}