Skip to content

Commit e86f3e6

Browse files
authored
Enhancement: Extract GitInfo value object (#120)
1 parent eab9ac2 commit e86f3e6

File tree

5 files changed

+165
-8
lines changed

5 files changed

+165
-8
lines changed

src/System/Git/GitCommand.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ class GitCommand extends SystemCommand
77
{
88
protected $commandPath = 'git';
99

10+
/**
11+
* @return GitInfoInterface
12+
*/
13+
public function getGitInfo()
14+
{
15+
return new GitInfo(
16+
$this->getHead(),
17+
$this->getBranch(),
18+
$this->getCommittedAt()
19+
);
20+
}
21+
1022
/**
1123
* @return string
1224
*/
13-
public function getHead()
25+
private function getHead()
1426
{
1527
$command = $this->createCommand("log -1 --pretty=format:'%H'");
1628

@@ -20,7 +32,7 @@ public function getHead()
2032
/**
2133
* @return string|null
2234
*/
23-
public function getBranch()
35+
private function getBranch()
2436
{
2537
$command = $this->createCommand("branch");
2638
$branches = $this->executeCommand($command);
@@ -37,7 +49,7 @@ public function getBranch()
3749
/**
3850
* @return int
3951
*/
40-
public function getCommittedAt()
52+
private function getCommittedAt()
4153
{
4254
$command = $this->createCommand("log -1 --pretty=format:'%ct'");
4355

src/System/Git/GitInfo.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace CodeClimate\PhpTestReporter\System\Git;
4+
5+
/**
6+
* @internal
7+
*/
8+
final class GitInfo implements GitInfoInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $head;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $branch;
19+
20+
/**
21+
* @var int
22+
*/
23+
private $committedAt;
24+
25+
/**
26+
* @param string $head
27+
* @param string|null $branch
28+
* @param int $committedAt
29+
*/
30+
public function __construct($head, $branch, $committedAt)
31+
{
32+
$this->head = $head;
33+
$this->branch = $branch;
34+
$this->committedAt = $committedAt;
35+
}
36+
37+
public function head()
38+
{
39+
return $this->head;
40+
}
41+
42+
public function branch()
43+
{
44+
return $this->branch;
45+
}
46+
47+
public function committedAt()
48+
{
49+
return $this->committedAt;
50+
}
51+
52+
public function toArray()
53+
{
54+
return array(
55+
'head' => $this->head,
56+
'branch' => $this->branch,
57+
'committed_at' => $this->committedAt,
58+
);
59+
}
60+
}

src/System/Git/GitInfoInterface.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CodeClimate\PhpTestReporter\System\Git;
4+
5+
/**
6+
* @internal
7+
*/
8+
interface GitInfoInterface
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function head();
14+
15+
/**
16+
* @return string
17+
*/
18+
public function branch();
19+
20+
/**
21+
* @return int
22+
*/
23+
public function committedAt();
24+
25+
/**
26+
* @return array
27+
*/
28+
public function toArray();
29+
}

src/TestReporter/Entity/JsonFile.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ protected function collectGitInfo()
4949
{
5050
$command = new GitCommand();
5151

52-
return array(
53-
"head" => $command->getHead(),
54-
"branch" => $command->getBranch(),
55-
"committed_at" => $command->getCommittedAt(),
56-
);
52+
return $command->getGitInfo()->toArray();
5753
}
5854

5955
/**

tests/Unit/System/Git/GitInfoTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace CodeClimate\PhpTestReporter\Tests\Unit\System\Git;
4+
5+
use CodeClimate\PhpTestReporter\System\Git\GitInfo;
6+
7+
class GitInfoTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testIsFinal()
10+
{
11+
$reflection = new \ReflectionClass('CodeClimate\PhpTestReporter\System\Git\GitInfo');
12+
13+
$this->assertTrue($reflection->isFinal());
14+
}
15+
16+
public function testImplementsGitInfoInterface()
17+
{
18+
$reflection = new \ReflectionClass('CodeClimate\PhpTestReporter\System\Git\GitInfo');
19+
20+
$this->assertTrue($reflection->implementsInterface('CodeClimate\PhpTestReporter\System\Git\GitInfoInterface'));
21+
}
22+
23+
public function testConstructorSetsValues()
24+
{
25+
$head = 'foo';
26+
$branch = 'feature/gitinfo';
27+
$committedAt = time();
28+
29+
$info = new GitInfo(
30+
$head,
31+
$branch,
32+
$committedAt
33+
);
34+
35+
$this->assertSame($head, $info->head());
36+
$this->assertSame($branch, $info->branch());
37+
$this->assertSame($committedAt, $info->committedAt());
38+
}
39+
40+
public function testToArrayReturnsArrayRepresentation()
41+
{
42+
$head = 'foo';
43+
$branch = 'feature/gitinfo';
44+
$committedAt = time();
45+
46+
$info = new GitInfo(
47+
$head,
48+
$branch,
49+
$committedAt
50+
);
51+
52+
$expected = array(
53+
'head' => $head,
54+
'branch' => $branch,
55+
'committed_at' => $committedAt,
56+
);
57+
58+
$this->assertEquals($expected, $info->toArray());
59+
}
60+
}

0 commit comments

Comments
 (0)