1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-flow |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-flow/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-flow |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFlow\Test; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Node\LocalFile; |
17
|
|
|
|
18
|
|
|
abstract class RealFileTestCase extends TestCase |
19
|
|
|
{ |
20
|
|
|
const TEST_DATA_PATH = '/tmp/data/'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $dir; |
26
|
|
|
|
27
|
|
|
public static function setUpBeforeClass() |
28
|
|
|
{ |
29
|
|
|
static::$dir = self::getTestDir(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function tearDownAfterClass() |
33
|
|
|
{ |
34
|
|
|
if (is_dir(static::$dir)) { |
35
|
|
|
self::rmDirRecursive(static::$dir); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the directory used for testing file io |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
private static function getTestDir() |
45
|
|
|
{ |
46
|
|
|
date_default_timezone_set('UTC'); |
47
|
|
|
$dir = static::TEST_DATA_PATH . strftime('%Y%m%d-%H%M/'); |
48
|
|
|
if (!file_exists($dir)) { |
49
|
|
|
mkdir($dir, 0777, true); |
50
|
|
|
} |
51
|
|
|
return $dir; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Delete the folder and all files/folders within it |
56
|
|
|
* |
57
|
|
|
* @param string $path |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
private static function rmDirRecursive($path) |
62
|
|
|
{ |
63
|
|
|
$files = array_diff(scandir($path), array('.', '..')); |
64
|
|
|
foreach ($files as $file) { |
65
|
|
|
(is_dir("$path/$file")) ? self::rmDirRecursive("$path/$file") : unlink("$path/$file"); |
66
|
|
|
} |
67
|
|
|
return rmdir($path); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path |
72
|
|
|
* @param string|null $contents |
73
|
|
|
* |
74
|
|
|
* @return LocalFile |
75
|
|
|
*/ |
76
|
|
|
protected function makeFile($path, $contents = null) |
77
|
|
|
{ |
78
|
|
|
$file = new LocalFile(static::$dir . $path); |
79
|
|
|
if (!is_null($contents)) { |
80
|
|
|
$file->write($contents); |
81
|
|
|
} |
82
|
|
|
return $file; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|