Skip to content

Commit c31555b

Browse files
Merge pull request #97 from TheDragonCode/4.x
Handle cases with no comparisons: throw `NoComparisonsException` for `toAssert` and log info message for `toConsole`.
2 parents aa93921 + 8901d0d commit c31555b

10 files changed

Lines changed: 109 additions & 9 deletions

File tree

src/Benchmark.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace DragonCode\Benchmark;
66

77
use Closure;
8+
use DragonCode\Benchmark\Exceptions\NoComparisonsException;
89
use DragonCode\Benchmark\Services\AssertService;
910
use DragonCode\Benchmark\Services\CallbacksService;
1011
use DragonCode\Benchmark\Services\CollectorService;
@@ -133,22 +134,26 @@ public function toData(): array
133134
return $this->mapResult();
134135
}
135136

136-
public function toConsole(): static
137+
public function toConsole(): void
137138
{
138-
$table = $this->transformer->toTable(
139-
$this->toData()
140-
);
139+
if (! $data = $this->toData()) {
140+
$this->view->line('[INFO] No comparisons were made.');
141141

142-
$this->view->table($table);
142+
return;
143+
}
143144

144-
return $this;
145+
$this->view->table(
146+
$this->transformer->toTable($data)
147+
);
145148
}
146149

147150
public function toAssert(): AssertService
148151
{
149-
return new AssertService(
150-
$this->toData()
151-
);
152+
if (! $data = $this->toData()) {
153+
throw new NoComparisonsException;
154+
}
155+
156+
return new AssertService($data);
152157
}
153158

154159
protected function perform(): void
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\Exceptions;
6+
7+
use RuntimeException;
8+
9+
class NoComparisonsException extends RuntimeException
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct('Method "compare" was not called. No comparisons were made.');
14+
}
15+
}

src/Services/ViewService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function progressBar(): ProgressBarView
2626
return $this->progressBar;
2727
}
2828

29+
public function line(string $text): void
30+
{
31+
$this->line->line($text);
32+
}
33+
2934
public function emptyLine(int $count = 1): void
3035
{
3136
$this->line->newLine($count);

src/View/LineView.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class LineView extends View
88
{
9+
public function line(string $text): void
10+
{
11+
$this->writeLine($text);
12+
}
13+
914
public function newLine(int $count = 1): void
1015
{
1116
for ($i = 0; $i < $count; $i++) {

tests/.pest/snapshots/Unit/Fails/WithoutComparisonsTest/_without_comparisons__→_toConsole.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
5+
[INFO] No comparisons were made.

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0____0_.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
5+
┌───────┬────────────────────┬───────────────────┐
6+
│ # │ 01
7+
├───────┼────────────────────┼───────────────────┤
8+
min16 ms - 202 bytes2 ms - 102 bytes
9+
max113 ms - 209 bytes10 ms - 109 bytes
10+
avg53 ms - 205 bytes6 ms - 105 bytes
11+
total424 ms - 1.61 KB48 ms - 844 bytes
12+
├───────┼────────────────────┼───────────────────┤
13+
order21
14+
└───────┴────────────────────┴───────────────────┘

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2____2_.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
5+
┌───────┬───────────────────────┬──────────────────────┐
6+
│ # │ 01
7+
├───────┼───────────────────────┼──────────────────────┤
8+
min15.68 ms - 202 bytes2.35 ms - 102 bytes
9+
max112.79 ms - 209 bytes9.76 ms - 109 bytes
10+
avg53.03 ms - 205 bytes5.94 ms - 105 bytes
11+
total424.2 ms - 1.61 KB47.54 ms - 844 bytes
12+
├───────┼───────────────────────┼──────────────────────┤
13+
order21
14+
└───────┴───────────────────────┴──────────────────────┘

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5____5_.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
5+
┌───────┬──────────────────────────┬────────────────────────┐
6+
│ # │ 01
7+
├───────┼──────────────────────────┼────────────────────────┤
8+
min15.6789 ms - 202 bytes2.34568 ms - 102 bytes
9+
max112.78901 ms - 209 bytes9.75679 ms - 109 bytes
10+
avg53.02525 ms - 205 bytes5.9429 ms - 105 bytes
11+
total424.20199 ms - 1.61 KB47.5432 ms - 844 bytes
12+
├───────┼──────────────────────────┼────────────────────────┤
13+
order21
14+
└───────┴──────────────────────────┴────────────────────────┘
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\Benchmark\Exceptions\NoComparisonsException;
6+
7+
describe('without comparisons', function () {
8+
test('toData', function () {
9+
$result = benchmark(false)->toData();
10+
11+
expect($result)->toBeEmpty();
12+
});
13+
14+
test('toConsole', function () {
15+
benchmark(false)->toConsole();
16+
17+
expectOutputToMatchSnapshot();
18+
});
19+
20+
test('toAssert', function () {
21+
benchmark(false)->toAssert();
22+
})->throws(NoComparisonsException::class);
23+
});

0 commit comments

Comments
 (0)