首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >phpunit必须是可遍历的或实现接口迭代器

phpunit必须是可遍历的或实现接口迭代器
EN

Stack Overflow用户
提问于 2016-12-06 23:39:31
回答 1查看 2.8K关注 0票数 2

我正在尝试对我的服务进行单元测试,其中包含symfony2(http://symfony.com/doc/current/components/finder.html)的依赖项查找器组件。

我得到了:

Mock_Finder_91776c5c::getIterator()返回的

异常对象必须是可遍历的或实现接口迭代器

服务:

代码语言:javascript
复制
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 ?

    }

和测试

代码语言:javascript
复制
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);

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-07 00:27:25

Finder类的模拟实例需要实现/模拟方法getIteratorgetMock方法不接受参数,因此不要传递字符串'Iterator'-change代码,如下所示:

代码语言:javascript
复制
private function getFinderMock()
{
    return $this->getMockBuilder(Finder::class)
        ->disableOriginalConstructor()
        ->getMock();
}

并在测试方法中添加模拟期望,例如:

代码语言:javascript
复制
    $this->finderMock->expects($this->once())
        ->method('getIterator')
        ->willReturn(new \ArrayObject([$this->createMock(SplFileInfo::class)]));

希望这能有所帮助。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40999220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档