erwane/phpunit-resource-helper

File resources helpers for PHPUnit tests

1.x-dev 2025-08-02 15:48 UTC

This package is auto-updated.

Last update: 2025-08-02 15:49:20 UTC


README

Software License codecov Build Status Packagist Downloads Packagist Version

Fixtures are for databases, resources for the rest.

Help your phpunit tests to load resources from files, like, json content, raw e-mails, logs file, etc.

Version map

branch This package version PHP min
1.x ^1.0 PHP 7.2

Usage

composer require --dev erwane/phpunit-resource-helper

Create a resources directory in your tests dir and put your files in. You can add subdirectories.

You can also configure your base directory and tmp directory in your tests/bootstrap.php file:

use ResourceHelper\ResourceHelper;

ResourceHelper::setBaseDir('/project/tests_resources/');
ResourceHelper::setTmpDir('/project/tmp/');

In your test, you can get your resources path, content or copy with File methods:

use ResourceHelper\File;

// Get <project_dir>/tests/resources/webhooks/mailgun.json content
$content = File::getContent('webhooks/mailgun.json');

// Create a copy you can manipulate without destroy your resource.
$copy = File::getCopy('accounting/invoices.csv');

You can clean your tmp directory with PHPUnit extension.
Only successful tests are cleaned, this allows you to check your resources copy files when test failed.
Set up ResourceHelper extension in your phpunit.dist.xml configuration file:

<!-- phpunit.dist.xml -->
<extensions>
    <extension class="ResourceHelper\PHPUnitExtension"></extension>
</extensions>

File Methods

getPath(string $path): string

Get resource absolute path from relative $path.

$path = File::getPath('file.csv');
// $path = '<project>/tests/resources/my-file.csv'

getInfo(string $path): array

Get resource information from relative $path.

$info = File::getInfo('file.csv');

$info will contain:

[
    'path' => '/path/file.csv', // Absolute resource path
    'filename' => 'file.csv', // Filename
    'hash' => 'abcdef0123456789', // File hash
]

getCopy(string $path, TestCase $test): array

Copy the resource to a temporary directory relative to current test.
Return the information of this copy.

$info = File::getCopy('file.csv');

$info will contain:

[
    'path' => '/tmp/project/Test_Method/file.ext', // Absolute resource copy path
    'filename' => 'file.ext', // Filename
    'hash' => 'abcdef0123456789', // File hash
]