1

I get the following error when testing accessing the file system in my Symfony 2 application with the Filesystem component and then accessing the list of files in a directory with the Finder component.

Call to undefined method Prophecy\Prophecy\MethodProphecy::in()

This is a snippet of my method which I am testing:

$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);
   }    
}

This is my spec for the helper class:

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([]);
}
crmpicco
  • 16,605
  • 26
  • 134
  • 210

1 Answers1

1

You don't want to test your method using real files (looking at the code I assume you want to do it), tests should work without it. You need to unit test your code, thus you can fake the files paths found by Finder to whatever you like, the code should not rely on some 3rd party files in order to pass the tests.

You will want to return Finder object on $finder->files() method, see below:

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

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

Example:

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([]);
}
takeit
  • 3,991
  • 1
  • 20
  • 36
  • Thanks for the reply, this doesn't work for me though. I am getting the following error: `some predictions failed: Double\Symfony\Component\Finder\Finder\P17: No calls have been made that match: Double\Symfony\Component\Finder\Finder\P17->files() but expected at least one.` – crmpicco Mar 03 '16 at 13:36
  • 1
    try to remove `shouldBeCalled()` from `$finder->files()->shouldBeCalled()->willReturn($finder);` – takeit Mar 03 '16 at 13:39
  • Thank you! Can you update your answer and I will gladly accept? – crmpicco Mar 03 '16 at 13:44
  • 1
    Updated. I'm glad it helped! – takeit Mar 03 '16 at 13:46