Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit 4200024

Browse files
committed
Merge remote-tracking branch 'origin/1.3.2.3-wip'
2 parents 9775fde + 8490c2a commit 4200024

8 files changed

Lines changed: 277 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ ChangeLog
33

44
This is a list of changes from 1.3.1.
55

6+
Version 1.3.2.3
7+
---------------------------------------------------------------
8+
* Add **LcovReporter** from **cloak/lcov-reporter:1.1.4**
9+
610
Version 1.3.2.2
711
---------------------------------------------------------------
812
* Change the output format of **TextReporter**.

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Please add a description to the **composer.json** in the configuration file.
2424

2525
{
2626
"require-dev": {
27-
"cloak/cloak": "1.3.2.2"
27+
"cloak/cloak": "1.3.2.3"
2828
}
2929
}
3030

@@ -98,6 +98,7 @@ Reporter that are supported by default are as follows.
9898

9999
* TextReporter
100100
* ProcessingTimeReporter
101+
* LcovReporter
101102

102103
Usage is as follows.
103104

@@ -119,17 +120,16 @@ Usage is as follows.
119120

120121
#### Result of the output
121122

122-
Start at: 1 July 2014 at 12:00
123+
Code Coverage Started: 1 July 2014 at 12:00
123124

124-
Total code coverage: 96.36%
125+
100.00% (19/19) src/Analyzer.php
126+
100.00% (27/27) src/Reporter/TextReporter.php
127+
85.71% ( 6/ 7) src/Reporter/Reportable.php
128+
81.25% (13/16) src/Configuration.php
129+
58.33% (14/24) src/ConfigurationBuilder.php
125130

126-
src/Analyzer.php ..................................................... 100.00% (19/19)
127-
src/Reporter/TextReporter.php ........................................ 100.00% (27/27)
128-
src/Reporter/Reportable.php .......................................... 85.71% ( 6/ 7)
129-
src/Configuration.php ................................................ 81.25% (13/16)
130-
src/ConfigurationBuilder.php ......................................... 58.33% (14/24)
131-
132-
Finished in 0.27125 seconds
131+
Code Coverage: 96.70%
132+
Code Coverage Finished in 1.44294 seconds
133133

134134
Other documents
135135
------------------------------------------------

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cloak/cloak",
33
"keywords": [ "code coverage", "simple", "text report", "php" ],
44
"description": "cloak is a library that takes a code coverage.",
5-
"version": "1.3.2.2",
5+
"version": "1.3.2.3",
66
"require": {
77
"php": ">=5.4.0",
88
"phpcollection/phpcollection": "0.4.*",
@@ -21,7 +21,6 @@
2121
"danielstjules/pho": "1.0.*",
2222
"jaz303/phake": "0.6.*",
2323
"cloak/coverallskit": "1.3.*",
24-
"cloak/lcov-reporter": "1.1.*",
2524
"mockery/mockery": "0.9.*",
2625
"symfony/yaml": "2.5.*"
2726
},

coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ targets:
1111
- spec/reporter/TextReporterSpec.php
1212
- spec/reporter/CompositeReporterSpec.php
1313
- spec/reporter/ProcessingTimeReporterSpec.php
14+
- spec/reporter/LcovReporterSpec.php
1415
- spec/AnalyzeLifeCycleNotifierSpec.php
1516
- spec/AnalyzerSpec.php
1617
- spec/DriverDetectorSpec.php

spec/fixtures/Example1.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace cloak\example;
4+
5+
class Example1
6+
{
7+
8+
public function sayHello()
9+
{
10+
echo "hello";
11+
}
12+
13+
}

spec/fixtures/Example2.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace cloak\example;
4+
5+
class Example2
6+
{
7+
8+
public function compare($value1, $value2)
9+
{
10+
return $value1 === $value2;
11+
}
12+
13+
public function equal($value1, $value2)
14+
{
15+
return $this->compare($value1, $value2);
16+
}
17+
18+
}

spec/reporter/LcovReporterSpec.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* This file is part of cloak.
5+
*
6+
* (c) Noritaka Horio <holy.shared.design@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
use cloak\Result;
13+
use cloak\result\Line;
14+
use cloak\reporter\LcovReporter;
15+
use \Mockery;
16+
use \DateTime;
17+
18+
describe('LcovReporter', function() {
19+
20+
describe('onStart', function() {
21+
before(function() {
22+
$this->reportFile = __DIR__ . '/../tmp/report.lcov';
23+
24+
$this->reporter = new LcovReporter($this->reportFile);
25+
$this->reporter->onStart($this->startEvent);
26+
27+
$this->startEvent = Mockery::mock('cloak\event\StartEventInterface');
28+
$this->startEvent->shouldReceive('getSendAt')->never();
29+
});
30+
it('check mock object expectations', function() {
31+
Mockery::close();
32+
});
33+
});
34+
35+
describe('onStop', function() {
36+
before(function() {
37+
$this->reportFile = __DIR__ . '/../tmp/report.lcov';
38+
$this->reporter = new LcovReporter($this->reportFile);
39+
40+
$this->source1 = realpath(__DIR__ . '/../fixtures/Example1.php');
41+
$this->source2 = realpath(__DIR__ . '/../fixtures/Example2.php');
42+
43+
$this->result = Result::from([
44+
$this->source1 => [
45+
10 => Line::EXECUTED,
46+
11 => Line::EXECUTED
47+
],
48+
$this->source2 => [
49+
10 => Line::EXECUTED,
50+
15 => Line::UNUSED
51+
]
52+
]);
53+
54+
$this->stopEvent = Mockery::mock('\cloak\event\StopEventInterface');
55+
$this->stopEvent->shouldReceive('getResult')->once()->andReturn($this->result);
56+
57+
$this->reporter->onStop($this->stopEvent);
58+
59+
$output = "";
60+
$output .= "SF:" . $this->source1 . PHP_EOL;
61+
$output .= "DA:10,1" . PHP_EOL;
62+
$output .= "DA:11,1" . PHP_EOL;
63+
$output .= "end_of_record" . PHP_EOL;
64+
65+
$output .= "SF:" . $this->source2 . PHP_EOL;
66+
$output .= "DA:10,1" . PHP_EOL;
67+
$output .= "DA:15,0" . PHP_EOL;
68+
$output .= "end_of_record" . PHP_EOL;
69+
70+
$this->output = $output;
71+
});
72+
after(function() {
73+
unlink($this->reportFile);
74+
});
75+
it('should output lcov report file', function() {
76+
$result = file_get_contents($this->reportFile);
77+
expect($result)->toEqual($this->output);
78+
});
79+
it('check mock object expectations', function() {
80+
Mockery::close();
81+
});
82+
});
83+
84+
});

src/reporter/LcovReporter.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
/**
4+
* This file is part of cloak.
5+
*
6+
* (c) Noritaka Horio <holy.shared.design@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace cloak\reporter;
13+
14+
15+
use cloak\Result;
16+
use cloak\result\File;
17+
use cloak\result\Line;
18+
use cloak\event\StartEventInterface;
19+
use cloak\event\StopEventInterface;
20+
use cloak\writer\FileWriter;
21+
22+
23+
/**
24+
* Class LcovReporter
25+
* @package cloak\reporter
26+
*/
27+
class LcovReporter implements ReporterInterface
28+
{
29+
30+
const SOURCE_FILE_PREFIX = 'SF:';
31+
const COVERAGE_PREFIX = 'DA:';
32+
const END_OF_RECORD = 'end_of_record';
33+
34+
use Reportable;
35+
36+
/**
37+
* @var \cloak\writer\FileWriter
38+
*/
39+
private $reportWriter;
40+
41+
42+
/**
43+
* @param string|null $outputFilePath
44+
* @throws \cloak\writer\DirectoryNotFoundException
45+
* @throws \cloak\writer\DirectoryNotWritableException
46+
*/
47+
public function __construct($outputFilePath)
48+
{
49+
$this->reportWriter = new FileWriter($outputFilePath);
50+
}
51+
52+
/**
53+
* @param \cloak\event\StartEventInterface $event
54+
*/
55+
public function onStart(StartEventInterface $event)
56+
{
57+
}
58+
59+
/**
60+
* @param \cloak\event\StopEventInterface $event
61+
*/
62+
public function onStop(StopEventInterface $event)
63+
{
64+
$result = $event->getResult();
65+
$this->writeResult($result);
66+
}
67+
68+
/**
69+
* @param Result $result
70+
*/
71+
private function writeResult(Result $result)
72+
{
73+
$files = $result->getFiles();
74+
75+
foreach($files as $file) {
76+
$this->writeFileResult($file);
77+
}
78+
}
79+
80+
/**
81+
* @param File $file
82+
*/
83+
private function writeFileResult(File $file)
84+
{
85+
$this->writeFileHeader($file);
86+
87+
$targetLines = $this->getTargetLinesFromFile($file);
88+
89+
foreach ($targetLines as $targetLine) {
90+
$this->writeLineResult($targetLine);
91+
}
92+
93+
$this->writeFileFooter();
94+
}
95+
96+
/**
97+
* @param File $file
98+
*/
99+
private function writeFileHeader(File $file)
100+
{
101+
$parts = [
102+
self::SOURCE_FILE_PREFIX,
103+
$file->getPath()
104+
];
105+
106+
$record = implode('', $parts);
107+
$this->reportWriter->writeLine($record);
108+
}
109+
110+
private function writeFileFooter()
111+
{
112+
$this->reportWriter->writeLine(self::END_OF_RECORD);
113+
}
114+
115+
/**
116+
* @param \cloak\result\Line $line
117+
*/
118+
private function writeLineResult(Line $line)
119+
{
120+
121+
$executedCount = $line->isExecuted() ? 1 : 0;
122+
$recordParts = [
123+
$line->getLineNumber(),
124+
$executedCount
125+
];
126+
127+
$record = self::COVERAGE_PREFIX . implode(',', $recordParts);
128+
$this->reportWriter->writeLine($record);
129+
}
130+
131+
/**
132+
* @param File $file
133+
* @return array
134+
*/
135+
private function getTargetLinesFromFile(File $file)
136+
{
137+
$lineResults = $file->getLineResults();
138+
139+
$results = $lineResults->selectLines(function(Line $line) {
140+
return $line->isExecuted() || $line->isUnused();
141+
})->all();
142+
143+
return $results;
144+
}
145+
146+
}

0 commit comments

Comments
 (0)