Files
michaelschiemer/tests/Unit/Framework/Attributes/RouteTest.php

112 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Framework\Attributes\Route;
use App\Framework\Core\ValueObjects\ClassName;
use App\Framework\Core\ValueObjects\MethodName;
use App\Framework\Http\Method;
use App\Framework\Router\ValueObjects\RoutePath;
describe('Route', function () {
it('constructs with default values', function () {
$route = new Route(path: '/api/users');
expect($route->path)->toBe('/api/users');
expect($route->method)->toBe(Method::GET);
expect($route->name)->toBeNull();
expect($route->subdomain)->toBe([]);
});
it('constructs with all parameters', function () {
$route = new Route(
path: '/api/users/{id}',
method: Method::POST,
name: 'users.create',
subdomain: 'api'
);
expect($route->path)->toBe('/api/users/{id}');
expect($route->method)->toBe(Method::POST);
expect($route->name)->toBe('users.create');
expect($route->subdomain)->toBe('api');
});
it('accepts RoutePath value object as path', function () {
$routePath = RoutePath::fromString('/api/test');
$route = new Route(path: $routePath);
expect($route->path)->toBe($routePath);
expect($route->getPathAsString())->toBe('/api/test');
});
it('accepts string as path', function () {
$route = new Route(path: '/api/test');
expect($route->path)->toBe('/api/test');
expect($route->getPathAsString())->toBe('/api/test');
});
it('accepts array of subdomains', function () {
$route = new Route(
path: '/api/users',
subdomain: ['api', 'admin']
);
expect($route->subdomain)->toBe(['api', 'admin']);
});
it('returns path as string from string path', function () {
$route = new Route(path: '/api/users');
expect($route->getPathAsString())->toBe('/api/users');
});
it('returns path as string from RoutePath object', function () {
$routePath = RoutePath::fromString('/api/test');
$route = new Route(path: $routePath);
expect($route->getPathAsString())->toBe('/api/test');
});
it('returns RoutePath object from string path', function () {
$route = new Route(path: '/api/users');
$routePath = $route->getRoutePath();
expect($routePath)->toBeInstanceOf(RoutePath::class);
expect($routePath->toString())->toBe('/api/users');
});
it('returns RoutePath object when already RoutePath', function () {
$originalRoutePath = RoutePath::fromString('/api/test');
$route = new Route(path: $originalRoutePath);
$routePath = $route->getRoutePath();
expect($routePath)->toBe($originalRoutePath);
});
it('supports different HTTP methods', function () {
$getMethods = [Method::GET, Method::POST, Method::PUT, Method::DELETE, Method::PATCH];
foreach ($getMethods as $method) {
$route = new Route(path: '/api/test', method: $method);
expect($route->method)->toBe($method);
}
});
it('handles route names', function () {
$route = new Route(
path: '/api/users',
name: 'users.index'
);
expect($route->name)->toBe('users.index');
});
it('handles null route name', function () {
$route = new Route(path: '/api/users');
expect($route->name)->toBeNull();
});
});