Skip to content

Commit d7cd045

Browse files
committed
Detect if an activity lacks the start and end keys
1 parent 360defc commit d7cd045

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎src/Debugger.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Framework\Helpers\Isolation;
1313
use InvalidArgumentException;
1414
use JetBrains\PhpStorm\ArrayShape;
15+
use LogicException;
1516

1617
/**
1718
* Class Debugger.
@@ -181,12 +182,18 @@ public function getActivities() : array
181182
$min = .0;
182183
$max = .0;
183184
if ($collected) {
185+
foreach($collected as $value) {
186+
if (!isset($value['start'])) {
187+
throw new LogicException('An activity does not have the "start" key');
188+
}
189+
if (!isset($value['end'])) {
190+
throw new LogicException('An activity does not have the "end" key.');
191+
}
192+
}
184193
\usort($collected, static function ($c1, $c2) {
185194
return $c1['start'] <=> $c2['start'];
186195
});
187-
// @phpstan-ignore-next-line - Allowed to fail because activities must have the "start" key!
188196
$min = \min(\array_column($collected, 'start'));
189-
// @phpstan-ignore-next-line - Allowed to fail because activities must have the "end" key!
190197
$max = \max(\array_column($collected, 'end'));
191198
foreach ($collected as &$activity) {
192199
$this->addActivityValues($activity, $min, $max);

‎tests/DebuggerTest.php‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Framework\Debug\Collection;
1313
use Framework\Debug\Debugger;
14+
use LogicException;
1415
use PHPUnit\Framework\TestCase;
1516

1617
final class DebuggerTest extends TestCase
@@ -216,6 +217,39 @@ public function testActivitiesDivisionByZero() : void
216217
self::assertStringContainsString('1 activity', $debugbar);
217218
}
218219

220+
public function testActivitiesWithoutStartKey() : void
221+
{
222+
$collector = new CollectorMock();
223+
$this->debugger->addCollector($collector, 'Foo');
224+
$collector->activities = [
225+
[
226+
'collector' => 'default',
227+
'class' => 'Class name',
228+
'description' => 'Collected data 1',
229+
],
230+
];
231+
$this->expectException(LogicException::class);
232+
$this->expectExceptionMessage('An activity does not have the "start" key');
233+
$this->debugger->getActivities();
234+
}
235+
236+
public function testActivitiesWithoutEndKey() : void
237+
{
238+
$collector = new CollectorMock();
239+
$this->debugger->addCollector($collector, 'Foo');
240+
$collector->activities = [
241+
[
242+
'collector' => 'default',
243+
'class' => 'Class name',
244+
'description' => 'Collected data 1',
245+
'start' => \microtime(),
246+
],
247+
];
248+
$this->expectException(LogicException::class);
249+
$this->expectExceptionMessage('An activity does not have the "end" key');
250+
$this->debugger->getActivities();
251+
}
252+
219253
public function testOptions() : void
220254
{
221255
self::assertEmpty($this->debugger->getOptions());
@@ -269,7 +303,7 @@ public function testInvalidIconPathOption() : void
269303
$this->debugger->setOption('icon_path', $iconPath);
270304
try {
271305
$this->debugger->renderDebugbar();
272-
} catch (\LogicException $e) {
306+
} catch (LogicException $e) {
273307
self::assertSame(
274308
'Icon not found: ' . $iconPath,
275309
$e->getMessage()

0 commit comments

Comments
 (0)