Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Hassle-Free Filesystem Operations during Testing? Yes Please!
Jul 28, 2016 @ 17:24:56

On the SitePoint PHP blog there's a new tutorial posted suggesting a method for hassle-free filesystem operations during testing in your applications. Traditionally external sources, including the file system have proved difficult to test mostly because connection/state issues or conflicts.

When working with the filesystem in our tests suites, a big concern is cleaning up the temporary files after each test runs. However, if for any reason the test’s execution is interrupted before the cleanup phase, further tests might fail, as the environment has not been cleaned up.

In this post, we will use a library named vfsStream to create filesystem mocks. It’s little more than a wrapper around a virtual filesystem, which also works very nicely with PHPUnit.

They start by creating a simple FileCreator class that just uses a file_put_contents call to write data to a provided path. They start with the traditional approach in testing - just writing to the actual file and ensuring it exists. Then comes vfsStream, changing up the testing to use mocks of the directory and file and it's own checks to ensure existence. These mocks work in basically the same way as a directory/filesystem structure would without the external interaction making it much easier to test in isolation.

tagged: filesystem testing phpunit unittest vfsstream package tutorial

Link: https://www.sitepoint.com/hassle-free-filesystem-operations-during-testing/


Trending Topics: