Skip to content

Commit f74353a

Browse files
committed
Unit tests for PhpcsDiff and ViolationsMapper. Add ext-gettext to composer, as it is used
1 parent 0385fb4 commit f74353a

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"require": {
55
"php": "^7.3 || ^8.0",
66
"ext-json": "*",
7+
"ext-gettext": "*",
78
"squizlabs/php_codesniffer": "*",
89
"league/climate": "^3.4"
910
},

tests/Mapper/PhpcsViolationsMapperTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@
88

99
class PhpcsViolationsMapperTest extends TestBase
1010
{
11+
private $mappedData1 = [
12+
'file.php' => [
13+
'messages' => [
14+
[
15+
'line' => 100,
16+
'message' => 'This is the message',
17+
'type' => 'ERROR',
18+
]
19+
],
20+
],
21+
];
22+
23+
private $mappedData2 = [
24+
'SomeImportantClass.php' => [
25+
'messages' => [
26+
[
27+
'line' => 230,
28+
'message' => 'This is the message',
29+
'type' => 'ERROR',
30+
],
31+
],
32+
],
33+
'index.php' => [
34+
'messages' => [
35+
[
36+
'line' => 230,
37+
'message' => 'This is the message',
38+
'type' => 'ERROR',
39+
],
40+
[
41+
'line' => 250,
42+
'message' => 'Some other warning',
43+
'type' => 'WARNING',
44+
]
45+
],
46+
],
47+
];
48+
1149
/**
1250
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::__construct
1351
*/
@@ -28,4 +66,60 @@ public function testEmptyMapper(): void
2866

2967
$this->assertEmpty($mappedData);
3068
}
69+
70+
/**
71+
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::__construct
72+
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::map
73+
*/
74+
public function testMapperLineMatch(): void
75+
{
76+
$changedLinesPerFile = [
77+
'file.php' => [
78+
100,
79+
]
80+
];
81+
82+
$mappedData = (new PhpcsViolationsMapper($changedLinesPerFile, ''))->map($this->mappedData1);
83+
84+
self::assertIsArray($mappedData);
85+
self::assertCount(1, $mappedData);
86+
self::assertIsString($mappedData[0]);
87+
self::assertSame($mappedData[0], 'file.php' . PHP_EOL . ' - Line 100 (ERROR) This is the message' . PHP_EOL);
88+
}
89+
90+
/**
91+
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::__construct
92+
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::map
93+
*/
94+
public function testMapperNoLineMatch(): void
95+
{
96+
$changedLinesPerFile = [
97+
'file.php' => [
98+
101,
99+
]
100+
];
101+
102+
$mappedData = (new PhpcsViolationsMapper($changedLinesPerFile, ''))->map($this->mappedData1);
103+
104+
self::assertIsArray($mappedData);
105+
self::assertCount(0, $mappedData);
106+
}
107+
108+
/**
109+
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::__construct
110+
* @covers \PhpcsDiff\Mapper\PhpcsViolationsMapper::map
111+
*/
112+
public function testMapperNoFileMatch(): void
113+
{
114+
$changedLinesPerFile = [
115+
'NonMatchedClass.php' => [
116+
101,
117+
]
118+
];
119+
120+
$mappedData = (new PhpcsViolationsMapper($changedLinesPerFile, ''))->map($this->mappedData2);
121+
122+
self::assertIsArray($mappedData);
123+
self::assertCount(0, $mappedData);
124+
}
31125
}

tests/PhpcsDiffTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace PhpcsDiff\Tests;
4+
5+
use League\CLImate\CLImate;
6+
use PhpcsDiff\PhpcsDiff;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class PhpcsDiffTest extends TestCase
10+
{
11+
/**
12+
* @var CLImate
13+
*/
14+
private $cliMate;
15+
16+
protected function setUp(): void
17+
{
18+
$this->cliMate = $this->createMock(CLImate::class);
19+
}
20+
21+
/**
22+
* @covers \PhpcsDiff\PhpcsDiff::__construct
23+
* @covers \PhpcsDiff\PhpcsDiff::error
24+
* @covers \PhpcsDiff\PhpcsDiff::getExitCode
25+
* @covers \PhpcsDiff\PhpcsDiff::isFlagSet
26+
* @covers \PhpcsDiff\PhpcsDiff::setExitCode
27+
*/
28+
public function testExitCodeBeforeRun()
29+
{
30+
$phpcsDiff = new PhpcsDiff([], $this->cliMate);
31+
32+
self::assertSame(1, $phpcsDiff->getExitCode());
33+
}
34+
35+
/**
36+
* @covers \PhpcsDiff\PhpcsDiff::__construct
37+
* @covers \PhpcsDiff\PhpcsDiff::error
38+
* @covers \PhpcsDiff\PhpcsDiff::getExitCode
39+
* @covers \PhpcsDiff\PhpcsDiff::isFlagSet
40+
* @covers \PhpcsDiff\PhpcsDiff::setExitCode
41+
*/
42+
public function testPhpcsDiffNoCurrent()
43+
{
44+
$phpcsDiff = new PhpcsDiff(['fakeBranch'], $this->cliMate);
45+
46+
self::assertSame(1, $phpcsDiff->getExitCode());
47+
}
48+
49+
/**
50+
* @covers \PhpcsDiff\PhpcsDiff::__construct
51+
* @covers \PhpcsDiff\PhpcsDiff::error
52+
* @covers \PhpcsDiff\PhpcsDiff::getExitCode
53+
* @covers \PhpcsDiff\PhpcsDiff::isFlagSet
54+
* @covers \PhpcsDiff\PhpcsDiff::setExitCode
55+
*/
56+
public function testVerboseNoCurrent()
57+
{
58+
$this->cliMate
59+
->expects(self::exactly(2))
60+
->method('__call')
61+
->withConsecutive(
62+
['comment', ['Running in verbose mode.']],
63+
['error', ['Please provide a <bold>base branch</bold> as the first argument.']]
64+
);
65+
;
66+
67+
$phpcsDiff = new PhpcsDiff(['-v', 'fakeBranch'], $this->cliMate);
68+
69+
self::assertSame(1, $phpcsDiff->getExitCode());
70+
}
71+
}

0 commit comments

Comments
 (0)