News Feed
Sections




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

Lars Tesmer's Blog:
How to Unit Test a Class Making Calls to an URL (or the Filesystem) With PHPUnit
September 21, 2011 @ 12: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.

0 comments voice your opinion now!
phpunit unittest remote url filesystem resource mock vfsstream


blog comments powered by Disqus

Similar Posts

Community News: rPath Updates PHP, PHP-MySQL and PHP-PGSQL Packages

James Morris: PHPUnit Mocking and Method Chaining

Jaanus\' Blog: How to retrieve remote files in your web apps and still be friends with the server

Mike Purcell's Blog: PHPUnit - Constant Already Defined - -process-isolation

The bla.st Blog: Process your URLs in PHP with Apache mod_rewrite and wildcard DNS


Community Events

Don't see your event here?
Let us know!


series interview laravel5 release library development unittest podcast security community framework language opinion introduction laravel php7 extension voicesoftheelephpant version api

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework