Skip to content

Commit

Permalink
[TASK] Remove dependency from constant TYPO3_MODE in page renderer hook
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Apr 4, 2020
1 parent c6d02c5 commit b8ef367
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 10 deletions.
25 changes: 25 additions & 0 deletions Classes/Context/Typo3Mode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace Brotkrueml\Schema\Context;

class Typo3Mode
{
/** @var string */
private $mode;

public function __construct()
{
$this->mode = \defined('TYPO3_MODE') ? TYPO3_MODE : '';
}

public function isInBackendMode(): bool
{
return $this->mode === 'BE';
}

public function isInFrontendMode(): bool
{
return $this->mode === 'FE';
}
}
24 changes: 23 additions & 1 deletion Classes/Hooks/PageRenderer/SchemaMarkupInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Brotkrueml\Schema\Hooks\PageRenderer;

use Brotkrueml\Schema\Aspect\AspectInterface;
use Brotkrueml\Schema\Context\Typo3Mode;
use Brotkrueml\Schema\Event\ShouldEmbedMarkupEvent;
use Brotkrueml\Schema\Manager\SchemaManager;
use TYPO3\CMS\Core\Cache\CacheManager;
Expand Down Expand Up @@ -39,6 +40,9 @@ final class SchemaMarkupInjection
/** @var FrontendInterface */
private $cache;

/** @var Typo3Mode */
private $typo3Mode;

public function __construct(
TypoScriptFrontendController $controller = null,
ExtensionConfiguration $extensionConfiguration = null,
Expand Down Expand Up @@ -70,7 +74,7 @@ private function getCache(): ?FrontendInterface

public function execute(?array &$params, PageRenderer &$pageRenderer): void
{
if (TYPO3_MODE !== 'FE') {
if ($this->getTypo3Mode()->isInBackendMode()) {
return;
}

Expand Down Expand Up @@ -167,4 +171,22 @@ private function getRegisteredAspects(): array

return $aspects;
}

private function getTypo3Mode(): Typo3Mode
{
if (!$this->typo3Mode) {
$this->typo3Mode = new Typo3Mode();
}

return $this->typo3Mode;
}

/**
* @param Typo3Mode $typo3Mode
* @internal For testing purposes only!
*/
public function setTypo3Mode(Typo3Mode $typo3Mode): void
{
$this->typo3Mode = $typo3Mode;
}
}
88 changes: 88 additions & 0 deletions Tests/Unit/Context/Typo3ModeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);

namespace Brotkrueml\Schema\Tests\Unit\Context;

use Brotkrueml\Schema\Context\Typo3Mode;
use PHPUnit\Framework\TestCase;

/**
* @runTestsInSeparateProcesses
* @coversDefaultClass \Brotkrueml\Schema\Context\Typo3Mode
*/
class Typo3ModeTest extends TestCase
{
/**
* @test
* @covers ::isInBackendMode
*/
public function isInBackendModeReturnsFalseWhenConstantIsNotDefined(): void
{
$subject = new Typo3Mode();

self::assertFalse($subject->isInBackendMode());
}

/**
* @test
* @covers ::isInBackendMode
*/
public function isInBackendModeReturnsFalseWhenConstantIsSetToFE(): void
{
\define('TYPO3_MODE', 'FE');

$subject = new Typo3Mode();

self::assertFalse($subject->isInBackendMode());
}

/**
* @test
* @covers ::isInBackendMode
*/
public function isInBackendModeReturnsTrueWhenConstantIsSetToBE(): void
{
\define('TYPO3_MODE', 'BE');

$subject = new Typo3Mode();

self::assertTrue($subject->isInBackendMode());
}

/**
* @test
* @covers ::isInFrontendMode
*/
public function isInFrontendModeReturnsFalseWhenConstantIsNotDefined(): void
{
$subject = new Typo3Mode();

self::assertFalse($subject->isInFrontendMode());
}

/**
* @test
* @covers ::isInFrontendMode
*/
public function isInFrontendModeReturnsFalseWhenConstantIsSetToBE(): void
{
\define('TYPO3_MODE', 'BE');

$subject = new Typo3Mode();

self::assertFalse($subject->isInFrontendMode());
}

/**
* @test
* @covers ::isInFrontendMode
*/
public function isInFrontendModeReturnsTrueWhenConstantIsSetToFE(): void
{
\define('TYPO3_MODE', 'FE');

$subject = new Typo3Mode();

self::assertTrue($subject->isInFrontendMode());
}
}
33 changes: 24 additions & 9 deletions Tests/Unit/Hooks/PageRenderer/SchemaMarkupInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

namespace Brotkrueml\Schema\Tests\Unit\Hooks\PageRenderer;

use Brotkrueml\Schema\Context\Typo3Mode;
use Brotkrueml\Schema\Hooks\PageRenderer\SchemaMarkupInjection;
use Brotkrueml\Schema\Manager\SchemaManager;
use Brotkrueml\Schema\Tests\Fixtures\Model\Type\FixtureThing;
use Brotkrueml\Schema\Tests\Helper\SchemaCacheTrait;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
Expand Down Expand Up @@ -66,6 +68,11 @@ class SchemaMarkupInjectionTest extends TestCase
*/
private $signalSlotDispatcherMock;

/**
* @var Stub|Typo3Mode
*/
private $typo3ModeStub;

public static function setUpBeforeClass(): void
{
if (!\defined('TYPO3_version')) {
Expand Down Expand Up @@ -99,6 +106,10 @@ protected function setUp(): void
$this->cacheMock
);

$this->typo3ModeStub = $this->createStub(Typo3Mode::class);

$this->subject->setTypo3Mode($this->typo3ModeStub);

$this->pageRendererMock = $this->createMock(PageRenderer::class);

$this->signalSlotDispatcherMock = $this->createMock(Dispatcher::class);
Expand All @@ -125,7 +136,8 @@ protected function tearDown(): void
*/
public function executeInBackendModeDoesNothing()
{
$this->defineConstants('9.5', 'BE');
$this->defineConstants('9.5');

$schemaManager = GeneralUtility::makeInstance(SchemaManager::class);
$schemaManager->addType((new FixtureThing())->setProperty('name', 'some name'));

Expand All @@ -137,6 +149,10 @@ public function executeInBackendModeDoesNothing()
->expects(self::never())
->method('addFooterData');

$this->typo3ModeStub
->method('isInBackendMode')
->willReturn(true);

$params = [];
$this->subject->execute($params, $this->pageRendererMock);
}
Expand All @@ -146,7 +162,7 @@ public function executeInBackendModeDoesNothing()
*/
public function executeWithoutDefinedMarkupAndNoAspectsDoesNotEmbedAnything()
{
$this->defineConstants('9.5', 'FE');
$this->defineConstants('9.5');
$this->pageRendererMock
->expects(self::never())
->method('addHeaderData');
Expand All @@ -164,7 +180,7 @@ public function executeWithoutDefinedMarkupAndNoAspectsDoesNotEmbedAnything()
*/
public function executeWithMarkupDefinedCallsAddHeaderDataIfShouldEmbeddedIntoHead(): void
{
$this->defineConstants('9.5', 'FE');
$this->defineConstants('9.5');

$schemaManager = GeneralUtility::makeInstance(SchemaManager::class);
$schemaManager->addType((new FixtureThing())->setProperty('name', 'some name'));
Expand Down Expand Up @@ -200,7 +216,7 @@ public function executeWithMarkupDefinedCallsAddHeaderDataIfShouldEmbeddedIntoHe
*/
public function executeWithSchemaCallsAddFooterDataOnceIfShouldEmbeddedIntoBody(): void
{
$this->defineConstants('9.5', 'FE');
$this->defineConstants('9.5');

$schemaManager = GeneralUtility::makeInstance(SchemaManager::class);
$schemaManager->addType((new FixtureThing())->setProperty('name', 'some name'));
Expand Down Expand Up @@ -236,7 +252,7 @@ public function executeWithSchemaCallsAddFooterDataOnceIfShouldEmbeddedIntoBody(
*/
public function seoExtensionIsNotInstalledAddsHeaderData(): void
{
$this->defineConstants('9.5', 'FE');
$this->defineConstants('9.5');

$controllerMock = $this->createMock(TypoScriptFrontendController::class);
$controllerMock->page = ['uid' => 42];
Expand Down Expand Up @@ -271,7 +287,7 @@ public function seoExtensionIsNotInstalledAddsHeaderData(): void
*/
public function whenCacheIDefinedItIsUsedToGetMarkup(): void
{
$this->defineConstants('9.5', 'FE');
$this->defineConstants('9.5');

$cacheMock = $this->createMock(FrontendInterface::class);
$cacheMock
Expand Down Expand Up @@ -300,7 +316,7 @@ public function whenCacheIDefinedItIsUsedToGetMarkup(): void
*/
public function whenCacheIsDefinedItIsUsedToStoreMarkup(): void
{
$this->defineConstants('9.5', 'FE');
$this->defineConstants('9.5');

$schemaManager = GeneralUtility::makeInstance(SchemaManager::class);
$schemaManager->addType((new FixtureThing())->setProperty('name', 'some name'));
Expand Down Expand Up @@ -331,9 +347,8 @@ public function whenCacheIsDefinedItIsUsedToStoreMarkup(): void
$subject->execute($params, $this->pageRendererMock);
}

private function defineConstants(string $version, string $mode): void
private function defineConstants(string $version): void
{
\define('TYPO3_branch', $version);
\define('TYPO3_MODE', $mode);
}
}

0 comments on commit b8ef367

Please sign in to comment.