Completed
Pull Request — master (#237)
by Théo
04:25 queued 01:49
created

FileSystemTestCase::normalizePaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Test;
16
17
use PHPUnit\Framework\TestCase;
18
use function KevinGH\Box\FileSystem\make_tmp_dir;
19
use function KevinGH\Box\FileSystem\remove;
20
21
/**
22
 * @private
23
 */
24
abstract class FileSystemTestCase extends TestCase
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $cwd;
30
31
    /**
32
     * @var string
33
     */
34
    protected $tmp;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function setUp(): void
40
    {
41
        parent::setUp();
42
43
        // Cleans up whatever was there before. Indeed upon failure PHPUnit fails to trigger the `tearDown()` method
44
        // and as a result some temporary files may still remain.
45
        remove(str_replace('\\', '/', realpath(sys_get_temp_dir())).'/box');
46
47
        $this->cwd = getcwd();
48
        $this->tmp = make_tmp_dir('box', __CLASS__);
49
50
        chdir($this->tmp);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function tearDown(): void
57
    {
58
        parent::tearDown();
59
60
        chdir($this->cwd);
61
62
        remove($this->tmp);
63
    }
64
65
    /**
66
     * @param string[] $files
67
     *
68
     * @return string[] File real paths relative to the current temporary directory
69
     */
70
    final protected function normalizePaths(array $files): array
71
    {
72
        $root = $this->tmp;
73
74
        $files = array_values(
75
            array_map(
76
                function (string $file) use ($root): string {
77
                    return str_replace($root.DIRECTORY_SEPARATOR, '', $file);
78
                },
79
                $files
80
            )
81
        );
82
83
        natcasesort($files);
84
85
        return array_values($files);
86
    }
87
}
88