vfsStream Documentation
repository·master·Indexed 23 days ago
https://github.com/bovigo/vfsstreamA PHP stream wrapper that provides a virtual file system for mocking file system interactions during unit testing. It allows file operations to be performed in memory, ensuring tests are faster and isolated. Features include the LargeFileContent class for mocking sparse large files and the Quota class for managing disk space limits.
What's inside vfsStream
- vfsStream is a stream wrapper for a virtual file system. It is primarily used in unit testing to mock the real file system, allowing you to perform file operations (like reading, writing, or deleting) in memory without touching the actual disk. This makes tests faster, more isolated, and prevents side effects on the host machine. It is compatible with any PHP unit testing framework, such as PHPUnit or SimpleTest.
Initialize LargeFileContent with specific sizes
masterYou can instantiate
LargeFileContentusing the constructor with a byte count, or use the following static factory methods for easier sizing:withKilobytes(int $kilobyte): Creates a file of the specified size in KB.withMegabytes(int $megabyte): Creates a file of the specified size in MB.withGigabytes(int $gigabyte): Creates a file of the specified size in GB.
Manage LargeFileContent size and truncation
masterThe
LargeFileContentclass provides methods to inspect and modify the virtual file size:size(): int: Returns the current size of the file in bytes.truncate(int $size): bool: Truncates the file to the specified length. If the new size is smaller, data beyond the new size is removed. If larger, the file size is updated.
Manage disk space limits with the Quota class
masterThe
Quotaclass is used to represent and manage disk space limits within the virtual file system. You can define a specific limit in bytes or set an unlimited quota.Key methods:
Quota::unlimited(): A static factory method to create a quota with no limit.isLimited(): Returnstrueif the quota has a specific byte limit, andfalseif it is set toUNLIMITED(-1).spaceLeft(int $usedSpace): Calculates the remaining space. If the quota is unlimited, it returns the$usedSpaceprovided (effectively indicating no limit is reached). If the limit is reached or exceeded, it returns0. Otherwise, it returns the difference between the limit and the used space.
Mock large files with LargeFileContent
masterThe
LargeFileContentclass allows you to mock files with a specific large size without consuming massive amounts of memory upfront. It behaves like a sparse file: when you write data, it is stored at specific offsets. When reading from offsets that haven't been written to, it returns spaces ().Warning: Calling
content()on aLargeFileContentinstance will attempt to return the entire file as a single string. For very large files, this can lead to memory exhaustion. It is better to use incremental reads/writes orsize()to manage large files.