Skip to content

Commit 6ccc89d

Browse files
committed
PHPUnitExtension tests and doc.
1 parent 3d85884 commit 6ccc89d

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ composer require --dev erwane/phpunit-resource-helper
1818

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

21-
You can also configure your base directory and tmp directory in your `tests/bootstrap.php` file:
21+
You can also configure your base directory and tmp directory in your `tests/bootstrap.php` file:
22+
2223
```php
2324
use ResourceHelper\ResourceHelper;
2425

@@ -27,6 +28,7 @@ ResourceHelper::setTmpDir('/project/tmp/');
2728
```
2829

2930
In your test, you can get your resources path, content or copy with `File` methods:
31+
3032
```php
3133
use ResourceHelper\File;
3234

@@ -37,21 +39,38 @@ $content = File::getContent('webhooks/mailgun.json');
3739
$copy = File::getCopy('accounting/invoices.csv');
3840
```
3941

42+
You can clean your tmp directory with PHPUnit extension.
43+
Only successful tests are cleaned, this allows you to check your resources copy files when test failed.
44+
Set up ResourceHelper extension in your `phpunit.dist.xml` configuration file:
45+
46+
```xml
47+
<!-- phpunit.dist.xml -->
48+
<extensions>
49+
<extension class="ResourceHelper\PHPUnitExtension"></extension>
50+
</extensions>
51+
```
52+
4053
## `File` Methods
4154

4255
### getPath(string \$path): string
56+
4357
Get resource absolute path from relative `$path`.
58+
4459
```php
4560
$path = File::getPath('file.csv');
4661
// $path = '<project>/tests/resources/my-file.csv'
4762
```
4863

4964
### getInfo(string \$path): array
65+
5066
Get resource information from relative `$path`.
67+
5168
```php
5269
$info = File::getInfo('file.csv');
5370
```
71+
5472
`$info` will contain:
73+
5574
```php
5675
[
5776
'path' => '/path/file.csv', // Absolute resource path
@@ -61,12 +80,16 @@ $info = File::getInfo('file.csv');
6180
```
6281

6382
### getCopy(string \$path, TestCase \$test): array
83+
6484
Copy the resource to a temporary directory relative to current test.
6585
Return the information of this copy.
86+
6687
```php
6788
$info = File::getCopy('file.csv');
6889
```
90+
6991
`$info` will contain:
92+
7093
```php
7194
[
7295
'path' => '/tmp/project/Test_Method/file.ext', // Absolute resource copy path

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414
<directory suffix=".php" phpVersion="7.2">src/</directory>
1515
</whitelist>
1616
</filter>
17+
<extensions>
18+
<extension class="ResourceHelper\PHPUnitExtension"></extension>
19+
</extensions>
1720
</phpunit>

src/PHPUnitExtension.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* This file is part of PHPUnit resources helpers library
4+
*
5+
* @copyright Copyright (c) Erwane BRETON
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace ResourceHelper;
12+
13+
use PHPUnit\Runner\AfterSuccessfulTestHook;
14+
15+
/**
16+
* PHPUnit extension to clean up temp files.
17+
*/
18+
class PHPUnitExtension implements AfterSuccessfulTestHook
19+
{
20+
/**
21+
* Cleanup test files if case of successful result.
22+
*
23+
* @param string $test Test name with namespace
24+
* @param float $time Test duration time
25+
* @return void
26+
*/
27+
public function executeAfterSuccessfulTest(string $test, float $time): void
28+
{
29+
ResourceHelper::destroyTmpTestDir($test);
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* This file is part of PHPUnit resources helpers library
4+
*
5+
* @copyright Copyright (c) Erwane BRETON
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace ResourceHelper\Test\TestCase;
12+
13+
use PHPUnit\Framework\TestCase;
14+
use ResourceHelper\PHPUnitExtension;
15+
use ResourceHelper\ResourceHelper;
16+
17+
/**
18+
* @uses \ResourceHelper\PHPUnitExtension
19+
* @covers \ResourceHelper\PHPUnitExtension
20+
*/
21+
class PHPUnitExtensionTest extends TestCase
22+
{
23+
public function testAfterSuccessfulTest(): void
24+
{
25+
$ext = new PHPUnitExtension();
26+
$this->assertTrue(method_exists($ext, 'executeAfterSuccessfulTest'));
27+
28+
// Create tmp test dir
29+
ResourceHelper::createTmpTestDir($this);
30+
31+
$ext->executeAfterSuccessfulTest($this->toString(), 0.3);
32+
33+
$this->assertDirectoryNotExists(ResourceHelper::getTmpTestPath($this));
34+
}
35+
}

0 commit comments

Comments
 (0)