MockingLargeFiles

Matteo edited this page Jul 15, 2015 · 3 revisions

Mocking large files

Available since release 1.3.0

Working with large files with several hundred megabytes or even gigabytes is possible, but how to test that the code behaves correctly when processing such files? Of course one could put such a large file into the test directory, but this is impractical and simply bloats the size of the application or library. Instead, let's utilize the possibility of a virtual file system where a file can pretend to be that large, but doesn't necessarily have to contain that much data.

With vfsStream one can mock large files with arbitrary sizes. Instead of putting a simple string as content into the file we use a mock object:

$root      = vfsStream::setup();
$largeFile = vfsStream::newFile('large.txt')
                      ->withContent(LargeFileContent::withGigabytes(100))
                      ->at($root);

Now the file will report it is 100 Gigabyte in size:

var_dump(filesize($largeFile->url())); // int(107374182400)

Please note that the content of the large file when read consists of spaces only. However, if one writes something into it and reads this portion of the file afterwards, it will of course contain what was originally written at these offsets.

The org\bovigo\vfs\content\LargeFileContent class provides three factory methods which ease the creation of large files:

  • LargeFileContent::withGigabytes($gigabyte) will create a file content which is as large as the given amount of Gigabyte
  • LargeFileContent::withMegabytes($megabyte) will create a file content which is as large as the given amount of Megabyte
  • LargeFileContent::withKilobytes($kilobyte) will create a file content which is as large as the given amount of Kilobyte

It is also possible to specify the exact size in bytes using the constructor directly: new LargeFileContent(123456789) will create a file with a size of 123,456,789 bytes.