Completed
Push — master ( 95fe89...6b0ea2 )
by Dave
19s queued 16s
created

ProjectRootTest::testProjectRootWithRelativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Core\Common;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\AbsoluteFileName;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\InvalidPathException;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName;
11
use PHPUnit\Framework\TestCase;
12
13
class ProjectRootTest extends TestCase
14
{
15
    private const CURRENT_WORKING_DIRECTORY = '/home/sarb';
16
    private const RELATIVE_PATH = 'foo/bar';
17
    private const ABSOLUTE_PATH = '/vagrant/code';
18
19
    public function testInstantiateRelativeToCurrentWorkingDirectory(): void
20
    {
21
        $projectRoot = ProjectRoot::fromProjectRoot(self::RELATIVE_PATH, self::CURRENT_WORKING_DIRECTORY);
22
        $this->assertEquals(self::CURRENT_WORKING_DIRECTORY.\DIRECTORY_SEPARATOR.self::RELATIVE_PATH, (string) $projectRoot);
23
    }
24
25
    public function testInstantiateWithAbsolutePath(): void
26
    {
27
        $projectRoot = ProjectRoot::fromProjectRoot(self::ABSOLUTE_PATH, self::CURRENT_WORKING_DIRECTORY);
28
        $this->assertEquals(self::ABSOLUTE_PATH, (string) $projectRoot);
29
    }
30
31
    public function testNonCanonicalPath(): void
32
    {
33
        $projectRoot = ProjectRoot::fromProjectRoot('/foo/baz/../bar', self::CURRENT_WORKING_DIRECTORY);
34
        $this->assertEquals('/foo/bar', (string) $projectRoot);
35
    }
36
37
    public function testRemoveProjectRootNoTrailingSlash(): void
38
    {
39
        $projectRoot = ProjectRoot::fromProjectRoot('/foo/bar', self::CURRENT_WORKING_DIRECTORY);
40
        $actual = $projectRoot->getPathRelativeToRootDirectory(new AbsoluteFileName('/foo/bar/baz/hello.php'));
41
        $this->assertSame('baz/hello.php', $actual->getFileName());
42
    }
43
44
    public function testRemoveProjectRootWithTrailingSlash(): void
45
    {
46
        $projectRoot = ProjectRoot::fromProjectRoot('/foo/bar/', self::CURRENT_WORKING_DIRECTORY);
47
        $actual = $projectRoot->getPathRelativeToRootDirectory(new AbsoluteFileName('/foo/bar/baz/hello.php'));
48
        $this->assertSame('baz/hello.php', $actual->getFileName());
49
    }
50
51
    public function testPathNotInProjectRoot(): void
52
    {
53
        $this->expectException(InvalidPathException::class);
54
        $expectedMessage = 'Path [/bar/baz.php] not in the project root [/foo/bar]. Is project root configured correctly?';
55
        $this->expectExceptionMessage($expectedMessage);
56
        $projectRoot = ProjectRoot::fromProjectRoot('/foo/bar', self::CURRENT_WORKING_DIRECTORY);
57
        $projectRoot->getPathRelativeToRootDirectory(new AbsoluteFileName('/bar/baz.php'));
58
    }
59
60
    public function testGetFullPath(): void
61
    {
62
        $projectRoot = ProjectRoot::fromProjectRoot('/foo/bar', self::CURRENT_WORKING_DIRECTORY);
63
        $fullPath = $projectRoot->getAbsoluteFileName(new RelativeFileName('fruit/apple.php'));
64
        $this->assertSame('/foo/bar/fruit/apple.php', $fullPath->getFileName());
65
    }
66
67
    public function testFromCurrentWorkingDirectory(): void
68
    {
69
        $projectRoot = ProjectRoot::fromCurrentWorkingDirectory(self::CURRENT_WORKING_DIRECTORY);
70
        $fullPath = $projectRoot->getAbsoluteFileName(new RelativeFileName('fruit/orange.php'));
71
        $this->assertSame('/home/sarb/fruit/orange.php', $fullPath->getFileName());
72
    }
73
74
    public function testProjectRootWithRelativePath(): void
75
    {
76
        $projectRoot = ProjectRoot::fromCurrentWorkingDirectory(self::CURRENT_WORKING_DIRECTORY);
77
        $projectRoot = $projectRoot->withRelativePath('foo/bar');
78
        $fullPath = $projectRoot->getAbsoluteFileName(new RelativeFileName('fruit/orange.php'));
79
        $this->assertSame('/home/sarb/foo/bar/fruit/orange.php', $fullPath->getFileName());
80
    }
81
}
82