Skip to content

Commit ee3da88

Browse files
committed
Modify git vcs for testability and add tests.
1 parent 2be2e9d commit ee3da88

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

src/VCS/AbstractVCS.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract public function getAuthorAndDate($file, $line);
2828
* VCSInterface constructor.
2929
* @param array $entries The uncovered object entries provided by the input.
3030
*/
31-
final public function __construct(array $entries = [])
31+
public function __construct(array $entries = [])
3232
{
3333
$this->setEntries($entries);
3434
}

src/VCS/GitVCS.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ class GitVCS extends AbstractVCS
2222
*/
2323
const DATE = "author-time ";
2424

25+
/**
26+
* The vcs.
27+
* @var Git
28+
*/
29+
protected $git;
30+
31+
/**
32+
* GitVCS constructor.
33+
* @param array $entries The entries.
34+
* @param Git|null $vcs The vcs.
35+
*/
36+
public function __construct(array $entries = [], $vcs = null)
37+
{
38+
$this->git = $vcs ?: new Git();
39+
parent::__construct($entries);
40+
}
41+
2542
/**
2643
* Get the author and edit date.
2744
* @param string $file The file name.
@@ -30,11 +47,10 @@ class GitVCS extends AbstractVCS
3047
*/
3148
public function getAuthorAndDate($file, $line)
3249
{
33-
$git = new Git();
3450
$author = '';
3551
$date = '';
3652

37-
$porcelain = $git->blameBus($file, $line);
53+
$porcelain = $this->git->blameBus($file, $line);
3854
foreach ($porcelain as $shard) {
3955
if (strpos($shard, self::AUTHOR) === 0) {
4056
$author = $this->stripEntity(self::AUTHOR, $shard);

tests/src/VCS/GitVCSTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace ChrisHalbert\PhpBCC\VCS;
4+
5+
use ChrisHalbert\Git\Git;
6+
use ChrisHalbert\PhpBCC\Exceptions\InvalidArrayFormatException;
7+
8+
class GitVCSTest extends \PHPUnit_Framework_TestCase
9+
{
10+
protected $git;
11+
12+
protected $gitVCS;
13+
14+
public function setUp()
15+
{
16+
$this->git = $this->getMockBuilder(Git::class)
17+
->setMethods(['blameBus'])
18+
->getMock();
19+
20+
$this->gitVCS = new GitVCS([], $this->git);
21+
}
22+
23+
public function testGetAuthorAndDate()
24+
{
25+
$this->setValidMocks();
26+
$this->gitVCS->setEntries($this->getEntries());
27+
$this->assertEquals($this->getExpectedEntriesWithHistory(), $this->gitVCS->getEntries());
28+
}
29+
30+
/**
31+
* @dataProvider dataProviderValidationFails
32+
*/
33+
public function testValidationFails($entries, $expectedException, $exceptionMessage)
34+
{
35+
$this->setExpectedException($expectedException, $exceptionMessage);
36+
$this->gitVCS->setEntries($entries);
37+
}
38+
39+
public function dataProviderValidationFails()
40+
{
41+
return [
42+
[
43+
[['1pair' => 1, '2pair' => 2, '3pair' => 3]],
44+
InvalidArrayFormatException::class,
45+
'Each entry should have only 2 key value pairs.'
46+
],
47+
[
48+
[['1pair' => 1, '2pair' => 2]],
49+
InvalidArrayFormatException::class,
50+
'Each entry must have a `file` and a `line` key.'
51+
]
52+
];
53+
}
54+
55+
protected function setValidMocks()
56+
{
57+
$this->git->expects($this->exactly(2))
58+
->method('blameBus')
59+
->will(
60+
$this->onConsecutiveCalls(
61+
[
62+
"a1b2c3d4e5f6",
63+
"author John ",
64+
"author-mail <>",
65+
"author-time 1477980000",
66+
"author-tz -0500",
67+
"committer John ",
68+
"committer-mail <>",
69+
"committer-time 1477980000",
70+
"committer-tz -0500",
71+
"summary Manufacture a test.",
72+
"previous f6e5d4c3d2a1",
73+
"filename src/Foo.php",
74+
],
75+
[
76+
"a1b2c3d4e5f6",
77+
"author Kelly ",
78+
"author-mail <>",
79+
"author-time 1478066400",
80+
"author-tz -0500",
81+
"committer Kelly ",
82+
"committer-mail <>",
83+
"committer-time 1478066400",
84+
"committer-tz -0500",
85+
"summary Carve out instructions.",
86+
"previous f6e5d4c3d2a1",
87+
"filename src/Foo.php",
88+
]
89+
)
90+
);
91+
}
92+
93+
protected function getExpectedEntriesWithHistory()
94+
{
95+
return [
96+
[
97+
'author' => 'John', 'date' => '2016-11-01', 'file' => 'Foo.php', 'line' => 1
98+
],
99+
[
100+
'author' => 'Kelly', 'date' => '2016-11-02', 'file' => 'Bar.php', 'line' => 2
101+
]
102+
];
103+
}
104+
105+
protected function getEntries()
106+
{
107+
return [
108+
[
109+
'file' => 'Foo.php',
110+
'line' => 1
111+
],
112+
[
113+
'file' => 'Bar.php',
114+
'line' => 2
115+
]
116+
];
117+
}
118+
}

0 commit comments

Comments
 (0)