Skip to content

Commit

Permalink
route lookup fix
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Nov 8, 2019
1 parent b9701c9 commit 40c49c7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/AppFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

/**
* Custom routing aware AppFactory.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AppFactory extends SlimAppFactory
{
Expand Down
2 changes: 0 additions & 2 deletions src/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

/**
* Metadata aware route.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Route extends SlimRoute
{
Expand Down
12 changes: 12 additions & 0 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public function getRoutes(): array
return $this->routes;
}

/**
* {@inheritdoc}
*/
public function lookupRoute(string $identifier): RouteInterface
{
if ($this->routesRegistered === false) {
$this->registerRoutes();
}

return parent::lookupRoute($identifier);
}

/**
* Register routes.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Routing/AppFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* slim-routing (https://github.com/juliangut/slim-routing).
* Slim framework routing.
*
* @license BSD-3-Clause
* @link https://github.com/juliangut/slim-routing
* @author Julián Gutiérrez <[email protected]>
*/

declare(strict_types=1);

namespace Jgut\Slim\Routing\Tests;

use Jgut\Slim\Routing\AppFactory;
use Jgut\Slim\Routing\Configuration;
use Jgut\Slim\Routing\RouteCollector;
use PHPUnit\Framework\TestCase;

/**
* Custom routing aware AppFactory tests.
*/
class AppFactoryTest extends TestCase
{
public function testCreation(): void
{
AppFactory::setRouteCollectorConfiguration(new Configuration());
$app = AppFactory::create();

static::assertInstanceOf(RouteCollector::class, $app->getRouteCollector());
}
}
52 changes: 52 additions & 0 deletions tests/Routing/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\RouteInterface;

/**
* Route loader collector tests.
Expand Down Expand Up @@ -97,4 +98,55 @@ public function testRoutes(): void

static::assertCount(2, $router->getRoutes());
}

public function testRouteLookup(): void
{
$responseFactory = $this->getMockBuilder(ResponseFactoryInterface::class)
->getMock();
/* @var ResponseFactoryInterface $responseFactory */
$callableResolver = $this->getMockBuilder(CallableResolverInterface::class)
->getMock();
/** @var CallableResolverInterface $callableResolver */
$configuration = $this->getMockBuilder(Configuration::class)
->setMethods(['getSources', 'getRouteResolver'])
->getMock();
$configuration->expects(static::once())
->method('getSources')
->will($this->returnValue([__DIR__ . '/Files/Annotation/Valid']));

$routesMetadata = [
(new RouteMetadata())
->setMethods(['GET'])
->setPattern('one/{id}')
->setPlaceholders(['id' => 'numeric'])
->setInvokable(['one', 'action'])
->setXmlHttpRequest(true),
(new RouteMetadata())
->setMethods(['POST'])
->setPattern('two')
->setName('two')
->setMiddleware(['twoMiddleware'])
->setInvokable(['two', 'action']),
];

$resolver = $this->getMockBuilder(RouteResolver::class)
->setConstructorArgs([$configuration])
->setMethods(['sort'])
->getMock();
$resolver->expects(static::once())
->method('sort')
->will($this->returnValue($routesMetadata));
/* @var RouteResolver $resolver */

$configuration->expects(static::any())
->method('getRouteResolver')
->will($this->returnValue($resolver));
/* @var Configuration $configuration */

$router = new RouteCollector($configuration, $responseFactory, $callableResolver);

$resolvedRoute = $router->lookupRoute('route1');
static::assertInstanceOf(RouteInterface::class, $resolvedRoute);
static::assertEquals('two', $resolvedRoute->getName());
}
}

0 comments on commit 40c49c7

Please sign in to comment.