Skip to content

Commit 8ed1ab0

Browse files
Render execution time the proper way
1 parent 400325b commit 8ed1ab0

13 files changed

+167
-57
lines changed

src/ConsoleOutput.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Console\Helper\TableStyle;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616

17-
class ConsoleOutput
17+
final class ConsoleOutput
1818
{
1919
public function __construct(
2020
private readonly OutputInterface $output,
@@ -38,6 +38,11 @@ public function __construct(
3838
);
3939
}
4040

41+
public static function create(): self
42+
{
43+
return new self(new \Symfony\Component\Console\Output\ConsoleOutput());
44+
}
45+
4146
/**
4247
* @param \RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageResult[] $results
4348
*/

src/Subscriber/Application/ApplicationFinishedSubscriber.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRule;
1515
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRules;
1616
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\ResultStatus;
17-
use SebastianBergmann\Timer\Timer;
17+
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\SystemTimer;
18+
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\Timer;
1819
use Symfony\Component\Console\Helper\FormatterHelper;
1920

2021
final class ApplicationFinishedSubscriber extends FormatterHelper implements FinishedSubscriber
@@ -25,13 +26,13 @@ public function __construct(
2526
private readonly bool $cleanUpCloverXml,
2627
private readonly Exitter $exitter,
2728
private readonly ConsoleOutput $consoleOutput,
29+
private readonly Timer $timer,
2830
) {
2931
}
3032

3133
public function notify(Finished $event): void
3234
{
33-
$timer = new Timer();
34-
$timer->start();
35+
$this->timer->start();
3536
/** @var string $reflectionFileName */
3637
$reflectionFileName = (new \ReflectionClass(ClassLoader::class))->getFileName();
3738
$absolutePathToCloverXml = dirname($reflectionFileName, 3).'/'.$this->relativePathToCloverXml;
@@ -51,15 +52,21 @@ public function notify(Finished $event): void
5152
if ($this->minCoverageRules->hasTotalRule() && \XMLReader::ELEMENT == $reader->nodeType && 'metrics' == $reader->name && 2 === $reader->depth) {
5253
/** @var \SimpleXMLElement $node */
5354
$node = simplexml_load_string($reader->readOuterXml());
54-
$metricTotal = CoverageMetric::fromCloverXmlNode($node, MinCoverageRule::TOTAL);
55+
$metricTotal = CoverageMetric::fromCloverXmlNode(
56+
node: $node,
57+
forClass: MinCoverageRule::TOTAL
58+
);
5559
continue;
5660
}
5761
if ($this->minCoverageRules->hasOtherRulesThanTotalRule() && \XMLReader::ELEMENT == $reader->nodeType && 'class' == $reader->name && 3 === $reader->depth) {
5862
/** @var \SimpleXMLElement $node */
5963
$node = simplexml_load_string($reader->readInnerXml());
6064
/** @var string $className */
6165
$className = $reader->getAttribute('name');
62-
$metrics[] = CoverageMetric::fromCloverXmlNode($node, $className);
66+
$metrics[] = CoverageMetric::fromCloverXmlNode(
67+
node: $node,
68+
forClass: $className
69+
);
6370
}
6471
}
6572
$reader->close();
@@ -78,7 +85,10 @@ public function notify(Finished $event): void
7885
metricTotal: $metricTotal,
7986
);
8087

81-
$this->consoleOutput->print($results, $timer->stop());
88+
$this->consoleOutput->print(
89+
results: $results,
90+
duration: $this->timer->stop()
91+
);
8292

8393
$needsExit = !empty(array_filter(
8494
$results,
@@ -138,7 +148,8 @@ public static function fromConfigurationAndParameters(
138148
minCoverageRules: $rules,
139149
cleanUpCloverXml: $cleanUpCloverXml,
140150
exitter: new Exitter(),
141-
consoleOutput: new ConsoleOutput(new \Symfony\Component\Console\Output\ConsoleOutput()),
151+
consoleOutput: ConsoleOutput::create(),
152+
timer: SystemTimer::create(),
142153
);
143154
}
144155
}

src/Timer/SystemTimer.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace RobinIngelbrecht\PHPUnitCoverageTools\Timer;
4+
5+
use SebastianBergmann\Timer\Duration;
6+
use SebastianBergmann\Timer\Timer as PhpUnitTimer;
7+
8+
final class SystemTimer implements Timer
9+
{
10+
private function __construct(
11+
private readonly PhpUnitTimer $timer,
12+
) {
13+
}
14+
15+
public function start(): void
16+
{
17+
$this->timer->start();
18+
}
19+
20+
public function stop(): Duration
21+
{
22+
return $this->timer->stop();
23+
}
24+
25+
public static function create(): self
26+
{
27+
return new self(new PhpUnitTimer());
28+
}
29+
}

src/Timer/Timer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace RobinIngelbrecht\PHPUnitCoverageTools\Timer;
4+
5+
use SebastianBergmann\Timer\Duration;
6+
7+
interface Timer
8+
{
9+
public function start(): void;
10+
11+
public function stop(): Duration;
12+
}

tests/PausedTimer.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\Timer;
6+
use SebastianBergmann\Timer\Duration;
7+
8+
final class PausedTimer implements Timer
9+
{
10+
private function __construct(
11+
private readonly Duration $duration
12+
) {
13+
}
14+
15+
public static function withDuration(Duration $duration): self
16+
{
17+
return new self($duration);
18+
}
19+
20+
public function start(): void
21+
{
22+
}
23+
24+
public function stop(): Duration
25+
{
26+
return $this->duration;
27+
}
28+
}

0 commit comments

Comments
 (0)