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

Weebly Engineering Blog:
PHPUnit - Mocking the Filesystem with vfsStream
Feb 24, 2017 @ 16:52:23

On the Weebly Engineering blog there's a new post showing you how to combine PHPUnit and vfsStream to mock out file system operations away from the actual file system.

Recently I found myself needing to write tests for a small class that read from a json file. The class needed to read a json file, validate its existence and content, provide a method to inform the user if a certain key exists, and provide a method to retrieve a value for a given key.

[...] Testing this class in isolation can be tricky because it currently has a dependency on the file system. Storing test json files to test this class would work, but is not ideal because it leaves a dependency on the file system in your tests. As with any external resource, there might be intermittent problems with the file system and could result in some flaky tests. This is where vfsStream shines.

The post includes an example class under test that pulls in the JSON files and operates on the contents. To make the testing easier they introduce vfsStream, a wrapper that allows for a virtual "file system" that can be operated on through the usual interfaces. They include an example of its use in a test on the same class making it easier to check the JSON based on a pre-defined value (essentially a mock of the file and its contents).

tagged: vfsstream unittest mock filesystem tutorial

Link: https://medium.com/weebly-engineering/phpunit-mocking-the-file-system-using-vfsstream-5d7d79b1eb2a#.vdie5rhyr

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/

Sebastian Göttschkes' Blog:
Using vsfstream (with symfony2)
Apr 05, 2012 @ 16:32:50

In this recent post Sebastian Göttschkes shows how to use the vfsStream stream wrapper in a Symfony2 application to create tests that involve the local file system.

I read about vsfstream when skipping through the phpunit docs. Back then, I decided I don't need a virtual file system. Some time later, I had to test classes which read and write files and found myself creating and deleting temporary folders, messing around with nasty errors (like my favourite one where for some reasons tests fail when I don't use @runTestsInSeparateProcesses).

He walks you through the install process, how to register it in the Symfony2 autoloader (so you don't have to include the files each time) and how to include it (via namespace-based loading) in your tests.

tagged: vfsstream symfony2 tutorial filesystem unittest phpunit

Link:

VG Tech Blog:
Mocking the File System Using PHPUnit and vfsStream
Dec 09, 2011 @ 15:40:13

On the VG Tech blog today there's another post related to unit testing (here's one from before) but this time they're talking about mocking the filesystem with vfsStream, a powerful tool that lets you interact with PHP streams as a virtual file system.

This article is about how to mock the file system when writing unit tests, and it will be rather code-heavy. [...] PHPUnit is the de-facto standard for unit testing in PHP projects, and this is what we will be using together with vfsStream in this article.

The include the code for a simple storage driver (VGF_Storage_Driver_Filesystem) to use with vfsStream with "store", "delete" and "get" methods. Also included are examples of using vfsStream to check things like directory existence, if a file exists, or if a file can be read. A few simple assertions are set up in their sample test to check the methods in their "VGF_Storage_Driver_Filesystem" class.

tagged: unittest phpunit vfsstream tutorial filesystem mock

Link:

Lars Tesmer's Blog:
How to Unit Test a Class Making Calls to an URL (or the Filesystem) With PHPUnit
Sep 21, 2011 @ 17:04:47

Lars Tesmer has a suggestion for all of the unit testers out there (you do unit test your code, right?) when needing to test a piece of code that makes a call to something on the file system or a remote resource. Their examples come from tests written against the Assetic codebase.

For our most recent After Work Hacking my co-workers and me decided to write unit tests for the open source project Assetic. That turned out to be a better decision than our last one, yet we still ran into an interesting challenge.

In testing the HttpAsset class from the tool, they came across the problem - a call to a remote/file resource that could not be tested because of a file_get_contents call that depends on an external source. They came up with a few options to try to test this example, some better than others:

  • Give it a real URL to test with
  • Wrap the file_get_contents inside of a new class (ex. a "ContentFetcher")
  • Use vfsStream to mock out the file system in the unit test

In their case, vfsStream couldn't be used due to how the fetch call was made, but the tool can be very handy if you need to mock out an external file system resource.

tagged: phpunit unittest remote url filesystem resource mock vfsstream

Link:

Stubbles Blog:
vfsStream 0.1.0 Released
Dec 27, 2007 @ 17:11:00

On the Stubbles blog, Frank Kleine has posted about the release of a new wrapper class he's developed to go around a virtual file system.

Some minutes ago I released the first version of vfsStream. vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

The idea for the class came about when he thought about the use of real databases vs test databases and applied it to file systems. You can grab the latest version from their trac website as well as get more information about known issues and a brief example of its use.

tagged: vfsstream release virtual file system unittest phpunit simpletest database vfsstream release virtual file system unittest phpunit simpletest database

Link:

Stubbles Blog:
vfsStream 0.1.0 Released
Dec 27, 2007 @ 17:11:00

On the Stubbles blog, Frank Kleine has posted about the release of a new wrapper class he's developed to go around a virtual file system.

Some minutes ago I released the first version of vfsStream. vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

The idea for the class came about when he thought about the use of real databases vs test databases and applied it to file systems. You can grab the latest version from their trac website as well as get more information about known issues and a brief example of its use.

tagged: vfsstream release virtual file system unittest phpunit simpletest database vfsstream release virtual file system unittest phpunit simpletest database

Link:


Trending Topics: