Skip to content

Commit 3d85884

Browse files
committed
project tests
1 parent ed2058b commit 3d85884

File tree

7 files changed

+465
-3
lines changed

7 files changed

+465
-3
lines changed

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Fixtures are for databases, resources for the rest.
44

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

7+
## Version map
8+
9+
| branch | This package version | PHP min |
10+
|:------:|----------------------|:-------:|
11+
| 1.x | ^1.0 | PHP 7.2 |
12+
713
## Usage
814

915
```shell
@@ -12,14 +18,59 @@ composer require --dev erwane/phpunit-resource-helper
1218

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

21+
You can also configure your base directory and tmp directory in your `tests/bootstrap.php` file:
22+
```php
23+
use ResourceHelper\ResourceHelper;
24+
25+
ResourceHelper::setBaseDir('/project/tests_resources/');
26+
ResourceHelper::setTmpDir('/project/tmp/');
27+
```
28+
29+
In your test, you can get your resources path, content or copy with `File` methods:
1530
```php
1631
use ResourceHelper\File;
1732

1833
// Get <project_dir>/tests/resources/webhooks/mailgun.json content
19-
$content = File::instance()->getContent('webhooks/mailgun.json');
34+
$content = File::getContent('webhooks/mailgun.json');
2035

2136
// Create a copy you can manipulate without destroy your resource.
22-
$copy = File::instance()->getCopy('emails/change-my-headers.eml');
37+
$copy = File::getCopy('accounting/invoices.csv');
38+
```
39+
40+
## `File` Methods
41+
42+
### getPath(string \$path): string
43+
Get resource absolute path from relative `$path`.
44+
```php
45+
$path = File::getPath('file.csv');
46+
// $path = '<project>/tests/resources/my-file.csv'
2347
```
2448

25-
## Methods
49+
### getInfo(string \$path): array
50+
Get resource information from relative `$path`.
51+
```php
52+
$info = File::getInfo('file.csv');
53+
```
54+
`$info` will contain:
55+
```php
56+
[
57+
'path' => '/path/file.csv', // Absolute resource path
58+
'filename' => 'file.csv', // Filename
59+
'hash' => 'abcdef0123456789', // File hash
60+
]
61+
```
62+
63+
### getCopy(string \$path, TestCase \$test): array
64+
Copy the resource to a temporary directory relative to current test.
65+
Return the information of this copy.
66+
```php
67+
$info = File::getCopy('file.csv');
68+
```
69+
`$info` will contain:
70+
```php
71+
[
72+
'path' => '/tmp/project/Test_Method/file.ext', // Absolute resource copy path
73+
'filename' => 'file.ext', // Filename
74+
'hash' => 'abcdef0123456789', // File hash
75+
]
76+
```

src/File.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 InvalidArgumentException;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Helper for tests resources files.
18+
*/
19+
class File
20+
{
21+
/**
22+
* Look in $_paths for file resource and return path.
23+
*
24+
* @param string $path Resource name or path.
25+
* @return string
26+
* @throws \InvalidArgumentException
27+
*/
28+
public static function getPath(string $path): string
29+
{
30+
$resourcePath = ResourceHelper::getBaseDir() . $path;
31+
if (!is_file($resourcePath)) {
32+
throw new InvalidArgumentException(sprintf('Path "%s" not found', $path));
33+
}
34+
35+
return $resourcePath;
36+
}
37+
38+
/**
39+
* Get resource file info.
40+
*
41+
* @param string $path Resource name or path.
42+
* @return array
43+
*/
44+
public static function getInfo(string $path): array
45+
{
46+
$resourcePath = self::getPath($path);
47+
48+
$fileInfo = pathinfo($resourcePath);
49+
$hash = hash_file('sha1', $resourcePath);
50+
51+
return [
52+
'path' => $resourcePath,
53+
'filename' => $fileInfo['basename'],
54+
'hash' => $hash,
55+
];
56+
}
57+
58+
/**
59+
* Get resources file content.
60+
*
61+
* @param string $path Resource name or path.
62+
* @return string
63+
*/
64+
public static function getContent(string $path): string
65+
{
66+
return file_get_contents(self::getPath($path));
67+
}
68+
69+
/**
70+
* Get resources file infos with a copy of file resource.
71+
*
72+
* @param string $path Resource name or path.
73+
* @param \PHPUnit\Framework\TestCase $test Running test
74+
* @return array
75+
*/
76+
public static function getCopy(string $path, TestCase $test): array
77+
{
78+
$infos = self::getInfo($path);
79+
$resourcePath = $infos['path'];
80+
81+
$infos['path'] = ResourceHelper::createTmpTestDir($test) . $infos['filename'];
82+
83+
copy($resourcePath, $infos['path']);
84+
85+
return $infos;
86+
}
87+
}

src/ResourceHelper.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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 Composer\InstalledVersions;
14+
use DirectoryIterator;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* ResourceHelper
19+
*/
20+
class ResourceHelper
21+
{
22+
/**
23+
* @var string
24+
*/
25+
protected static $_baseDir;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected static $_tmpDir;
31+
32+
/**
33+
* Get resource base dir.
34+
*
35+
* @return string
36+
*/
37+
public static function getBaseDir(): string
38+
{
39+
if (empty(self::$_baseDir)) {
40+
self::$_baseDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR;
41+
}
42+
43+
return self::$_baseDir;
44+
}
45+
46+
/**
47+
* Set resources base dir.
48+
*
49+
* @param string $baseDir New resources base dir
50+
* @return void
51+
*/
52+
public static function setBaseDir(string $baseDir): void
53+
{
54+
self::$_baseDir = $baseDir;
55+
}
56+
57+
/**
58+
* Get resources tmp dir.
59+
*
60+
* @return string
61+
*/
62+
public static function getTmpDir(): string
63+
{
64+
if (empty(self::$_tmpDir)) {
65+
$root = InstalledVersions::getRootPackage();
66+
$project = preg_replace('#[._ /-]#', '-', $root['name']);
67+
68+
self::$_tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $project . DIRECTORY_SEPARATOR;
69+
}
70+
71+
return self::$_tmpDir;
72+
}
73+
74+
/**
75+
* Set resources tmp dir.
76+
*
77+
* @param string $tmpDir New project resources tmp dir
78+
* @return void
79+
*/
80+
public static function setTmpDir(string $tmpDir): void
81+
{
82+
self::$_tmpDir = $tmpDir;
83+
}
84+
85+
/**
86+
* Build the test dir path from TestCase.
87+
*
88+
* @param \PHPUnit\Framework\TestCase|string $test Running test or test method full name
89+
* @return string
90+
*/
91+
public static function getTmpTestPath($test): string
92+
{
93+
$name = $test;
94+
if ($test instanceof TestCase) {
95+
$name = $test->toString();
96+
}
97+
$namespace = explode('\\', $name);
98+
$method = str_replace('::', '_', end($namespace));
99+
100+
return ResourceHelper::getTmpDir() . $method . DIRECTORY_SEPARATOR;
101+
}
102+
103+
/**
104+
* Create tmp test directory.
105+
*
106+
* @param \PHPUnit\Framework\TestCase|string $test Running test or test method full name
107+
* @return string
108+
*/
109+
public static function createTmpTestDir($test): string
110+
{
111+
self::destroyTmpTestDir($test);
112+
113+
$dir = self::getTmpTestPath($test);
114+
mkdir($dir, 0700, true);
115+
116+
return $dir;
117+
}
118+
119+
/**
120+
* Destroy tmp test directory.
121+
*
122+
* @param \PHPUnit\Framework\TestCase|string $test Running test or test method full name
123+
* @return void
124+
*/
125+
public static function destroyTmpTestDir($test): void
126+
{
127+
$dir = self::getTmpTestPath($test);
128+
if (is_dir($dir)) {
129+
/** @var \DirectoryIterator $file */
130+
foreach (new DirectoryIterator($dir) as $file) {
131+
if (is_file($file->getPathname())) {
132+
unlink($file->getPathname());
133+
}
134+
}
135+
136+
rmdir($dir);
137+
}
138+
}
139+
}

tests/TestCase/FileTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\File;
15+
use ResourceHelper\ResourceHelper;
16+
17+
/**
18+
* @uses \ResourceHelper\File
19+
* @covers \ResourceHelper\File
20+
*/
21+
class FileTest extends TestCase
22+
{
23+
protected function tearDown(): void
24+
{
25+
parent::tearDown();
26+
ResourceHelper::destroyTmpTestDir($this);
27+
}
28+
29+
public function testGetPathUnknown(): void
30+
{
31+
$this->expectException(\InvalidArgumentException::class);
32+
$this->expectErrorMessage('Path "unknown.file" not found');
33+
File::getPath('unknown.file');
34+
}
35+
36+
public function testGetPath(): void
37+
{
38+
$path = File::getPath('basic.json');
39+
$this->assertEquals(ResourceHelper::getBaseDir() . 'basic.json', $path);
40+
}
41+
42+
public function testGetInfos(): void
43+
{
44+
$infos = File::getInfo('basic.json');
45+
46+
$this->assertSame([
47+
'path' => ResourceHelper::getBaseDir() . 'basic.json',
48+
'filename' => 'basic.json',
49+
'hash' => 'ea52ae55e385714418888d9bbd1ab849855eae3e',
50+
], $infos);
51+
}
52+
53+
public function testGetContent(): void
54+
{
55+
$content = File::getContent('basic.json');
56+
57+
$expected = <<<JSON
58+
{
59+
"key": "value",
60+
"int": 23
61+
}
62+
63+
JSON;
64+
$this->assertEquals($expected, $content);
65+
}
66+
67+
public function testGetCopy(): void
68+
{
69+
$copy = File::getCopy('basic.json', $this);
70+
$expected = ResourceHelper::getTmpDir() . 'FileTest_testGetCopy' . DIRECTORY_SEPARATOR . 'basic.json';
71+
$this->assertEquals($expected, $copy['path']);
72+
$this->assertFileExists($copy['path']);
73+
$this->assertEquals('ea52ae55e385714418888d9bbd1ab849855eae3e', hash_file('sha1', $copy['path']));
74+
}
75+
}

0 commit comments

Comments
 (0)