Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 9, 2020
1 parent f11ab17 commit 4dd724a
Show file tree
Hide file tree
Showing 8 changed files with 591 additions and 163 deletions.
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/config/routing.yml
Expand Up @@ -4,8 +4,8 @@ services:

Contao\CoreBundle\Routing\Content\ArticleRouteProvider:
arguments:
- '@Contao\CoreBundle\Routing\Page\PageRouteFactory'
- '@Contao\CoreBundle\Routing\RouteFactory'

Contao\CoreBundle\Routing\Content\PageRouteProvider:
arguments:
- '@Contao\CoreBundle\Routing\Page\PageRouteFactory'
- '@Contao\CoreBundle\Routing\RouteFactory'
9 changes: 2 additions & 7 deletions core-bundle/src/Routing/Page/PageRegistry.php
Expand Up @@ -32,14 +32,9 @@ class PageRegistry
*/
private $compositionAware = [];

public function hasRouteConfig(string $type): bool
public function getRouteConfig(string $type): RouteConfig
{
return isset($this->routeConfigs[$type]);
}

public function getRouteConfig(string $type): ?RouteConfig
{
return $this->routeConfigs[$type] ?? null;
return $this->routeConfigs[$type] ?? new RouteConfig();
}

public function enhancePageRoute(PageRoute $route): Route
Expand Down
3 changes: 1 addition & 2 deletions core-bundle/src/Routing/RouteFactory.php
Expand Up @@ -16,7 +16,6 @@
use Contao\CoreBundle\Routing\Content\ContentRouteProviderInterface;
use Contao\CoreBundle\Routing\Page\PageRegistry;
use Contao\CoreBundle\Routing\Page\PageRoute;
use Contao\CoreBundle\Routing\Page\RouteConfig;
use Contao\PageModel;
use Symfony\Component\Routing\Route;

Expand Down Expand Up @@ -51,7 +50,7 @@ public function __construct(PageRegistry $pageRegistry, iterable $routeProviders
*/
public function createRouteForPage(PageModel $pageModel, string $defaultParameters = '', $content = null): Route
{
$config = $this->pageRegistry->getRouteConfig($pageModel->type) ?: new RouteConfig();
$config = $this->pageRegistry->getRouteConfig($pageModel->type);
$pathParameters = $config->getPathParameters();
$defaults = $config->getDefault();
$requirements = $config->getRequirements();
Expand Down
Expand Up @@ -151,7 +151,6 @@
use Symfony\Cmf\Component\Routing\DynamicRouter;
use Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -2823,7 +2822,7 @@ public function testRegistersTheRoutingRouteGenerator(): void

$this->assertEquals(
[
new TaggedIteratorArgument('contao.content_route_provider'),
new Reference(RouteFactory::class),
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
],
$definition->getArguments()
Expand Down
140 changes: 7 additions & 133 deletions core-bundle/tests/Routing/ContentResolvingGeneratorTest.php
Expand Up @@ -13,26 +13,20 @@
namespace Contao\CoreBundle\Tests\Routing;

use Contao\CoreBundle\Exception\ContentRouteNotFoundException;
use Contao\CoreBundle\Routing\Content\ContentRouteProviderInterface;
use Contao\CoreBundle\Routing\ContentResolvingGenerator;
use Contao\CoreBundle\Routing\Page\PageRoute;
use Contao\CoreBundle\Routing\RouteFactory;
use Contao\CoreBundle\Tests\TestCase;
use Contao\PageModel;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Route;

class ContentResolvingGeneratorTest extends TestCase
{
/**
* @var ContentRouteProviderInterface&MockObject
* @var RouteFactory&MockObject
*/
private $provider1;

/**
* @var ContentRouteProviderInterface&MockObject
*/
private $provider2;
private $routeFactory;

/**
* @var ContentResolvingGenerator
Expand All @@ -41,10 +35,9 @@ class ContentResolvingGeneratorTest extends TestCase

protected function setUp(): void
{
$this->provider1 = $this->createMock(ContentRouteProviderInterface::class);
$this->provider2 = $this->createMock(ContentRouteProviderInterface::class);
$this->routeFactory = $this->createMock(RouteFactory::class);

$this->generator = new ContentResolvingGenerator([$this->provider1, $this->provider2]);
$this->generator = new ContentResolvingGenerator($this->routeFactory);
}

public function testThrowsExceptionIfRouteNameIsNotSupported(): void
Expand All @@ -61,113 +54,6 @@ public function testThrowsExceptionIfContentParameterIsNotSet(): void
$this->generator->generate(PageRoute::ROUTE_NAME);
}

public function testGeneratesTheContentObjectWithoutResolvingIfItIsARoute(): void
{
$content = new Route('/foobar');

$url = $this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content]
);

$this->assertSame('/foobar', $url);
}

public function testUsesTheFirstResolverThatSupportsTheContent(): void
{
$content = (object) ['foo' => 'bar'];

$this->provider1
->expects($this->once())
->method('supportsContent')
->with($content)
->willReturn(true)
;

$this->provider1
->expects($this->once())
->method('getRouteForContent')
->with($content)
->willReturn(new Route('/foobar'))
;

$this->provider2
->expects($this->never())
->method($this->anything())
;

$url = $this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content]
);

$this->assertSame('/foobar', $url);
}

public function testIteratesOverTheResolvers(): void
{
$content = (object) ['foo' => 'bar'];

$this->provider1
->expects($this->once())
->method('supportsContent')
->with($content)
->willReturn(false)
;

$this->provider1
->expects($this->never())
->method('getRouteForContent')
;

$this->provider2
->expects($this->once())
->method('supportsContent')
->with($content)
->willReturn(true)
;

$this->provider2
->expects($this->once())
->method('getRouteForContent')
->with($content)
->willReturn(new Route('/foobar'))
;

$url = $this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content]
);

$this->assertSame('/foobar', $url);
}

public function testThrowsExceptionIfNoResolverSupportsTheContent(): void
{
$content = (object) ['foo' => 'bar'];

$this->provider1
->expects($this->once())
->method('supportsContent')
->with($content)
->willReturn(false)
;

$this->provider2
->expects($this->once())
->method('supportsContent')
->with($content)
->willReturn(false)
;

$this->expectException(ContentRouteNotFoundException::class);

$this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content]
);
}

public function testGeneratesTheContentRoute(): void
{
$content = (object) ['foo' => 'bar'];
Expand All @@ -185,25 +71,13 @@ public function testGeneratesTheContentRoute(): void

$route = new PageRoute($page);

$this->provider1
$this->routeFactory
->expects($this->once())
->method('supportsContent')
->with($content)
->willReturn(true)
;

$this->provider1
->expects($this->once())
->method('getRouteForContent')
->method('createRouteForContent')
->with($content)
->willReturn($route)
;

$this->provider2
->expects($this->never())
->method($this->anything())
;

$url = $this->generator->generate(
PageRoute::ROUTE_NAME,
[PageRoute::CONTENT_PARAMETER => $content],
Expand Down

0 comments on commit 4dd724a

Please sign in to comment.