Unit testing file operations - where to maintain mock directory structure?

I'm still new to Unit testing, and specifically PHPUnit as the testing framework.

Suppose I'm building a unit test for a resource loader class. The class looks for resources to load in two directories (a global and a user-specific one).

To test the class, I would like to set up a mock testing directory containing some resource files. I would like to put the mock directory somewhere within the test suite (the suite is part of a web application that will get distributed.)

Is there a convention for where to put this testing directory in the context of a PHPUnit test suite composed using the file system as outlined here?

Is there a way (e.g. a constant defined by PHPUnit when it runs the test) to access such a mock directory from within a unit test without having to specify its exact location, i.e. a path relative to the current test suite? Or do I just use dirname(__FILE__) and compose an absolute path from that?

Answers


Since the resource loader looks for files in directories and you want to test the loader's correct interaction with these directories, you should mock the filesystem. If you test with a real directory, you are risking outside change to these directories that may affect the test outcome. Using the vfs is also much faster.


Need Your Help

Matlab MATPOWER Output in long format

matlab long-integer matpower

I've been trying to use MATPOWER to do a power flow analysis for a network i have but all the outputs are coming with only 2 decimal places. Is there a way to configure the output to have the long ...

asp.net datasource instantiate class

asp.net data-binding

I have a static method that takes a parameter and returns a class. the class has a ReadOnlyCollection Property that i'd like to display in a asp:repeater. Is there a way to do this using LinqData...