Skip to content

Commit

Permalink
Update RootController, no longer needs additional interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 20, 2020
1 parent ea8f57b commit 2e373e4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 84 deletions.
36 changes: 1 addition & 35 deletions core-bundle/src/Controller/Page/RootPageController.php
Expand Up @@ -15,27 +15,11 @@
use Contao\CoreBundle\Controller\AbstractController;
use Contao\CoreBundle\Exception\NoActivePageFoundException;
use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\CoreBundle\Routing\Page\CompositionAwareInterface;
use Contao\CoreBundle\Routing\Page\PageRoute;
use Contao\CoreBundle\Routing\Page\PageRouteEnhancerInterface;
use Contao\PageModel;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;

class RootPageController extends AbstractController implements PageRouteEnhancerInterface, CompositionAwareInterface
class RootPageController extends AbstractController
{
/**
* @var Connection
*/
private $connection;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

public function __invoke(PageModel $pageModel): Response
{
if ('root' !== $pageModel->type) {
Expand All @@ -45,24 +29,6 @@ public function __invoke(PageModel $pageModel): Response
return $this->redirectToContent($this->getNextPage((int) $pageModel->id));
}

public function enhancePageRoute(PageRoute $route): Route
{
return $route;
}

public function getUrlSuffixes(): array
{
return $this->connection
->query("SELECT DISTINCT urlSuffix FROM tl_page WHERE type='root'")
->fetchAll(FetchMode::COLUMN)
;
}

public function supportsContentComposition(PageModel $pageModel): bool
{
return false;
}

private function getNextPage(int $rootPageId): PageModel
{
$this->initializeContaoFramework();
Expand Down
2 changes: 0 additions & 2 deletions core-bundle/src/Resources/config/controller.yml
Expand Up @@ -4,8 +4,6 @@ services:
public: true

Contao\CoreBundle\Controller\Page\RootPageController:
arguments:
- '@database_connection'
tags:
- { name: contao.page, contentComposition: false }
- { name: monolog.logger, channel: contao }
4 changes: 3 additions & 1 deletion core-bundle/src/Resources/config/routing.yml
Expand Up @@ -10,7 +10,9 @@ services:
arguments:
- '@Contao\CoreBundle\Routing\RouteFactory'

Contao\CoreBundle\Routing\Page\PageRegistry: ~
Contao\CoreBundle\Routing\Page\PageRegistry:
arguments:
- '@database_connection'

Contao\CoreBundle\Routing\RouteFactory:
arguments:
Expand Down
47 changes: 1 addition & 46 deletions core-bundle/tests/Controller/Page/RootPageControllerTest.php
Expand Up @@ -14,14 +14,9 @@

use Contao\CoreBundle\Controller\Page\RootPageController;
use Contao\CoreBundle\Exception\NoActivePageFoundException;
use Contao\CoreBundle\Routing\Page\CompositionAwareInterface;
use Contao\CoreBundle\Routing\Page\PageRoute;
use Contao\CoreBundle\Routing\Page\PageRouteEnhancerInterface;
use Contao\CoreBundle\Tests\TestCase;
use Contao\PageModel;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Statement;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -34,11 +29,6 @@ class RootPageControllerTest extends TestCase
*/
private $pageModelAdapter;

/**
* @var Connection&MockObject
*/
private $connection;

/**
* @var UrlGeneratorInterface&MockObject
*/
Expand All @@ -55,7 +45,6 @@ protected function setUp(): void
$pageModelAdapter = $this->mockAdapter(['findFirstPublishedByPid']);
$this->pageModelAdapter = $pageModelAdapter;

$this->connection = $this->createMock(Connection::class);
$this->router = $this->createMock(UrlGeneratorInterface::class);

$framework = $this->mockContaoFramework([PageModel::class => $this->pageModelAdapter]);
Expand All @@ -71,16 +60,10 @@ protected function setUp(): void
)
;

$this->controller = new RootPageController($this->connection);
$this->controller = new RootPageController();
$this->controller->setContainer($container);
}

public function testImplementsTheInterfaces(): void
{
$this->assertInstanceOf(PageRouteEnhancerInterface::class, $this->controller);
$this->assertInstanceOf(CompositionAwareInterface::class, $this->controller);
}

public function testThrowsExceptionIfPageTypeIsNotSupported(): void
{
/** @var PageModel&MockObject $page */
Expand Down Expand Up @@ -136,32 +119,4 @@ public function testCreatesRedirectResponseToFirstPage(): void
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('https://www.example.org/en/foobar.html', $response->getTargetUrl());
}

public function testReturnsUrlSuffixesFromDatabase(): void
{
$statement = $this->createMock(Statement::class);
$statement
->expects($this->once())
->method('fetchAll')
->with(FetchMode::COLUMN)
->willReturn(['foo', 'bar'])
;

$this->connection
->expects($this->once())
->method('query')
->with("SELECT DISTINCT urlSuffix FROM tl_page WHERE type='root'")
->willReturn($statement)
;

$this->assertSame(['foo', 'bar'], $this->controller->getUrlSuffixes());
}

public function testDoesNotSupportContentComposition(): void
{
/** @var PageModel&MockObject $page */
$page = $this->mockClassWithProperties(PageModel::class);

$this->assertFalse($this->controller->supportsContentComposition($page));
}
}

0 comments on commit 2e373e4

Please sign in to comment.