Completed
Pull Request — master (#3)
by Harry
05:31
created

AbstractFileTestCase::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/data-file
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-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test;
15
16
abstract class AbstractFileTestCase extends TestCase
17
{
18
    const TEST_DATA_PATH = '/tmp/data/';
19
20
    /**
21
     * @var string
22
     */
23
    protected static $dir;
24
25
    public static function setUpBeforeClass()
26
    {
27
        static::$dir = self::getTestDir();
28
    }
29
30
    public static function tearDownAfterClass()
31
    {
32
        if (is_dir(static::$dir)) {
33
            self::rmDirRecursive(static::$dir);
34
        }
35
    }
36
37
    /**
38
     * Get the directory used for testing file io
39
     *
40
     * @return string
41
     */
42
    private static function getTestDir()
43
    {
44
        date_default_timezone_set('UTC');
45
        $dir = static::TEST_DATA_PATH . strftime('%Y%m%d-%H%M/');
46
        if (!file_exists($dir)) {
47
            mkdir($dir, 0777, true);
48
        }
49
        return $dir;
50
    }
51
52
    /**
53
     * Delete the folder and all files/folders within it
54
     *
55
     * @param string $path
56
     *
57
     * @return bool
58
     */
59
    private static function rmDirRecursive($path)
60
    {
61
        $files = array_diff(scandir($path), ['.', '..']);
62
        foreach ($files as $file) {
63
            (is_dir("$path/$file")) ? self::rmDirRecursive("$path/$file") : unlink("$path/$file");
64
        }
65
        return rmdir($path);
66
    }
67
}
68