Skip to content

Commit fd492ed

Browse files
committed
TASK: Introduce getObject pattern
1 parent 609a153 commit fd492ed

7 files changed

Lines changed: 58 additions & 46 deletions

File tree

Tests/Functional/AbstractNodeTemplateTestCase.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Neos\ContentRepository\Domain\Model\Workspace;
1212
use Neos\ContentRepository\Domain\Repository\ContentDimensionRepository;
1313
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
14-
use Neos\ContentRepository\Domain\Service\Context;
1514
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
1615
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
1716
use Neos\Flow\Tests\FunctionalTestCase;
@@ -43,10 +42,11 @@ abstract class AbstractNodeTemplateTestCase extends FunctionalTestCase
4342

4443
private NodeTypeManager $nodeTypeManager;
4544

46-
private Context $subgraph;
47-
4845
private string $fixturesDir;
4946

47+
/** @deprecated please use {@see self::getObject()} instead */
48+
protected $objectManager;
49+
5050
public function setUp(): void
5151
{
5252
parent::setUp();
@@ -82,6 +82,17 @@ public function tearDown(): void
8282
$this->objectManager->forgetInstance(NodeTypeManager::class);
8383
}
8484

85+
/**
86+
* @template T of object
87+
* @param class-string<T> $className
88+
*
89+
* @return T
90+
*/
91+
final protected function getObject(string $className): object
92+
{
93+
return $this->objectManager->get($className);
94+
}
95+
8596
private function setupContentRepository(): void
8697
{
8798
// Create an environment to create nodes.
@@ -98,10 +109,9 @@ private function setupContentRepository(): void
98109

99110
$this->persistenceManager->persistAll();
100111
$this->contextFactory = $this->objectManager->get(ContextFactoryInterface::class);
101-
$this->subgraph = $this->contextFactory->create(['workspaceName' => 'live']);
102-
103-
$rootNode = $this->subgraph->getRootNode();
112+
$subgraph = $this->contextFactory->create(['workspaceName' => 'live']);
104113

114+
$rootNode = $subgraph->getRootNode();
105115

106116
$sitesRootNode = $rootNode->createNode('sites');
107117
$testSiteNode = $sitesRootNode->createNode('test-site');

Tests/Functional/FakeNodeTypeManagerTrait.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@
66

77
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
88
use Neos\Flow\Configuration\ConfigurationManager;
9-
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
109
use Neos\Utility\Arrays;
1110
use Symfony\Component\Yaml\Yaml;
1211

1312
/**
14-
* @property ObjectManagerInterface $objectManager
1513
* @property NodeTypeManager $nodeTypeManager
1614
*/
1715
trait FakeNodeTypeManagerTrait
1816
{
17+
/**
18+
* @template T of object
19+
* @param class-string<T> $className
20+
*
21+
* @return T
22+
*/
23+
abstract protected function getObject(string $className): object;
24+
1925
private function loadFakeNodeTypes(): void
2026
{
21-
$configuration = $this->objectManager->get(ConfigurationManager::class)->getConfiguration('NodeTypes');
27+
$configuration = $this->getObject(ConfigurationManager::class)->getConfiguration('NodeTypes');
2228

2329
$fileIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/Features'));
2430

Tests/Functional/Features/Exceptions/ExceptionsTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,6 @@ public function exceptionsAreCaughtAndPartialTemplateIsNotBuild(): void
4848

4949
$this->assertLastCreatedTemplateMatchesSnapshot('OnlyExceptions');
5050

51-
// self::assertSame([
52-
// [
53-
// 'message' => 'Template for "WithOneEvaluationException" was not applied. Only Node /sites/test-site/homepage/main/new-node@live[Flowpack.NodeTemplates:Content.WithOneEvaluationException] was created.',
54-
// 'severity' => 'ERROR'
55-
// ],
56-
// [
57-
// 'message' => 'Expression "${\'left open" in "childNodes.abort.when" | EelException(The EEL expression "${\'left open" was not a valid EEL expression. Perhaps you forgot to wrap it in ${...}?, 1410441849)',
58-
// 'severity' => 'ERROR'
59-
// ]
60-
// ], $this->getMessagesOfFeedbackCollection());
61-
62-
6351
$this->assertCaughtExceptionsMatchesSnapshot('OnlyExceptions');
6452
$this->assertNodeDumpAndTemplateDumpMatchSnapshot('OnlyExceptions', $createdNode);
6553
});

Tests/Functional/Features/ResolvableProperties/ResolvablePropertiesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public function itMatchesSnapshot1(): void
2020
$this->createFakeNode('some-node-id');
2121
$this->createFakeNode('other-node-id');
2222

23-
$resource = $this->objectManager->get(ResourceManager::class)->importResource(__DIR__ . '/image.png');
23+
$resource = $this->getObject(ResourceManager::class)->importResource(__DIR__ . '/image.png');
2424

2525
$asset = new Asset($resource);
2626
ObjectAccess::setProperty($asset, 'Persistence_Object_Identifier', 'c228200e-7472-4290-9936-4454a5b5692a', true);
27-
$this->objectManager->get(AssetRepository::class)->add($asset);
27+
$this->getObject(AssetRepository::class)->add($asset);
2828

29-
$resource2 = $this->objectManager->get(ResourceManager::class)->importResource(__DIR__ . '/image.png');
29+
$resource2 = $this->getObject(ResourceManager::class)->importResource(__DIR__ . '/image.png');
3030

3131
$image = new Image($resource2);
3232
ObjectAccess::setProperty($image, 'Persistence_Object_Identifier', 'c8ae9f9f-dd11-4373-bf42-4bf31ec5bd19', true);
33-
$this->objectManager->get(ImageRepository::class)->add($image);
33+
$this->getObject(ImageRepository::class)->add($image);
3434

3535
$createdNode = $this->createNodeInto(
3636
$this->homePageMainContentCollectionNode,

Tests/Functional/Features/StandaloneValidationCommand/StandaloneValidationCommandTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
namespace Flowpack\NodeTemplates\Tests\Functional\Features\StandaloneValidationCommand;
66

77
use Flowpack\NodeTemplates\Application\Command\NodeTemplateCommandController;
8-
use Flowpack\NodeTemplates\Domain\NodeTemplateDumper\NodeTemplateDumper;
9-
use Flowpack\NodeTemplates\Domain\Template\RootTemplate;
108
use Flowpack\NodeTemplates\Tests\Functional\FakeNodeTypeManagerTrait;
119
use Flowpack\NodeTemplates\Tests\Functional\SnapshotTrait;
12-
use Neos\ContentRepository\Domain\Model\NodeInterface;
1310
use Neos\ContentRepository\Domain\Model\Workspace;
1411
use Neos\ContentRepository\Domain\Repository\ContentDimensionRepository;
1512
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
@@ -33,14 +30,6 @@ final class StandaloneValidationCommandTest extends FunctionalTestCase
3330

3431
private ContextFactoryInterface $contextFactory;
3532

36-
protected NodeInterface $homePageNode;
37-
38-
protected NodeInterface $homePageMainContentCollectionNode;
39-
40-
private NodeTemplateDumper $nodeTemplateDumper;
41-
42-
private RootTemplate $lastCreatedRootTemplate;
43-
4433
private NodeTypeManager $nodeTypeManager;
4534

4635
private string $fixturesDir;
@@ -49,7 +38,7 @@ public function setUp(): void
4938
{
5039
parent::setUp();
5140

52-
$this->nodeTypeManager = $this->objectManager->get(NodeTypeManager::class);
41+
$this->nodeTypeManager = $this->getObject(NodeTypeManager::class);
5342

5443
$this->loadFakeNodeTypes();
5544

@@ -63,34 +52,45 @@ public function tearDown(): void
6352
{
6453
parent::tearDown();
6554
$this->inject($this->contextFactory, 'contextInstances', []);
66-
$this->objectManager->get(FeedbackCollection::class)->reset();
55+
$this->getObject(FeedbackCollection::class)->reset();
6756
$this->objectManager->forgetInstance(ContentDimensionRepository::class);
6857
$this->objectManager->forgetInstance(NodeTypeManager::class);
6958
}
7059

60+
/**
61+
* @template T of object
62+
* @param class-string<T> $className
63+
*
64+
* @return T
65+
*/
66+
final protected function getObject(string $className): object
67+
{
68+
return $this->objectManager->get($className);
69+
}
70+
7171
private function setupContentRepository(): void
7272
{
7373
// Create an environment to create nodes.
74-
$this->objectManager->get(ContentDimensionRepository::class)->setDimensionsConfiguration([]);
74+
$this->getObject(ContentDimensionRepository::class)->setDimensionsConfiguration([]);
7575

7676
$liveWorkspace = new Workspace('live');
77-
$workspaceRepository = $this->objectManager->get(WorkspaceRepository::class);
77+
$workspaceRepository = $this->getObject(WorkspaceRepository::class);
7878
$workspaceRepository->add($liveWorkspace);
7979

8080
$testSite = new Site('test-site');
8181
$testSite->setSiteResourcesPackageKey('Test.Site');
82-
$siteRepository = $this->objectManager->get(SiteRepository::class);
82+
$siteRepository = $this->getObject(SiteRepository::class);
8383
$siteRepository->add($testSite);
8484

8585
$this->persistenceManager->persistAll();
86-
$this->contextFactory = $this->objectManager->get(ContextFactoryInterface::class);
86+
$this->contextFactory = $this->getObject(ContextFactoryInterface::class);
8787
$subgraph = $this->contextFactory->create(['workspaceName' => 'live']);
8888

8989
$rootNode = $subgraph->getRootNode();
9090

9191
$sitesRootNode = $rootNode->createNode('sites');
9292
$testSiteNode = $sitesRootNode->createNode('test-site');
93-
$this->homePageNode = $testSiteNode->createNode(
93+
$testSiteNode->createNode(
9494
'homepage',
9595
$this->nodeTypeManager->getNodeType('Flowpack.NodeTemplates:Document.HomePage')
9696
);
@@ -99,7 +99,7 @@ private function setupContentRepository(): void
9999
/** @test */
100100
public function itMatchesSnapshot()
101101
{
102-
$commandController = $this->objectManager->get(NodeTemplateCommandController::class);
102+
$commandController = $this->getObject(NodeTemplateCommandController::class);
103103

104104
ObjectAccess::setProperty($commandController, 'response', $cliResponse = new Response(), true);
105105
ObjectAccess::getProperty($commandController, 'output', true)->setOutput($bufferedOutput = new BufferedOutput());

Tests/Functional/FeedbackCollectionMessagesTrait.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@
1010

1111
trait FeedbackCollectionMessagesTrait
1212
{
13+
/**
14+
* @template T of object
15+
* @param class-string<T> $className
16+
*
17+
* @return T
18+
*/
19+
abstract protected function getObject(string $className): object;
20+
1321
private function getMessagesOfFeedbackCollection(): array
1422
{
1523
/** @var FeedbackInterface[] $allFeedbacks */
16-
$allFeedbacks = ObjectAccess::getProperty($this->objectManager->get(FeedbackCollection::class), 'feedbacks', true);
24+
$allFeedbacks = ObjectAccess::getProperty($this->getObject(FeedbackCollection::class), 'feedbacks', true);
1725

1826
/** @var AbstractMessageFeedback[] $allFeedbacks */
1927
$messages = [];

Tests/Functional/WithConfigurationTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait WithConfigurationTrait
1818
*/
1919
private function withMockedConfigurationSettings(array $additionalSettings, callable $fn): void
2020
{
21-
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
21+
$configurationManager = $this->getObject(ConfigurationManager::class);
2222
$configurationManagerMock = $this->getMockBuilder(ConfigurationManager::class)->disableOriginalConstructor()->getMock();
2323
$mockedSettings = Arrays::arrayMergeRecursiveOverrule($configurationManager->getConfiguration('Settings'), $additionalSettings);
2424
$configurationManagerMock->expects(self::any())->method('getConfiguration')->willReturnCallback(function (string $configurationType, string $configurationPath = null) use($configurationManager, $mockedSettings) {

0 commit comments

Comments
 (0)