Passed
Pull Request — master (#75)
by Dave
02:21 queued 11s
created

GitCliWrapperTest::removeTestDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Integration;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser\internal\GitCliWrapper;
10
use PHPUnit\Framework\Assert;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
class GitCliWrapperTest extends TestCase
15
{
16
    /**
17
     * @var Filesystem
18
     */
19
    private $fileSystem;
20
21
    /**
22
     * @var GitCliWrapper
23
     */
24
    private $gitWrapper;
25
    /**
26
     * @var ProjectRoot
27
     */
28
    private $projectRoot;
29
30
    protected function setUp(): void
31
    {
32
        $this->fileSystem = new Filesystem();
33
        $this->gitWrapper = new GitCliWrapper();
34
35
        $dateTimeFolderName = date('Ymd_His');
36
        $random = rand(0, 10000);
37
        $testDirectory = __DIR__."/../scratchpad/{$dateTimeFolderName}{$random}";
38
        $this->fileSystem->mkdir($testDirectory);
39
        $cwd = getcwd();
40
        $this->assertNotFalse($cwd);
41
        $this->projectRoot = new ProjectRoot($testDirectory, $cwd);
42
        $this->gitWrapper->init($this->projectRoot);
43
    }
44
45
    public function testNoChanges(): void
46
    {
47
        $this->assertClean();
48
    }
49
50
    public function testUntrackedFile(): void
51
    {
52
        $relativeFileName = new RelativeFileName('untracked.txt');
53
        $absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName);
54
        $this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'untracked');
55
56
        $this->assertNotClean();
57
    }
58
59
    public function testUpdatedFilesCommitted(): void
60
    {
61
        $relativeFileName = new RelativeFileName('committed.txt');
62
        $absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName);
63
64
        // Add and commit file to git
65
        $this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'committed');
66
        $this->gitWrapper->addAndCommit('Add file', $this->projectRoot);
67
68
        $this->assertClean();
69
    }
70
71
    public function testUnstaged(): void
72
    {
73
        $relativeFileName = new RelativeFileName('unstaged.txt');
74
        $absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName);
75
76
        // Add and commit file to git
77
        $this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'untracked');
78
        $this->gitWrapper->addAndCommit('Add file', $this->projectRoot);
79
80
        // Update
81
        $this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'modified but not staged');
82
83
        $this->assertNotClean();
84
    }
85
86
    public function testStaged(): void
87
    {
88
        $relativeFileName = new RelativeFileName('staged.txt');
89
        $absoluteFileName = $this->projectRoot->getAbsoluteFileName($relativeFileName);
90
91
        // Add and commit file to git
92
        $this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'untracked');
93
        $this->gitWrapper->addAndCommit('Add file', $this->projectRoot);
94
95
        // Update
96
        $this->fileSystem->dumpFile($absoluteFileName->getFileName(), 'staged');
97
        $this->gitWrapper->addAll($this->projectRoot);
98
99
        $this->assertNotClean();
100
    }
101
102
    public function assertNotClean(): void
103
    {
104
        Assert::assertFalse($this->gitWrapper->isClean($this->projectRoot));
105
        $this->removeTestDirectory();
106
    }
107
108
    private function assertClean(): void
109
    {
110
        Assert::assertTrue($this->gitWrapper->isClean($this->projectRoot));
111
        $this->removeTestDirectory();
112
    }
113
114
    private function removeTestDirectory(): void
115
    {
116
        $this->fileSystem->remove((string) $this->projectRoot);
117
    }
118
}
119