Question

I decided to write simple MVC with BDD workflow. I want to implement method which sets some property of a class. The thing is connected with file path. In this method I want to check if path is correct and if this file exists. Final product should be look like:

<?php
class SomeClass
{
     public function setProperty($property_value)
     {
         if (!file_exists($property_value)) {
             throw new CustomFileNotFoundException();
         }
         $this->someProperty = $property_value;
     }
}

How should I implement a test method (example) in PhpSpec? I don't want to creat "dummy" files in spec tests, I'm sure there is some mock/stab method for that but I don't know how to handle this... I just want to check if this property is being set.
Should I create separate middle layer for filesystem and then mock it? Thanks for any help!

Was it helpful?

Solution

I can see three options here. First two you already figured out yourself ;)

You could either create and remove "dummy" files or inject a mock of collaborator for filesystem manipulation (have a look at the Symfony's Filesystem component).

Third option would be to use a stream wrapper for virtual filesystem, like vfsStream. Here's a nice explanation of how it works: https://github.com/mikey179/vfsStream/wiki/Example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top