Skip to content

Commit ee7fdc8

Browse files
committed
test: Enhance unit tests for command help, form submissions, and performance metrics
- Added tests to verify command help outputs for various commands, ensuring they contain expected strings. - Introduced new form submission tests for `DeleteRecordsByFilterType` and `RecordFiltersType`, validating correct data handling. - Enhanced performance metrics service tests to cover correlation analysis and traffic distribution scenarios. - Improved coverage for route data and event handling tests, ensuring robust functionality across different environments.
1 parent 9767735 commit ee7fdc8

29 files changed

Lines changed: 769 additions & 7 deletions

tests/Unit/Command/CreateRecordsTableCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function testHelpContainsCreateRecordsTable(): void
8181
$this->assertStringContainsString('access records', $help);
8282
$this->assertStringContainsString('--update', $help);
8383
$this->assertStringContainsString('--force', $help);
84+
$this->assertStringContainsString('access records', $help);
85+
$this->assertStringContainsString('--update', $help);
86+
$this->assertStringContainsString('--force', $help);
8487
}
8588

8689
public function testExecuteWhenTableExistsWithoutOptions(): void

tests/Unit/Command/DiagnoseCommandTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,12 @@ public function testCommandHelpContainsConfiguration(): void
188188
$help = $this->command->getHelp();
189189
$this->assertStringContainsString('configuration', $help);
190190
}
191+
192+
public function testCommandDescriptionIsNonEmpty(): void
193+
{
194+
$description = $this->command->getDescription();
195+
196+
$this->assertNotEmpty($description);
197+
$this->assertStringContainsString('Diagnose', $description);
198+
}
191199
}

tests/Unit/Command/RebuildAggregatesCommandTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ public function testCommandEnvOptionHasDefault(): void
176176
$this->assertFalse($option->isValueRequired());
177177
}
178178

179+
public function testCommandBatchSizeOptionHasDefault(): void
180+
{
181+
$definition = $this->command->getDefinition();
182+
$option = $definition->getOption('batch-size');
183+
184+
$this->assertTrue($definition->hasOption('batch-size'));
185+
$this->assertSame('200', $option->getDefault());
186+
}
187+
179188
public function testExecuteWithStageEnvFilter(): void
180189
{
181190
$this->routeDataRepository

tests/Unit/Command/SetRouteMetricsCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public function testCommandHasEnvMemoryAndQueryTimeOptions(): void
4141
$this->assertTrue($definition->hasArgument('route'));
4242
}
4343

44+
public function testCommandHelpContainsMetricsAndRoute(): void
45+
{
46+
$help = $this->command->getHelp();
47+
$this->assertStringContainsString('metrics', strtolower($help));
48+
$this->assertStringContainsString('route', strtolower($help));
49+
}
50+
4451
public function testExecuteWithNewRoute(): void
4552
{
4653
$routeData = new RouteData();

tests/Unit/DBAL/QueryTrackingMiddlewareTest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,17 @@ public function testQueryTrackingConnectionExecWithException(): void
249249
public function testQueryTrackingConnectionPrepareWithException(): void
250250
{
251251
QueryTrackingMiddleware::reset();
252-
252+
253253
$connection = $this->createMock(\Doctrine\DBAL\Driver\Connection::class);
254254
$statement = $this->createMock(\Doctrine\DBAL\Driver\Statement::class);
255-
255+
256256
$connection->method('prepare')->willReturn($statement);
257257
$statement->method('execute')->willThrowException(new \Exception('Database error'));
258-
258+
259259
$trackingConnection = new QueryTrackingConnection($connection);
260-
260+
261261
$preparedStatement = $trackingConnection->prepare('SELECT * FROM users');
262-
262+
263263
try {
264264
$preparedStatement->execute();
265265
$this->fail('Expected exception was not thrown');
@@ -269,4 +269,18 @@ public function testQueryTrackingConnectionPrepareWithException(): void
269269
$this->assertSame(1, QueryTrackingMiddleware::getQueryCount());
270270
}
271271
}
272+
273+
public function testStartQuerySameIdTwiceOverwritesStartTime(): void
274+
{
275+
QueryTrackingMiddleware::reset();
276+
277+
QueryTrackingMiddleware::startQuery('q1');
278+
usleep(10000);
279+
QueryTrackingMiddleware::startQuery('q1');
280+
usleep(2000);
281+
QueryTrackingMiddleware::stopQuery('q1');
282+
283+
$this->assertSame(1, QueryTrackingMiddleware::getQueryCount());
284+
$this->assertGreaterThan(0.0, QueryTrackingMiddleware::getTotalQueryTime());
285+
}
272286
}

tests/Unit/Entity/RouteDataRecordTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ public function testSetResponseTimeWithZero(): void
207207
$this->assertSame(0.0, $this->record->getResponseTime());
208208
}
209209

210+
public function testSetResponseTimeWithNull(): void
211+
{
212+
$this->record->setResponseTime(0.5);
213+
$this->assertSame(0.5, $this->record->getResponseTime());
214+
215+
$this->record->setResponseTime(null);
216+
$this->assertNull($this->record->getResponseTime());
217+
}
218+
210219
public function testRequestIdIsInitiallyNull(): void
211220
{
212221
$this->assertNull($this->record->getRequestId());
@@ -347,6 +356,18 @@ public function testSetStatusCodeWith504(): void
347356
$this->assertSame(504, $this->record->getStatusCode());
348357
}
349358

359+
public function testSetStatusCodeWith501(): void
360+
{
361+
$this->record->setStatusCode(501);
362+
$this->assertSame(501, $this->record->getStatusCode());
363+
}
364+
365+
public function testSetStatusCodeWith400(): void
366+
{
367+
$this->record->setStatusCode(400);
368+
$this->assertSame(400, $this->record->getStatusCode());
369+
}
370+
350371
public function testSetAccessedAtReturnsSelf(): void
351372
{
352373
$date = new \DateTimeImmutable('2024-06-01 10:00:00');
@@ -417,6 +438,15 @@ public function testSetQueryTimeWithNull(): void
417438
$this->assertNull($this->record->getQueryTime());
418439
}
419440

441+
public function testSetTotalQueriesWithNull(): void
442+
{
443+
$this->record->setTotalQueries(10);
444+
$this->assertSame(10, $this->record->getTotalQueries());
445+
446+
$this->record->setTotalQueries(null);
447+
$this->assertNull($this->record->getTotalQueries());
448+
}
449+
420450
public function testSetMemoryUsageWithNull(): void
421451
{
422452
$this->record->setMemoryUsage(1024);

tests/Unit/Entity/RouteDataToStringTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,16 @@ public function testToStringWithEmptyNameIncludesEmptyInParts(): void
179179

180180
$this->assertSame('GET (dev)', $result);
181181
}
182+
183+
public function testToStringWithCONNECTMethod(): void
184+
{
185+
$routeData = new RouteData();
186+
$routeData->setHttpMethod('CONNECT');
187+
$routeData->setName('api_proxy');
188+
$routeData->setEnv('prod');
189+
190+
$result = (string) $routeData;
191+
192+
$this->assertSame('CONNECT api_proxy (prod)', $result);
193+
}
182194
}

tests/Unit/Event/AfterMetricsRecordedEventTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,15 @@ public function testGetRouteDataWithStageEnv(): void
130130
$this->assertSame('api_dashboard', $event->getRouteData()->getName());
131131
$this->assertSame('stage', $event->getRouteData()->getEnv());
132132
}
133+
134+
public function testGetRouteDataWithTestEnv(): void
135+
{
136+
$routeData = new RouteData();
137+
$routeData->setName('api_health')->setEnv('test');
138+
$event = new AfterMetricsRecordedEvent($routeData, false);
139+
140+
$this->assertSame('api_health', $event->getRouteData()->getName());
141+
$this->assertSame('test', $event->getRouteData()->getEnv());
142+
$this->assertFalse($event->isNew());
143+
}
133144
}

tests/Unit/Event/BeforeMetricsRecordedEventTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,12 @@ public function testGetRouteNameAndEnvWithStage(): void
143143
$this->assertSame('api_dashboard', $event->getRouteName());
144144
$this->assertSame('stage', $event->getEnv());
145145
}
146+
147+
public function testGetRouteNameAndEnvWithTest(): void
148+
{
149+
$event = new BeforeMetricsRecordedEvent('api_health', 'test');
150+
151+
$this->assertSame('api_health', $event->getRouteName());
152+
$this->assertSame('test', $event->getEnv());
153+
}
146154
}

tests/Unit/Event/BeforeRecordsClearedEventTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public function testExtendsSymfonyEvent(): void
6161
$this->assertInstanceOf(Event::class, $event);
6262
}
6363

64+
public function testGetEnvWithProdEnvironment(): void
65+
{
66+
$event = new BeforeRecordsClearedEvent('prod');
67+
68+
$this->assertSame('prod', $event->getEnv());
69+
$this->assertFalse($event->isClearingPrevented());
70+
}
71+
6472
public function testGetEnvWithStageEnvironment(): void
6573
{
6674
$event = new BeforeRecordsClearedEvent('stage');

0 commit comments

Comments
 (0)