André Roaldseth
Written by André Roaldseth
Published 2011-06-27

Unit testing with streams in PHP

Using the memory/temporary stream provided by php:// stream wrapper you  can create a stream with read and write access directly to RAM or to a temporary file.

Entry image

This gives you the possibilty to write unit tests that does not rely on a specific file, resource or stream, but rather on data provided by the test itself.

Creating a memory stream is easy; $fp = fopen('php://memory', 'r+');. Now you have a resource that can be used and manipulated just like any other stream or file.

Written by André Roaldseth
Published 2011-06-27