2016-12-06 122 views
1

我試圖單元測試我的服務,包含symfony2(http://symfony.com/doc/current/components/finder.html)的依賴搜索組件。phpunit必須可遍歷或實現接口迭代器

我越來越:

[例外]對象由Mock_Finder_91776c5c :: getIterator()方法返回 必須穿越或實現接口的Iterator

服務:

public function getFile($fileName, $path = '/') 
    { 

     if($this->wrapper == null || $this->connection == null) 
      throw new LogicException("call method setFTP first"); 

     // get file on ftp server 
     $this->connection->open(); 
     $downloadComplete = $this->wrapper->get($this->tmpDir . $fileName, $path . $fileName); 
     $this->connection->close(); 

     if($downloadComplete == false) 
      return false; // TODO exception ? 

     // return file downloaded 
     $this->finder->files()->in(__DIR__); 
     foreach ($this->finder as $file) { 
      return $file; 
     } 

     return false; // TODO exception ? 

    } 

和測試

class FtpServiceTest extends \PHPUnit_Framework_TestCase 
{ 

    protected $connectionMock; 
    protected $ftpWrapperMock; 
    protected $finderMock; 

    protected function setUp() 
    { 
     $this->connectionMock = $this->getConnectionMock(); 
     $this->ftpWrapperMock = $this->getFTPWrapperMock(); 
     $this->finderMock = $this->getFinderMock(); 
    } 

    protected function tearDown() 
    { 
    } 

    private function getFinderMock() 
    { 
     return $this->getMockBuilder(Finder::class) 
      ->disableOriginalConstructor() 
      ->getMock('Iterator'); 
    } 

    private function getConnectionMock() 
    { 
     return $this->getMockBuilder(Connection::class) 
      ->disableOriginalConstructor() 
      ->getMock(); 
    } 

    private function getFTPWrapperMock() 
    { 
     return $this->getMockBuilder(FTPWrapper::class) 
      ->disableOriginalConstructor() 
      ->getMock(); 
    } 

    // tests 
    public function testMe() 
    { 

     // arrange 
     $host = 'localhost'; 
     $user = 'user'; 
     $password = '1234'; 

     $filesArray = new ArrayObject(array('')); 

     $service = new FtpService('var/tmp/'); 
     $service->setFTP($this->connectionMock, $this->ftpWrapperMock); 
     $service->setFinder($this->finderMock); 

     $this->connectionMock 
      ->expects($this->once()) 
      ->method('open'); 

     $this->ftpWrapperMock 
      ->expects($this->once()) 
      ->method('get') 
      ->will($this->returnValue(true)); 

     $this->connectionMock 
      ->expects($this->once()) 
      ->method('close'); 

     $this->finderMock 
      ->expects($this->once()) 
      ->method('files') 
      ->will($this->returnValue($this->finderMock)); 

     $this->finderMock 
      ->expects($this->once()) 
      ->method('in') 
      ->will($this->returnValue($filesArray)); 

     // act 
     $file = $service->getFile('/file.zip'); 

     // assert 
     $this->assertInstanceOf(SplFileInfo::class, $file); 

    } 

} 

回答

1

Finder類需要實現嘲笑實例/嘲笑方法getIterator(該getMock方法不接受參數,所以沒有通過'Iterator'字符串),所以更改代碼如下:

private function getFinderMock() 
{ 
    return $this->getMockBuilder(Finder::class) 
     ->disableOriginalConstructor() 
     ->getMock(); 
} 

,並添加嘲笑預期中的測試方法,如例如:

$this->finderMock->expects($this->once()) 
     ->method('getIterator') 
     ->willReturn(new \ArrayObject([$this->createMock(SplFileInfo::class)])); 

希望這有助於