Reputation: 17181
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([]);
}
Upvotes: 1
Views: 982
Reputation: 4081
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([]);
}
Upvotes: 2