Skip to content
Permalink
Browse files

ResolverTest

  • Loading branch information
rodolfoberrios committed Mar 24, 2020
1 parent e7c5329 commit ddf63969406bb26b93deedf6c5ec9ccc456143ee
@@ -17,13 +17,13 @@

interface RoutesCacheInterface
{
public function has(string $routeName): bool;
public function has(string $name): bool;

public function get(string $routeName): RouteInterface;
public function get(string $name): RouteInterface;

public function put(RouteInterface $route): void;

public function remove(string $routeName): void;
public function remove(string $name): void;

public function puts(): array;
}
@@ -13,12 +13,11 @@

namespace Chevere\Components\Router;

use Chevere\Components\App\Interfaces\ResolverInterface;
use Chevere\Components\Cache\CacheKey;
use Chevere\Components\Message\Message;
use Chevere\Components\Router\Exceptions\RouteNotFoundException;
use Chevere\Components\Router\Exceptions\RouterException;
use Chevere\Components\Router\Interfaces\ResolverCacheInterface;
use Chevere\Components\Router\Interfaces\ResolverInterface;
use Chevere\Components\Router\Interfaces\RoutedInterface;
use Chevere\Components\Router\Interfaces\RouterRegexInterface;
use OutOfBoundsException;
@@ -33,10 +32,10 @@ final class Resolver implements ResolverInterface

public function __construct(
RouterRegexInterface $routerRegex,
ResolverCacheInterface $resolveCache
ResolverCacheInterface $resolverCache
) {
$this->routerRegex = $routerRegex;
$this->resolverCache = $resolveCache;
$this->resolverCache = $resolverCache;
}

/**
@@ -67,17 +66,18 @@ public function resolve(UriInterface $uri): RoutedInterface
*/
private function resolver(array $matches): RoutedInterface
{
$id = $matches['MARK'];
$idString = (string) $matches['MARK'];
$idInt = (int) $idString;
unset($matches['MARK']);
array_shift($matches);
if (!$this->resolverCache->has($id)) {
if (!$this->resolverCache->has($idInt)) {
throw new OutOfBoundsException(
(new Message('No cache for regex tag id %id%'))
->code('%id%', (string) $id)
->code('%id%', $idString)
->toString()
);
}
$routeResolve = $this->getRouteResolve($id);
$routeResolve = $this->getRouteResolve($idInt);
$name = $routeResolve->name();
$routeWildcards = $routeResolve->routeWildcards();
$arguments = [];
@@ -31,19 +31,19 @@ public function __construct(CacheInterface $cache)
$this->cache = $cache;
}

public function has(string $routeName): bool
public function has(string $name): bool
{
return $this->cache->exists(new CacheKey($routeName));
return $this->cache->exists(new CacheKey($name));
}

public function get(string $routeName): RouteInterface
public function get(string $name): RouteInterface
{
try {
$item = $this->cache->get(new CacheKey($routeName));
$item = $this->cache->get(new CacheKey($name));
} catch (Throwable $e) {
throw new RouteCacheNotFoundException(
(new Message('Cache not found for route %routeName%'))
->strong('%routeName%', $routeName)
->strong('%routeName%', $name)
->toString()
);
}
@@ -60,11 +60,11 @@ public function put(RouteInterface $route): void
);
}

public function remove(string $routeName): void
public function remove(string $name): void
{
$this->cache = $this->cache
->withRemove(
new CacheKey($routeName)
new CacheKey($name)
);
}

@@ -67,6 +67,16 @@ public function getWorkingCache(): CacheInterface
return new Cache($this->getWorkingDir());
}

public function getWrongDir(): DirInterface
{
return $this->getChildDir('wrong/');
}

public function getWrongCache(): CacheInterface
{
return new Cache($this->getWrongDir());
}

public function getCachedDir(): DirInterface
{
return $this->getChildDir('cached/');
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Router\Tests;

use Chevere\Components\Route\Route;
use Chevere\Components\Route\RouteName;
use Chevere\Components\Route\RoutePath;
use Chevere\Components\Router\Exceptions\RouteCacheNotFoundException;
use Chevere\Components\Router\Exceptions\RouteCacheTypeException;
use Chevere\Components\Router\ResolverCache;
use Chevere\Components\Router\RouteResolve;
use PHPUnit\Framework\TestCase;

final class ResolverCacheTest extends TestCase
{
private CacheHelper $cacheHelper;

private array $routes;

public function setUp(): void
{
$this->cacheHelper = new CacheHelper(__DIR__, $this);
$this->routes = [
new Route(new RouteName('route-1'), new RoutePath('/test')),
new Route(new RouteName('route-2'), new RoutePath('/test/{id}')),
new Route(new RouteName('route-3'), new RoutePath('/test/path')),
];
}

public function tearDown(): void
{
$this->cacheHelper->tearDown();
}

public function testEmptyCache(): void
{
$resolverCache = new ResolverCache($this->cacheHelper->getEmptyCache());
/** @var int $id */
$keys = array_keys($this->routes);
foreach ($keys as $id) {
$this->assertFalse($resolverCache->has($id));
}
$this->assertEmpty($resolverCache->puts());
$this->expectException(RouteCacheNotFoundException::class);
$resolverCache->get($keys[0]);
}

public function testWorkingCache(): void
{
$resolverCache = new ResolverCache($this->cacheHelper->getWorkingCache());
/**
* @var int $pos
* @var Route $route
*/
foreach ($this->routes as $pos => $route) {
$routeResolve = new RouteResolve(
$route->name()->toString(),
$route->path()->routeWildcards()
);
$resolverCache->put($pos, $routeResolve);
$this->assertArrayHasKey($pos, $resolverCache->puts());
$this->assertEquals($routeResolve, $resolverCache->get($pos));
$resolverCache->remove($pos);
$this->assertArrayNotHasKey($pos, $resolverCache->puts());
}
}

public function testCachedCache(): void
{
$resolverCache = new ResolverCache($this->cacheHelper->getCachedCache());
/**
* @var int $pos
* @var Route $route
*/
foreach ($this->routes as $pos => $route) {
$this->assertTrue($resolverCache->has($pos));
$routeResolve = new RouteResolve(
$route->name()->toString(),
$route->path()->routeWildcards()
);
$this->assertEquals($routeResolve, $resolverCache->get($pos));
}
}

public function testWrongCachedCache(): void
{
$pos = 0;
$resolverCache = new ResolverCache($this->cacheHelper->getWrongCache());
$this->assertTrue($resolverCache->has($pos));
$this->expectException(RouteCacheTypeException::class);
$resolverCache->get($pos);
}

public function _testGenerateCached(): void
{
$this->expectNotToPerformAssertions();
$resolverCache = new ResolverCache($this->cacheHelper->getCachedCache());
/**
* @var int $pos
* @var Route $route
*/
foreach ($this->routes as $pos => $route) {
$routeResolve = new RouteResolve(
$route->name()->toString(),
$route->path()->routeWildcards()
);
$resolverCache->put($pos, $routeResolve);
}
}
}
@@ -0,0 +1,144 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Router\Tests;

use Chevere\Components\Http\Methods\GetMethod;
use Chevere\Components\Route\Route;
use Chevere\Components\Route\RouteEndpoint;
use Chevere\Components\Route\RouteName;
use Chevere\Components\Route\RoutePath;
use Chevere\Components\Route\RouteWildcard;
use Chevere\Components\Route\RouteWildcardMatch;
use Chevere\Components\Router\Exceptions\RouteNotFoundException;
use Chevere\Components\Router\Exceptions\RouterException;
use Chevere\Components\Router\Interfaces\RouterRegexInterface;
use Chevere\Components\Router\Resolver;
use Chevere\Components\Router\ResolverCache;
use Chevere\Components\Router\Routeable;
use Chevere\Components\Router\Routed;
use Chevere\Components\Router\RouteResolve;
use Chevere\Components\Router\RouterMaker;
use Chevere\TestApp\App\Controllers\TestController;
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\TestCase;

final class ResolverTest extends TestCase
{
private CacheHelper $cacheHelper;

private array $routes;

private array $routesResolves;

private RouterRegexInterface $routerRegex;

public function setUp(): void
{
$this->cacheHelper = new CacheHelper(__DIR__, $this);
$routerMaker = new RouterMaker;
$routeEndpoint = new RouteEndpoint(new GetMethod, new TestController);
$this->routes = [
new Route(new RouteName('route-1'), new RoutePath('/test')),
new Route(
new RouteName('route-2'),
(new RoutePath('/test/{id}'))
->withWildcard(
(new RouteWildcard('id'))
->withMatch(new RouteWildcardMatch('[0-9]+'))
)
),
new Route(new RouteName('route-3'), new RoutePath('/test/path')),
];
$this->routesResolves = [];
/** @var Route $route */
foreach ($this->routes as &$route) {
$route = $route->withAddedEndpoint($routeEndpoint);
$routerMaker = $routerMaker->withAddedRouteable(
new Routeable($route),
'grupian'
);
$this->routesResolves[] = new RouteResolve(
$route->name()->toString(),
$route->path()->routeWildcards()
);
}
$this->routerRegex = $routerMaker->router()->regex();
}

public function tearDown(): void
{
$this->cacheHelper->tearDown();
}

public function testRouteNotFound(): void
{
$resolver = new Resolver(
$this->routerRegex,
new ResolverCache($this->cacheHelper->getEmptyCache())
);
$this->expectException(RouteNotFoundException::class);
$resolver->resolve(new Uri('/404'));
}

public function testUnableToResolve(): void
{
$resolver = new Resolver(
$this->routerRegex,
new ResolverCache($this->cacheHelper->getEmptyCache())
);
$this->expectException(RouterException::class);
$resolver->resolve(new Uri('/test'));
}

public function testResolver(): void
{
$resolver = new Resolver(
$this->routerRegex,
new ResolverCache($this->cacheHelper->getCachedCache())
);
$uris = [
new Uri('/test'),
new Uri('/test/123'),
new Uri('/test/path')
];
$arguments = [
[],
['id' => '123'],
[]
];
/**
* @var int $pos
* @var RouteResolve $routeResolve
*/
foreach ($this->routesResolves as $pos => $routeResolve) {
$this->assertEquals(
new Routed($routeResolve->name(), $arguments[$pos]),
$resolver->resolve($uris[$pos])
);
}
}

public function _testGenerateCached(): void
{
$this->expectNotToPerformAssertions();
$resolverCache = new ResolverCache($this->cacheHelper->getCachedCache());
/**
* @var int $pos
* @var RouteResolve $routeResolve
*/
foreach ($this->routesResolves as $pos => $routeResolve) {
$resolverCache->put($pos, $routeResolve);
}
}
}

0 comments on commit ddf6396

Please sign in to comment.
You can’t perform that action at this time.