Skip to content

Commit

Permalink
[BUGFIX] Avoid error in SchemaManager in backend context (#110)
Browse files Browse the repository at this point in the history
Related: #108
  • Loading branch information
brotkrueml authored Feb 24, 2023
1 parent 4dc3bd6 commit 87f17ac
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- Avoid error when SchemaManager is called via view helpers in backend context (#108)

## [2.7.0] - 2023-02-14

### Added
Expand Down
6 changes: 6 additions & 0 deletions Classes/Adapter/ApplicationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Brotkrueml\Schema\Adapter;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\ApplicationType as CoreApplicationType;

/**
Expand All @@ -23,6 +24,11 @@ class ApplicationType

public function isBackend(): bool
{
if (Environment::isCli()) {
// This is helpful in functional tests
return false;
}

if ($this->applicationType === null) {
$this->applicationType = CoreApplicationType::fromRequest($this->getRequest());
}
Expand Down
10 changes: 9 additions & 1 deletion Classes/Manager/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Brotkrueml\Schema\Manager;

use Brotkrueml\Schema\Adapter\ApplicationType;
use Brotkrueml\Schema\Core\Model\TypeInterface;
use Brotkrueml\Schema\Core\Model\WebPageTypeInterface;
use Brotkrueml\Schema\Event\InitialiseTypesEvent;
Expand Down Expand Up @@ -44,15 +45,22 @@ final class SchemaManager implements SingletonInterface
private array $breadcrumbLists = [];

private MainEntityOfWebPageBag $mainEntityOfWebPageBag;
private ApplicationType $applicationType;

public function __construct(
?ApplicationType $applicationType = null,
?EventDispatcherInterface $eventDispatcher = null,
?ExtensionConfiguration $extensionConfiguration = null,
?RendererInterface $renderer = null
) {
$this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->applicationType = $applicationType ?? new ApplicationType();
$this->renderer = $renderer ?? new Renderer();
$this->mainEntityOfWebPageBag = new MainEntityOfWebPageBag();
$this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);

if ($this->applicationType->isBackend()) {
return;
}

$eventDispatcher ??= GeneralUtility::makeInstance(EventDispatcherInterface::class);
/** @var InitialiseTypesEvent $event */
Expand Down
6 changes: 6 additions & 0 deletions Documentation/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`_.
`Unreleased <https://github.com/brotkrueml/schema/compare/v2.7.0...HEAD>`_
------------------------------------------------------------------------------

Fixed
^^^^^


* Avoid error when SchemaManager is called via view helpers in backend context (#108)

`2.7.0 <https://github.com/brotkrueml/schema/compare/v2.6.4...v2.7.0>`_ - 2023-02-14
----------------------------------------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions Tests/Unit/Hooks/PageRenderer/SchemaMarkupInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ final class SchemaMarkupInjectionTest extends TestCase

protected function setUp(): void
{
$applicationTypeStub = $this->createStub(ApplicationType::class);
$applicationTypeStub
->method('isBackend')
->willReturn(false);

$this->controllerMock = $this->createMock(TypoScriptFrontendController::class);
$this->controllerMock->newHash = 'somehash';
$this->controllerMock->page = [
Expand All @@ -83,6 +88,7 @@ protected function setUp(): void

$this->extensionConfigurationMock = $this->createMock(ExtensionConfiguration::class);
$this->schemaManager = new SchemaManager(
$applicationTypeStub,
new NoopEventDispatcher(),
$this->extensionConfigurationMock,
new Renderer()
Expand Down
42 changes: 42 additions & 0 deletions Tests/Unit/Manager/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Brotkrueml\Schema\Tests\Unit\Manager;

use Brotkrueml\Schema\Adapter\ApplicationType;
use Brotkrueml\Schema\Core\Model\TypeInterface;
use Brotkrueml\Schema\JsonLd\Renderer;
use Brotkrueml\Schema\Manager\SchemaManager;
Expand All @@ -23,6 +24,7 @@
use Brotkrueml\Schema\Tests\Fixtures\Model\ServiceStub;
use Brotkrueml\Schema\Tests\Helper\NoopEventDispatcher;
use Brotkrueml\Schema\Tests\Helper\SchemaCacheTrait;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
Expand All @@ -35,6 +37,10 @@ final class SchemaManagerTest extends Testcase
private SchemaManager $subject;
private \ReflectionProperty $rendererTypes;
private Renderer $renderer;
/**
* @var ApplicationType&Stub
*/
private Stub $applicationTypeStub;

protected function setUp(): void
{
Expand All @@ -44,8 +50,14 @@ protected function setUp(): void
$this->rendererTypes = $reflector->getProperty('types');
$this->rendererTypes->setAccessible(true);

$this->applicationTypeStub = $this->createStub(ApplicationType::class);
$this->applicationTypeStub
->method('isBackend')
->willReturn(false);

$this->renderer = new Renderer();
$this->subject = new SchemaManager(
$this->applicationTypeStub,
new NoopEventDispatcher(),
$this->createStub(ExtensionConfiguration::class),
$this->renderer
Expand Down Expand Up @@ -358,6 +370,7 @@ public function dispatch(object $event)
};

$subject = new SchemaManager(
$this->applicationTypeStub,
$eventDispatcherStub,
$this->createStub(ExtensionConfiguration::class),
$this->renderer
Expand All @@ -384,6 +397,7 @@ public function onlyOneBreadCrumbListIsRenderedIfExtensionConfigurationIsEnabled
->willReturn('1');

$subject = new SchemaManager(
$this->applicationTypeStub,
new NoopEventDispatcher(),
$extensionConfigurationStub,
$this->renderer
Expand Down Expand Up @@ -415,6 +429,7 @@ public function allBreadCrumbListsAreRenderedIfExtensionConfigurationIsDisabled(
->willReturn('0');

$subject = new SchemaManager(
$this->applicationTypeStub,
new NoopEventDispatcher(),
$extensionConfigurationStub,
$this->renderer
Expand Down Expand Up @@ -464,4 +479,31 @@ public function multipleCallsOfRenderJsonLd(): void
$this->subject->renderJsonLd();
self::assertCount(1, $this->rendererTypes->getValue($this->renderer));
}

/**
* @test
*/
public function inBackendContextTheEventDispatcherIsNotCalled(): void
{
self::expectNotToPerformAssertions();

$applicationTypeStub = $this->createStub(ApplicationType::class);
$applicationTypeStub
->method('isBackend')
->willReturn(true);

$eventDispatcherStub = new class() implements EventDispatcherInterface {
public function dispatch(object $event)
{
throw new \Exception('dispatch() method must not be called');
}
};

new SchemaManager(
$applicationTypeStub,
$eventDispatcherStub,
$this->createStub(ExtensionConfiguration::class),
$this->renderer
);
}
}
2 changes: 1 addition & 1 deletion phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ parameters:
path: Classes/Manager/SchemaManager.php

-
message: "#^Class cognitive complexity is 25, keep it under 20$#"
message: "#^Class cognitive complexity is 26, keep it under 20$#"
count: 1
path: Classes/Manager/SchemaManager.php

Expand Down

0 comments on commit 87f17ac

Please sign in to comment.