From 30f84a5023997f8dc743e4bc3c0eac28e0bb74d4 Mon Sep 17 00:00:00 2001 From: Funky Waddle Date: Fri, 13 Feb 2026 17:26:31 -0600 Subject: [PATCH] refactor: restructure into proper directory hierarchy - Move Config class to src/Config/Config.php - Move exception classes to src/Exception/ - Move Router components to src/Router/ - Update test files with proper namespace references --- composer.json | 1 + src/{ => Config}/Config.php | 2 +- .../MissingConfigurationException.php | 2 +- .../NotFoundRouteException.php | 2 +- .../RouteNotFoundException.php | 2 +- .../RouteValidationException.php | 2 +- src/{ => Router}/Route.php | 2 +- src/{ => Router}/RouteDefinition.php | 2 +- src/{ => Router}/RouteGroup.php | 2 +- src/{ => Router}/Router.php | 7 ++-- tests/Unit/ErrorHandlingTest.php | 42 +++++++++---------- tests/Unit/RouteMatcherTest.php | 37 ++++++++-------- tests/Unit/RouterBasicTest.php | 30 ++++++------- 13 files changed, 67 insertions(+), 66 deletions(-) rename src/{ => Config}/Config.php (98%) rename src/{ => Exception}/MissingConfigurationException.php (72%) rename src/{ => Exception}/NotFoundRouteException.php (69%) rename src/{ => Exception}/RouteNotFoundException.php (69%) rename src/{ => Exception}/RouteValidationException.php (70%) rename src/{ => Router}/Route.php (95%) rename src/{ => Router}/RouteDefinition.php (98%) rename src/{ => Router}/RouteGroup.php (98%) rename src/{ => Router}/Router.php (98%) diff --git a/composer.json b/composer.json index 9837585..2ff367a 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ }, "autoload-dev": { "psr-4": { + "Atlas\\": "src/", "Atlas\\Tests\\": "tests/" } }, diff --git a/src/Config.php b/src/Config/Config.php similarity index 98% rename from src/Config.php rename to src/Config/Config.php index c54d01d..0ae4281 100644 --- a/src/Config.php +++ b/src/Config/Config.php @@ -1,6 +1,6 @@ ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $uri = $this->createMock(UriInterface::class); $uri->method('getPath')->willReturn('/nonexistent'); @@ -27,18 +27,18 @@ final class ErrorHandlingTest extends TestCase $request->method('getMethod')->willReturn('GET'); $request->method('getUri')->willReturn($uri); - $this->expectException(\Atlas\NotFoundRouteException::class); + $this->expectException(\Atlas\Exception\NotFoundRouteException::class); $router->matchOrFail($request); } public function testMatchReturnsNullWhenNoRouteFound(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $uri = $this->createMock(UriInterface::class); $uri->method('getPath')->willReturn('/nonexistent'); @@ -57,40 +57,38 @@ final class ErrorHandlingTest extends TestCase public function testRouteChainingWithDifferentHttpMethods(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $result = new \Atlas\Router\Router($config)->get('/test', 'GetHandler')->post('/test', 'PostHandler'); - $result = $router->get('/test', 'GetHandler')->post('/test', 'PostHandler'); - - $this->assertTrue($result instanceof Router); - $this->assertCount(2, $router->getRoutes()); + $this->assertTrue($result instanceof \Atlas\Router\Router); + $this->assertCount(2, $result->getRoutes()); } public function testMatchUsingRouteDefinition(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'TestMethod'); $routes = $router->getRoutes(); $this->assertCount(1, $routes); - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $routes[0]); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $routes[0]); } public function testCaseInsensitiveHttpMethodMatching(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'TestHandler'); @@ -111,11 +109,11 @@ final class ErrorHandlingTest extends TestCase public function testPathNormalizationLeadingSlashes(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'TestHandler'); @@ -136,11 +134,11 @@ final class ErrorHandlingTest extends TestCase public function testMatchOrFailThrowsExceptionForMultipleRoutes(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'TestHandler'); @@ -156,6 +154,6 @@ final class ErrorHandlingTest extends TestCase $matchedRoute = $router->matchOrFail($request); - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $matchedRoute); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $matchedRoute); } } \ No newline at end of file diff --git a/tests/Unit/RouteMatcherTest.php b/tests/Unit/RouteMatcherTest.php index 3197870..5a3942c 100644 --- a/tests/Unit/RouteMatcherTest.php +++ b/tests/Unit/RouteMatcherTest.php @@ -2,7 +2,7 @@ namespace Atlas\Tests\Unit; -use Atlas\Router; +use Atlas\Router\Router; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; @@ -11,11 +11,11 @@ final class RouteMatcherTest extends TestCase { public function testReturnsRouteOnSuccessfulMatch(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/hello', 'HelloWorldHandler'); @@ -31,18 +31,18 @@ final class RouteMatcherTest extends TestCase $matchedRoute = $router->match($request); - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $matchedRoute); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $matchedRoute); $this->assertSame('/hello', $matchedRoute->getPath()); $this->assertSame('HelloWorldHandler', $matchedRoute->getHandler()); } public function testReturnsNullOnNoMatch(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'Handler'); @@ -61,13 +61,13 @@ final class RouteMatcherTest extends TestCase $this->assertNull($matchedRoute); } - public function testHttpMethodMatchingCaseInsensitive(): void + public function testCaseInsensitiveHttpMethodMatching(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'Handler'); @@ -88,11 +88,11 @@ final class RouteMatcherTest extends TestCase public function testRouteCollectionIteratesCorrectly(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/route1', 'Handler1'); $router->post('/route2', 'Handler2'); @@ -102,31 +102,32 @@ final class RouteMatcherTest extends TestCase $routeArray = iterator_to_array($routes); $this->assertCount(2, $routeArray); - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $routeArray[0]); - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $routeArray[1]); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $routeArray[0]); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $routeArray[1]); } public function testUrlGenerationWithNamedRoute(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/users', 'UserListHandler', 'user_list'); $url = $router->url('user_list'); + $this->assertSame('/users', $url); } public function testHttpMethodsReturnSameInstanceForChaining(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $methodsResult = $router ->get('/get', 'Handler') @@ -135,6 +136,6 @@ final class RouteMatcherTest extends TestCase ->patch('/patch', 'Handler') ->delete('/delete', 'Handler'); - $this->assertTrue($methodsResult instanceof Router); + $this->assertTrue($methodsResult instanceof \Atlas\Router\Router); } } \ No newline at end of file diff --git a/tests/Unit/RouterBasicTest.php b/tests/Unit/RouterBasicTest.php index d601db1..89ada9b 100644 --- a/tests/Unit/RouterBasicTest.php +++ b/tests/Unit/RouterBasicTest.php @@ -2,30 +2,30 @@ namespace Atlas\Tests\Unit; -use Atlas\Router; +use Atlas\Router\Router; use PHPUnit\Framework\TestCase; class RouterBasicTest extends TestCase { public function testRouterCanBeCreatedWithValidConfig(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'], 'routes_file' => 'routes.php' ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); - $this->assertInstanceOf(Router::class, $router); + $this->assertInstanceOf(\Atlas\Router\Router::class, $router); } public function testRouterCanCreateSimpleRoute(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/hello', function() { return 'Hello World'; @@ -36,31 +36,31 @@ class RouterBasicTest extends TestCase public function testRouterReturnsSameInstanceForChaining(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $result = $router->get('/get', 'handler')->post('/post', 'handler'); - $this->assertTrue($result instanceof Router); + $this->assertTrue($result instanceof \Atlas\Router\Router); } public function testRouteHasCorrectProperties(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/test', 'test_handler', 'test_route'); $routes = $router->getRoutes(); $route = $routes[0] ?? null; - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $route); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $route); $this->assertSame('GET', $route->getMethod()); $this->assertSame('/test', $route->getPath()); $this->assertSame('test_handler', $route->getHandler()); @@ -72,18 +72,18 @@ class RouterBasicTest extends TestCase public function testRouteNormalizesPath(): void { - $config = new \Atlas\Config([ + $config = new \Atlas\Tests\Config\Config([ 'modules_path' => ['/path/to/modules'] ]); - $router = new Router($config); + $router = new \Atlas\Router\Router($config); $router->get('/api/test', 'handler'); $routes = $router->getRoutes(); $route = $routes[0] ?? null; - $this->assertInstanceOf(\Atlas\RouteDefinition::class, $route); + $this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $route); $this->assertSame('/api/test', $route->getPath()); } } \ No newline at end of file