Passed
Push — master ( 7bee4d...457c92 )
by Sebastian
01:54
created

UtilTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 34
dl 0
loc 102
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testRelativeArrayToPath() 0 4 1
A testGetSubPathOf() 0 4 1
A testAbsoluteArrayToPath() 0 4 1
A testRelativePathToArray() 0 7 1
A testIsSubdirectory() 0 6 1
A testGetTplTargetPath() 0 17 1
A testAbsolutePathToArray() 0 7 1
A testGetRelativeTest() 0 3 1
A testGetSubPathOfNoSubDirectory() 0 5 1
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Storage;
11
12
use PHPUnit\Framework\TestCase;
13
14
class UtilTest extends TestCase
15
{
16
    /**
17
     * Tests Util::arrayToPath
18
     */
19
    public function testAbsoluteArrayToPath(): void
20
    {
21
        $expected = DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR . 'bar';
22
        $this->assertEquals($expected, Util::arrayToPath(['foo', 'bar'], true));
23
    }
24
25
    /**
26
     * Tests Util::arrayToPath
27
     */
28
    public function testRelativeArrayToPath(): void
29
    {
30
        $expected = 'foo' . DIRECTORY_SEPARATOR . 'bar';
31
        $this->assertEquals($expected, Util::arrayToPath(['foo', 'bar']));
32
    }
33
34
    /**
35
     * Tests Util::pathToArray
36
     */
37
    public function testAbsolutePathToArray(): void
38
    {
39
        $absolute = DIRECTORY_SEPARATOR . 'foo' . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR . 'baz';
40
        $path     = Util::pathToArray($absolute);
41
42
        $this->assertCount(3, $path);
43
        $this->assertEquals('bar', $path[1]);
44
    }
45
46
    /**
47
     * Tests Util::pathToArray
48
     */
49
    public function testRelativePathToArray(): void
50
    {
51
        $relative = 'foo' . DIRECTORY_SEPARATOR . 'bar' . DIRECTORY_SEPARATOR . 'baz';
52
        $path     = Util::pathToArray($relative);
53
54
        $this->assertCount(3, $path);
55
        $this->assertEquals('bar', $path[1]);
56
    }
57
58
    /**
59
     * Tests Util::isSubDirectoryOf
60
     */
61
    public function testIsSubdirectory(): void
62
    {
63
        $this->assertTrue(Util::isSubDirectoryOf(['foo', 'bar', 'baz'], ['foo', 'bar']));
64
        $this->assertTrue(Util::isSubDirectoryOf(['foo', 'bar'], ['foo']));
65
        $this->assertFalse(Util::isSubDirectoryOf(['foo', 'bar', 'baz'], ['fiz', 'baz']));
66
        $this->assertFalse(Util::isSubDirectoryOf(['foo'], ['bar']));
67
    }
68
69
    /**
70
     * Tests Util::isSubDirectoryOf
71
     */
72
    public function testGetSubPathOf(): void
73
    {
74
        $this->assertEquals(['baz'], Util::getSubPathOf(['foo', 'bar', 'baz'],['foo', 'bar']));
75
        $this->assertEquals(['baz', 'buz'], Util::getSubPathOf(['foo', 'bar', 'baz', 'buz'], ['foo', 'bar']));
76
    }
77
78
    /**
79
     * Tests Util::isSubDirectoryOf
80
     */
81
    public function testGetSubPathOfNoSubDirectory(): void
82
    {
83
        $this->expectException(\Exception::class);
84
85
        Util::getSubPathOf(['foo', 'bar', 'baz'], ['fiz', 'baz']);
86
    }
87
88
    /**
89
     * Tests Util::getHookTargetPath
90
     */
91
    public function testGetTplTargetPath(): void
92
    {
93
        $repo   = Util::arrayToPath(['foo', 'bar'], true);
94
        $target = Util::arrayToPath(['foo', 'bar', 'baz', 'vendor'], true);
95
        $this->assertEquals('__DIR__ . \'/../../baz/vendor', Util::getTplTargetPath($repo, $target));
96
97
        $repo   = Util::arrayToPath(['foo', 'bar'], true);
98
        $target = Util::arrayToPath(['foo', 'bar', 'vendor'], true);
99
        $this->assertEquals('__DIR__ . \'/../../vendor', Util::getTplTargetPath($repo, $target));
100
101
        $repo   = Util::arrayToPath(['foo', 'bar'], true);
102
        $target = Util::arrayToPath(['foo', 'bar', 'captainhook.json'], true);
103
        $this->assertEquals('__DIR__ . \'/../../captainhook.json', Util::getTplTargetPath($repo, $target));
104
105
        $repo   = Util::arrayToPath(['foo', 'bar'], true);
106
        $target = Util::arrayToPath(['fiz', 'baz', 'captainhook.json'], true);
107
        $this->assertEquals('\'/fiz/baz/captainhook.json', Util::getTplTargetPath($repo, $target));
108
    }
109
110
    /**
111
     * Tests Util::getRelativePath
112
     */
113
    public function testGetRelativeTest(): void
114
    {
115
        $this->assertEquals('bar', Util::getRelativePath('/foo/bar', '/foo'));
116
    }
117
}
118