2016-12-06 2 views
1

symfony2의 의존성 Finder 구성 요소 (http://symfony.com/doc/current/components/finder.html)가 포함 된 서비스 테스트를하고 있습니다.phpunit은 traversable이거나 인터페이스 Iterator를 구현해야합니다.

나는군요 :

[예외] 인터페이스 반복자를에 이동이거나 구현해야 Mock_Finder_91776c5c ::에서는 getIterator() 에 의해 반환 된 개체

서비스 :

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)])); 

희망이 도움

관련 문제