Skip to content

Commit

Permalink
test(frontend): fix router testing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 6, 2024
1 parent a123cd6 commit fc60022
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/Handler/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ public static function convertQueryParam(
),
)
: $value);
case $typeString === 'float':
return (float) (\is_array($value)
? throw new \InvalidArgumentException(
\sprintf(
'Query parameter `%s` must be an integer, array given.',
$queryName,
),
)
: $value);
case $typeString === 'string':
return (string) (\is_array($value)
? throw new \InvalidArgumentException(
Expand Down
17 changes: 13 additions & 4 deletions src/Module/Frontend/Module/Profiler/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ public function profilerTop(string $uuid, #[QueryParam] string $metric = ''): Me
#[
AssertSuccess(
Method::Get,
'api/profiler/0190402f-7eb2-7287-82a8-897a0091f58e/call-graph',
['uuid' => '0190402f-7eb2-7287-82a8-897a0091f58e'],
'api/profiler/0190402f-7eb2-7287-82a8-897a0091f58e/call-graph?threshold=1.1&percentage=15.1&metric=wt',
[
'uuid' => '0190402f-7eb2-7287-82a8-897a0091f58e',
'threshold' => 1.1,
'percentage' => 15.1,
'metric' => 'wt',
],
),
]
public function profilerCallGraph(string $uuid): Message\CallGraph
{
public function profilerCallGraph(
string $uuid,
#[QueryParam] float $threshold = 1,
#[QueryParam] float $percentage = 15,
#[QueryParam] string $metric = 'wt'
): Message\CallGraph {
$event = $this->eventsStorage->get($uuid) ?? throw new \RuntimeException('Event not found.');

$event?->payload instanceof ProfilerPayload or throw new \RuntimeException('Invalid payload type.');
Expand Down
6 changes: 3 additions & 3 deletions src/Module/Frontend/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function smtpAttachments(string $uuid): Attachments|Success

#[StaticRoute(Method::Delete, 'api/events')]
#[
AssertFail(Method::Delete, '/api/events'),
AssertSuccess(Method::Delete, '/api/events'),
AssertFail(Method::Delete, 'api/events/'),
AssertFail(Method::Delete, 'api/event'),
]
Expand All @@ -117,9 +117,9 @@ public function eventsDelete(array $uuids = []): Success

#[StaticRoute(Method::Get, 'api/events')]
#[
AssertSuccess(Method::Get, '/api/events'),
AssertFail(Method::Get, 'api/event'),
AssertFail(Method::Post, 'api/events'),
AssertFail(Method::Get, '/api/events'),
]
public function eventsList(): EventCollection
{
Expand All @@ -131,7 +131,7 @@ public function eventsList(): EventCollection
#[
AssertFail(Method::Get, 'api/setting'),
AssertFail(Method::Post, 'api/settings'),
AssertFail(Method::Get, '/api/settings'),
AssertSuccess(Method::Get, '/api/settings'),
]
public function settings(): Settings
{
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/Handler/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public static function routedClassesProvider(): iterable
yield 'Frontend Event Assets' => [\Buggregator\Trap\Module\Frontend\Http\EventAssets::class];
}

#[StaticRoute(Method::Get, '/public-static-static-route')]
#[StaticRoute(Method::Get, 'public-static-static-route')]
public static function publicStaticStaticRoute(): string
{
return 'public-static-static-route-result';
}

#[AssertRouteSuccess(Method::Delete, '/item/f00', ['uuid' => 'f00'])]
#[AssertRouteSuccess(Method::Delete, '/item/fzzzzzzzzz', ['uuid' => 'f'])]
#[AssertRouteFail(Method::Get, '/item/f00')]
#[RegexpRoute(Method::Delete, '#^/item/(?<uuid>[a-f0-9-]++)#i')]
#[AssertRouteSuccess(Method::Delete, 'item/f00', ['uuid' => 'f00'])]
#[AssertRouteSuccess(Method::Delete, 'item/fzzzzzzzzz', ['uuid' => 'f'])]
#[AssertRouteFail(Method::Get, 'item/f00')]
#[RegexpRoute(Method::Delete, '#^item/(?<uuid>[a-f0-9-]++)#i')]
public static function publicStaticRegexpRoute(string $uuid): string
{
return $uuid;
Expand Down Expand Up @@ -113,16 +113,16 @@ public function testArgumentsCollision(): void
self::assertSame('123e4567-e89b-12d3-a456-426614174000', $route(uuid: 'no-pasaran'));
}

#[StaticRoute(Method::Get, '/public-static-route')]
#[StaticRoute(Method::Get, 'public-static-route')]
public function publicStaticRoute(): string
{
return 'public-static-route-result';
}

#[AssertRouteSuccess(Method::Get, '/private-route')]
#[AssertRouteSuccess(Method::Get, 'private-route')]
#[AssertRouteFail(Method::Post, '/private-route')]
#[AssertRouteFail(Method::Get, 'private-route')]
#[StaticRoute(Method::Get, '/private-route')]
#[StaticRoute(Method::Get, 'private-route')]
private function privateRoute(): never
{
throw new \LogicException('This method should not be called.');
Expand Down

0 comments on commit fc60022

Please sign in to comment.