erwane/phpunit-resource-helper
最新稳定版本:2.1.1
Composer 安装命令:
composer require erwane/phpunit-resource-helper
包简介
File resources helpers for PHPUnit tests
关键字:
README 文档
README
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 | PHPUnit |
|---|---|---|---|
| 1.x | ^1.0 | PHP 7.2 | ^7.1 | ^8.0 | ^9.0 |
| 2.0 | 2.0.* | PHP 8.0 | ^8.5 | ^9.3 |
| 2.1 | ^2.1 | PHP 8.1 | ^8.5 | ^9.3 | ^10.0 | ^11.0 | ^12.0 |
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 ^8.5 & ^9.3
<!-- phpunit.dist.xml --> <extensions> <extension class="ResourceHelper\PHPUnitHooks"></extension> </extensions>
PHPUnit >=10
<!-- 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
]
统计信息
- 总下载量: 202
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-02