Skip to content

Commit bda527f

Browse files
committed
Improved ReporterTest::testOutput test.
Reporter class now doesn't depend on Symfony\Component\Console\Input\InputInterface. The values it depends on must be set using setters instead. In this particular case, setFullPath. Added line property to the json output.
1 parent 330bb7d commit bda527f

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

src/PHPSemVerChecker/Console/Command/CompareCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7979
$report = $analyzer->analyze($registryBefore, $registryAfter);
8080

8181
$reporter = new Reporter($report, $input);
82+
$reporter->setFullPath($input->getOption('full-path'));
8283
$reporter->output($output);
8384

8485
$toJson = $input->getOption('to-json');

src/PHPSemVerChecker/Reporter/JsonReporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function output()
5555
$output['changes'][$context][] = [
5656
'level' => Level::toString($level),
5757
'location' => $operation->getLocation(),
58+
'line' => $operation->getLine(),
5859
'target' => $operation->getTarget(),
5960
'reason' => $operation->getReason(),
6061
'code' => $operation->getCode(),

src/PHPSemVerChecker/Reporter/Reporter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,35 @@
66
use PHPSemVerChecker\Report\Report;
77
use PHPSemVerChecker\SemanticVersioning\Level;
88
use Symfony\Component\Console\Helper\Table;
9-
use Symfony\Component\Console\Input\InputInterface;
109
use Symfony\Component\Console\Output\OutputInterface;
1110

1211
class Reporter {
1312
/**
1413
* @var \PHPSemVerChecker\Report\Report
1514
*/
1615
protected $report;
17-
/**
18-
* @var \Symfony\Component\Console\Input\InputInterface
19-
*/
20-
protected $input;
2116
/**
2217
* @var string
2318
*/
2419
protected $cwd;
20+
/**
21+
* @var bool
22+
*/
23+
protected $fullPath = false;
2524

26-
public function __construct(Report $report, InputInterface $input)
25+
public function __construct(Report $report)
2726
{
2827
$this->report = $report;
29-
$this->input = $input;
3028
$this->cwd = getcwd();
3129
}
3230

31+
public function setFullPath($fullPath)
32+
{
33+
$this->fullPath = $fullPath;
34+
35+
return $this;
36+
}
37+
3338
public function output(OutputInterface $output)
3439
{
3540
$suggestedChange = $this->report->getSuggestedLevel();
@@ -81,7 +86,7 @@ protected function outputTable(OutputInterface $output, Report $report, $context
8186

8287
protected function getLocation(Operation $operation)
8388
{
84-
$isFullPath = $this->input->getOption('full-path');
89+
$isFullPath = $this->fullPath;
8590
if ($isFullPath) {
8691
$location = $operation->getLocation();
8792
} else {

tests/PHPSemVerChecker/Reporter/JsonReporterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ public function testOutput()
4848
$operation = $data['changes']['class'][0];
4949

5050
return $operation['level'] === Level::toString(Level::MAJOR) &&
51+
$operation['line'] === 'test-line' &&
5152
$operation['location'] === 'test-location' &&
5253
$operation['target'] === 'test-target' &&
5354
$operation['reason'] === 'test-reason' &&
5455
$operation['code'] === 'test-code';
5556
}));
5657

5758
$operation->shouldReceive('getLocation')->once()->andReturn('test-location')
59+
->shouldReceive('getLine')->once()->andReturn('test-line')
5860
->shouldReceive('getTarget')->once()->andReturn('test-target')
5961
->shouldReceive('getReason')->once()->andReturn('test-reason')
6062
->shouldReceive('getCode')->once()->andReturn('test-code');

tests/PHPSemVerChecker/Reporter/ReporterTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPSemVerChecker\Test\Reporter;
44

5-
use ArrayIterator;
65
use Mockery as m;
76
use PHPSemVerChecker\Reporter\Reporter;
87
use PHPSemVerChecker\SemanticVersioning\Level;
@@ -13,9 +12,9 @@
1312
class ReporterTest extends TestCase {
1413
public function testOutput()
1514
{
16-
$input = new ArrayInput([]);
1715
$output = new NullOutput();
1816
$report = m::mock('PHPSemVerChecker\Report\Report');
17+
$operation = m::mock('PHPSemVerChecker\Operation\Operation');
1918

2019
$levels = [
2120
Level::PATCH => [],
@@ -24,20 +23,32 @@ public function testOutput()
2423
];
2524

2625
$differences = [
27-
'class' => $levels,
26+
'class' => [
27+
Level::PATCH => [],
28+
Level::MINOR => [],
29+
Level::MAJOR => [
30+
$operation
31+
],
32+
],
2833
'function' => $levels,
2934
'interface' => $levels,
3035
'method' => $levels,
3136
'trait' => $levels,
3237
];
3338

3439
$report->shouldReceive('getSuggestedLevel')->andReturn(Level::MAJOR)
35-
->shouldReceive('getIterator')->andReturn(new ArrayIterator($differences))
36-
->shouldReceive('offsetGet')->andReturn($levels)
37-
->shouldReceive('hasDifferences')->andReturn(true)
40+
->shouldReceive('hasDifferences')->with('class')->andReturn(true)
41+
->shouldReceive('offsetGet')->with('class')->andReturn($differences['class'])
42+
->shouldReceive('hasDifferences')->andReturn(false)
3843
->shouldReceive('getLevelForContext')->andReturn(Level::MAJOR);
3944

40-
$reporter = new Reporter($report, $input);
45+
$operation->shouldReceive('getLocation')->once()->andReturn('test-location')
46+
->shouldReceive('getLine')->once()->andReturn('test-line')
47+
->shouldReceive('getTarget')->once()->andReturn('test-target')
48+
->shouldReceive('getReason')->once()->andReturn('test-reason')
49+
->shouldReceive('getCode')->once()->andReturn('test-code');
50+
51+
$reporter = new Reporter($report);
4152
$reporter->output($output);
4253
}
4354
}

0 commit comments

Comments
 (0)