From 3b43d5510827288078d26813d896d039cd429d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 17 Apr 2026 11:53:08 +0200 Subject: [PATCH] Support multiple aliases + tests; + Removal of params --- src/Platform/Platform.php | 4 ++-- src/Platform/Scope/HTTP.php | 33 ++++++++++------------------ tests/Platform/TestActionAliased.php | 26 ++++++++++++++++++++++ tests/Platform/TestService.php | 1 + tests/e2e/HTTPServicesTest.php | 20 +++++++++++++++++ 5 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 tests/Platform/TestActionAliased.php diff --git a/src/Platform/Platform.php b/src/Platform/Platform.php index 66e0840..2662713 100644 --- a/src/Platform/Platform.php +++ b/src/Platform/Platform.php @@ -106,8 +106,8 @@ protected function initHttp(array $services): void ->desc($action->getDesc() ?? ''); if ($hook instanceof Route) { - if (! empty($action->getHttpAliasPath())) { - $hook->alias($action->getHttpAliasPath()); + foreach ($action->getHttpAliases() as $alias) { + $hook->alias($alias); } } diff --git a/src/Platform/Scope/HTTP.php b/src/Platform/Scope/HTTP.php index af22c01..73cc0e1 100644 --- a/src/Platform/Scope/HTTP.php +++ b/src/Platform/Scope/HTTP.php @@ -8,9 +8,10 @@ trait HTTP protected ?string $httpPath = null; - protected ?string $httpAliasPath = null; - - protected array $httpAliasParams = []; + /** + * @var array + */ + protected array $httpAliases = []; /** * Set Http path @@ -49,23 +50,13 @@ public function getHttpPath(): string } /** - * Get the value of httpAliasPath - * - * @return string - */ - public function getHttpAliasPath(): ?string - { - return $this->httpAliasPath; - } - - /** - * Get the value of httpAliasParams + * Get the value of httpAliases * - * @return array + * @return array */ - public function getHttpAliasParams(): array + public function getHttpAliases(): array { - return $this->httpAliasParams; + return $this->httpAliases; } /** @@ -79,16 +70,14 @@ public function getHttpMethod(): string } /** - * Set httpAlias path and params + * Append an httpAlias path. Can be called multiple times to register several aliases. * * @param string $path - * @param array $params * @return self */ - public function httpAlias(string $path, array $params = []): self + public function httpAlias(string $path): self { - $this->httpAliasPath = $path; - $this->httpAliasParams = $params; + $this->httpAliases[] = $path; return $this; } diff --git a/tests/Platform/TestActionAliased.php b/tests/Platform/TestActionAliased.php new file mode 100644 index 0000000..cbace7d --- /dev/null +++ b/tests/Platform/TestActionAliased.php @@ -0,0 +1,26 @@ +httpPath = '/aliased'; + $this->httpMethod = 'GET'; + $this->httpAlias('/alias-one'); + $this->httpAlias('/alias-two'); + $this->httpAlias('/alias-three'); + $this->inject('response'); + $this->callback(function ($response) { + $this->action($response); + }); + } + + public function action($response) + { + $response->send('Aliased!'); + } +} diff --git a/tests/Platform/TestService.php b/tests/Platform/TestService.php index 091de5f..52b5ca0 100644 --- a/tests/Platform/TestService.php +++ b/tests/Platform/TestService.php @@ -14,5 +14,6 @@ public function __construct() $this->addAction('redirect', new TestActionRedirect()); $this->addAction('initHook', new TestActionInit()); $this->addAction('withParams', new TestActionWithParams()); + $this->addAction('aliased', new TestActionAliased()); } } diff --git a/tests/e2e/HTTPServicesTest.php b/tests/e2e/HTTPServicesTest.php index 5efa28d..8fb26a4 100644 --- a/tests/e2e/HTTPServicesTest.php +++ b/tests/e2e/HTTPServicesTest.php @@ -107,6 +107,26 @@ public function testHook() $this->assertEquals('', ($response1->getHeaders()['x-init'] ?? '')); } + public function testAliasedAction() + { + $paths = ['/aliased', '/alias-one', '/alias-two', '/alias-three']; + + foreach ($paths as $path) { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = $path; + + $request = new Request(); + $response = new MockResponse(); + + \ob_start(); + $this->http->run($request, $response); + $result = \ob_get_contents(); + \ob_end_clean(); + + $this->assertEquals('Aliased!', $result, "Alias '{$path}' should resolve to the aliased action"); + } + } + public function testActionParamFieldsForwardedToRoute() { $routes = Http::getRoutes();