Skip to content

Commit

Permalink
[TASK] Get rid of Utility::getNamespacedClassNameForType()
Browse files Browse the repository at this point in the history
Related: #38
  • Loading branch information
brotkrueml committed Mar 21, 2020
1 parent 2a8a863 commit 33cb1e2
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 226 deletions.
21 changes: 13 additions & 8 deletions Classes/Aspect/BreadcrumbListAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Brotkrueml\Schema\Core\Model\AbstractType;
use Brotkrueml\Schema\Manager\SchemaManager;
use Brotkrueml\Schema\Model\Type;
use Brotkrueml\Schema\Utility\Utility;
use Brotkrueml\Schema\Provider\TypesProvider;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand All @@ -27,17 +27,22 @@ final class BreadcrumbListAspect implements AspectInterface
/** @var ExtensionConfiguration */
private $configuration;

/** @var object|ContentObjectRenderer */
/** @var ContentObjectRenderer */
private $contentObjectRenderer;

/** @var TypesProvider */
private $typesProvider;

public function __construct(
TypoScriptFrontendController $controller = null,
ExtensionConfiguration $configuration = null,
ContentObjectRenderer $contentObjectRenderer = null
ContentObjectRenderer $contentObjectRenderer = null,
TypesProvider $typesProvider = null
) {
$this->controller = $controller ?: $GLOBALS['TSFE'];
$this->configuration = $configuration ?: GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->contentObjectRenderer = $contentObjectRenderer ?: GeneralUtility::makeInstance(ContentObjectRenderer::class);
$this->controller = $controller ?? $GLOBALS['TSFE'];
$this->configuration = $configuration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->contentObjectRenderer = $contentObjectRenderer ?? GeneralUtility::makeInstance(ContentObjectRenderer::class);
$this->typesProvider = $typesProvider ?? new TypesProvider();
}

public function execute(SchemaManager $schemaManager): void
Expand Down Expand Up @@ -77,8 +82,8 @@ private function buildBreadCrumbList(array $rootLine): Type\BreadcrumbList
{
$breadcrumbList = (new Type\BreadcrumbList());
foreach ($rootLine as $index => $page) {
$givenItemTypeClass = Utility::getNamespacedClassNameForType($page['tx_schema_webpagetype']);
$webPageTypeClass = $givenItemTypeClass ?: Type\WebPage::class;
$givenItemTypeClass = $this->typesProvider->resolveTypeToModel($page['tx_schema_webpagetype']);
$webPageTypeClass = $givenItemTypeClass ?? Type\WebPage::class;

/** @var AbstractType $itemType */
$itemType = new $webPageTypeClass();
Expand Down
17 changes: 11 additions & 6 deletions Classes/Aspect/WebPageAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Brotkrueml\Schema\Core\Model\AbstractType;
use Brotkrueml\Schema\Manager\SchemaManager;
use Brotkrueml\Schema\Utility\Utility;
use Brotkrueml\Schema\Provider\TypesProvider;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand All @@ -27,12 +27,17 @@ final class WebPageAspect implements AspectInterface
/** @var ExtensionConfiguration */
private $configuration;

/** @var TypesProvider */
private $typesProvider;

public function __construct(
TypoScriptFrontendController $controller = null,
ExtensionConfiguration $configuration = null
ExtensionConfiguration $configuration = null,
TypesProvider $typesProvider = null
) {
$this->controller = $controller ?: $GLOBALS['TSFE'];
$this->configuration = $configuration ?: GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->controller = $controller ?? $GLOBALS['TSFE'];
$this->configuration = $configuration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->typesProvider = $typesProvider ?? new TypesProvider();
}

public function execute(SchemaManager $schemaManager): void
Expand All @@ -50,10 +55,10 @@ public function execute(SchemaManager $schemaManager): void

$type = $this->controller->page['tx_schema_webpagetype'] ?: static::DEFAULT_WEBPAGE_TYPE;

$webPageClass = Utility::getNamespacedClassNameForType($type);
$webPageClass = $this->typesProvider->resolveTypeToModel($type);
if ($webPageClass) {
/** @var AbstractType $webPage */
$webPage = GeneralUtility::makeInstance($webPageClass);
$webPage = new $webPageClass();

if ($this->controller->page['endtime']) {
$webPage->setProperty('expires', \date('c', $this->controller->page['endtime']));
Expand Down
16 changes: 16 additions & 0 deletions Classes/Provider/TypesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,20 @@ public function getContentTypes(): array
)
);
}

/**
* @internal Only for internal use, not a public API!
*/
public function resolveTypeToModel(string $type): ?string
{
if (empty(static::$types)) {
$this->getTypesWithModels();
}

if (\array_key_exists($type, static::$types)) {
return static::$types[$type];
}

return null;
}
}
52 changes: 0 additions & 52 deletions Classes/Utility/Utility.php

This file was deleted.

4 changes: 2 additions & 2 deletions Classes/ViewHelpers/BreadcrumbViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Brotkrueml\Schema\Model\Type\BreadcrumbList;
use Brotkrueml\Schema\Model\Type\ListItem;
use Brotkrueml\Schema\Model\Type\WebPage;
use Brotkrueml\Schema\Utility\Utility;
use Brotkrueml\Schema\Provider\TypesProvider;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper;
Expand Down Expand Up @@ -101,7 +101,7 @@ public static function renderStatic(
for ($i = 0; $i < count($arguments[static::ARGUMENT_BREADCRUMB]); $i++) {
$webPageTypeClass = WebPage::class;
if (static::hasWebPageType($arguments[static::ARGUMENT_BREADCRUMB][$i])) {
$givenItemTypeClass = Utility::getNamespacedClassNameForType($arguments[static::ARGUMENT_BREADCRUMB][$i]['data']['tx_schema_webpagetype']);
$givenItemTypeClass = (new TypesProvider())->resolveTypeToModel($arguments[static::ARGUMENT_BREADCRUMB][$i]['data']['tx_schema_webpagetype']);
$webPageTypeClass = $givenItemTypeClass ?: $webPageTypeClass;
}

Expand Down
110 changes: 78 additions & 32 deletions Tests/Unit/Aspect/BreadcrumbListAspectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

use Brotkrueml\Schema\Aspect\BreadcrumbListAspect;
use Brotkrueml\Schema\Manager\SchemaManager;
use Brotkrueml\Schema\Provider\TypesProvider;
use Brotkrueml\Schema\Tests\Fixtures\Model\Type\ItemPage;
use Brotkrueml\Schema\Tests\Helper\SchemaCacheTrait;
use Brotkrueml\Schema\Tests\Unit\Helper\TypeFixtureNamespace;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
Expand All @@ -20,7 +22,6 @@
class BreadcrumbListAspectTest extends UnitTestCase
{
use SchemaCacheTrait;
use TypeFixtureNamespace;

protected $resetSingletonInstances = true;

Expand All @@ -36,6 +37,9 @@ class BreadcrumbListAspectTest extends UnitTestCase
/** @var MockObject|ContentObjectRenderer */
protected $contentObjectRendererMock;

/** @var Stub|TypesProvider */
private $typesProviderStub;

protected function setUp(): void
{
$this->defineCacheStubsWhichReturnEmptyEntry();
Expand Down Expand Up @@ -70,7 +74,8 @@ public function automaticBreadcrumbListGenerationIsDeactivated(): void
(new BreadcrumbListAspect(
$this->controllerMock,
$configurationMock,
$this->contentObjectRendererMock
$this->contentObjectRendererMock,
$this->typesProviderStub
))
->execute($schemaManagerMock);
}
Expand All @@ -79,6 +84,7 @@ protected function setUpGeneralMocks(): void
{
$this->controllerMock = $this->createMock(TypoScriptFrontendController::class);
$this->contentObjectRendererMock = $this->createMock(ContentObjectRenderer::class);
$this->typesProviderStub = $this->createStub(TypesProvider::class);
}

/**
Expand All @@ -99,7 +105,8 @@ public function withActivatedConfigurationOptionAndEmptyRootlineNoMarkupIsGenera
(new BreadcrumbListAspect(
$this->controllerMock,
$this->getExtensionConfigurationMockWithGetReturnsTrue(),
$this->contentObjectRendererMock
$this->contentObjectRendererMock,
$this->typesProviderStub
))
->execute($schemaManagerMock);
}
Expand All @@ -121,30 +128,6 @@ private function getExtensionConfigurationMockWithGetReturnsTrue()

public function rootLineProvider(): iterable
{
yield 'Rootline with web page type set' => [
[
2 => [
'uid' => 2,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'A page',
'nav_title' => '',
'nav_hide' => '0',
'is_siteroot' => '0',
'tx_schema_webpagetype' => 'ItemPage',
],
1 => [
'uid' => 1,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Site root page',
'nav_title' => '',
'nav_hide' => '0',
'is_siteroot' => '1',
'tx_schema_webpagetype' => '',
],
],
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":{"@type":"ListItem","item":{"@type":"ItemPage","@id":"https://example.org/the-page/"},"name":"A page","position":"1"}}</script>',
];

yield 'Rootline with nav_title set' => [
[
2 => [
Expand Down Expand Up @@ -329,7 +312,8 @@ public function breadCrumbIsGeneratedCorrectly(array $rootLine, string $expected
$subject = new BreadcrumbListAspect(
$this->controllerMock,
$this->getExtensionConfigurationMockWithGetReturnsTrue(),
$this->contentObjectRendererMock
$this->contentObjectRendererMock,
$this->typesProviderStub
);

$subject->execute($schemaManager);
Expand Down Expand Up @@ -380,7 +364,7 @@ public function breadCrumbIsSortedCorrectly(): void
])
->willReturn('https://example.org/level-4/');

$this->controllerMock->rootLine = [
$this->controllerMock->rootLine = [
[
'uid' => 111,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
Expand Down Expand Up @@ -433,11 +417,73 @@ public function breadCrumbIsSortedCorrectly(): void
$subject = new BreadcrumbListAspect(
$this->controllerMock,
$this->getExtensionConfigurationMockWithGetReturnsTrue(),
$this->contentObjectRendererMock
$this->contentObjectRendererMock,
$this->typesProviderStub
);

$subject->execute($schemaManager);

self::assertSame(
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-1/"},"name":"Level 1","position":"1"},{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-2/"},"name":"Level 2","position":"2"},{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-3/"},"name":"Level 3","position":"3"},{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-4/"},"name":"Level 4","position":"4"}]}</script>',
$schemaManager->renderJsonLd()
);
}

/**
* @test
*/
public function rootlineWithDifferentWebPageTypeSet(): void
{
$this->setUpGeneralMocks();

$this->typesProviderStub
->method('resolveTypeToModel')
->with('ItemPage')
->willReturn(ItemPage::class);

$this->controllerMock->rootLine = [
2 => [
'uid' => 2,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'A page',
'nav_title' => '',
'nav_hide' => '0',
'is_siteroot' => '0',
'tx_schema_webpagetype' => 'ItemPage',
],
1 => [
'uid' => 1,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Site root page',
'nav_title' => '',
'nav_hide' => '0',
'is_siteroot' => '1',
'tx_schema_webpagetype' => '',
],
];

$schemaManager = GeneralUtility::makeInstance(SchemaManager::class);

$this->contentObjectRendererMock
->expects(self::once())
->method('typoLink_URL')
->with([
'parameter' => '2',
'forceAbsoluteUrl' => true,
])
->willReturn('https://example.org/the-page/');

$subject = new BreadcrumbListAspect(
$this->controllerMock,
$this->getExtensionConfigurationMockWithGetReturnsTrue(),
$this->contentObjectRendererMock,
$this->typesProviderStub
);

$subject->execute($schemaManager);

self::assertSame('<script type="application/ld+json">{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-1/"},"name":"Level 1","position":"1"},{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-2/"},"name":"Level 2","position":"2"},{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-3/"},"name":"Level 3","position":"3"},{"@type":"ListItem","item":{"@type":"WebPage","@id":"https://example.org/level-4/"},"name":"Level 4","position":"4"}]}</script>', $schemaManager->renderJsonLd());
$expected = '<script type="application/ld+json">{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":{"@type":"ListItem","item":{"@type":"ItemPage","@id":"https://example.org/the-page/"},"name":"A page","position":"1"}}</script>';

self::assertSame($expected, $schemaManager->renderJsonLd());
}
}
Loading

0 comments on commit 33cb1e2

Please sign in to comment.