Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PhpSpec测试Symfony 2 Finder组件的实现_Php_Symfony_Tdd_Php 5.6_Phpspec - Fatal编程技术网

用PhpSpec测试Symfony 2 Finder组件的实现

用PhpSpec测试Symfony 2 Finder组件的实现,php,symfony,tdd,php-5.6,phpspec,Php,Symfony,Tdd,Php 5.6,Phpspec,使用文件系统组件访问Symfony 2应用程序中的文件系统,然后使用查找器组件访问目录中的文件列表时,我遇到以下错误 调用未定义的方法Prophecy\Prophecy\MethodProphecy::in() 这是我正在测试的方法的一个片段: $finder = new Finder(); if ($filesystem->exists($imageImportPath)) { $finder->files()->in($imageImportPath); fo

使用
文件系统
组件访问Symfony 2应用程序中的文件系统,然后使用
查找器
组件访问目录中的文件列表时,我遇到以下错误

调用未定义的方法Prophecy\Prophecy\MethodProphecy::in()

这是我正在测试的方法的一个片段:

$finder = new Finder();

if ($filesystem->exists($imageImportPath)) {
   $finder->files()->in($imageImportPath);
   foreach ($finder as $image) {         
      // ...do some stuff in here with uploading images and creating an entity...
      $this->entityManager->persist($entity);
      $this->entityManager->flush($entity);
   }    
}
这是我对helper类的规范:

function it_imports_image_assets(
    Filesystem $filesystem,
    EntityManager $entityManager,
    Finder $finder
) {
    $imageImportPath = '/var/www/crmpicco/app/files/images/rfc-1872/';
    $filesystem->exists($imageImportPath)->willReturn(true);

    $finder->files()->in($imageImportPath)->shouldHaveCount(2);

    $this->importImageAssets($imageImportPath)->shouldReturn([]);
}

如果您不想使用真实的文件来测试您的方法(查看代码,我假设您想这样做),那么测试应该在没有它的情况下工作。你需要对你的代码进行单元测试,这样你就可以把
Finder
找到的文件路径伪造成你喜欢的任何东西,代码不应该依靠第三方文件来通过测试

您需要返回
$Finder->files()
方法上的
Finder
对象,请参见以下内容:

$finder->files()->shouldBeCalled()->willReturn($finder);
$finder->in($imageImportPath)->willReturn($finder);

$finder->getIterator()->willReturn(new \ArrayIterator([
    $file1->getWrappedObject(),
    $file2->getWrappedObject(),
]));
例如:

use Symfony\Component\Finder\SplFileInfo;
//..

function it_imports_image_assets(
    Filesystem $filesystem,
    EntityManager $entityManager,
    Finder $finder,
    SplFileInfo $file1,
    SplFileInfo $file2
) {
    $imageImportPath = '/var/www/crmpicco/app/files/images/rfc-1872/';
    $filesystem->exists($imageImportPath)->willReturn(true);
    $finder->files()->willReturn($finder);
    $finder->in($imageImportPath)->willReturn($finder);

    $finder->getIterator()->willReturn(new \ArrayIterator([
        $file1->getWrappedObject(),
        $file2->getWrappedObject(),
    ]));

    $file1->getPathname()->willReturn($imageImportPath.'file1.txt');
    $file2->getPathname()->willReturn($imageImportPath.'file1.txt');

    //...

    $this->importImageAssets($imageImportPath)->shouldReturn([]);
}

谢谢你的回复,但这对我来说不起作用。我收到以下错误:
一些预测失败:Double\Symfony\Component\Finder\Finder\P17:没有进行匹配的调用:Double\Symfony\Component\Finder\P17->files(),但至少应该有一个。
尝试从
$Finder->files()中删除
shouldBeCalled()
->shouldBeCalled()->willReturn($finder)谢谢!你能更新你的答案吗?我很乐意接受。我很高兴这有帮助!