Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 579
Pekka Avatar asked Mar 26 '10 11:03

Pekka


1 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.

like image 197
Gordon Avatar answered Sep 29 '22 13:09

Gordon