erwane / phpunit-resource-helper
File resources helpers for PHPUnit tests
                                    Fund package maintenance!
                                                                            
                                                                                                                                        Erwane
                                                                                    
                                                                            
                                                                                                                                        Liberapay
                                                                                    
                                                                            
                                                                                                                                        Buy Me A Coffee
                                                                                    
                                                                            
                                                                                                                                        Patreon
                                                                                    
                                                                            
                                                                                                                                        Thanks Dev
                                                                                    
                                                                
Installs: 200
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/erwane/phpunit-resource-helper
Requires
- php: ^8.1
- ext-json: *
- phpunit/phpunit: ^8.5 | ^9.3 | ^10.0 | ^11.0 | ^12.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- symfony/var-dumper: ^v6.0
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
]