Skip to content

Commit fb87cf4

Browse files
committed
TASK: Refactor and unify tests
1 parent 4c54512 commit fb87cf4

8 files changed

Lines changed: 249 additions & 334 deletions
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional;
5+
6+
/*
7+
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
8+
*
9+
* (c) Contributors of the Neos Project - www.neos.io
10+
*
11+
* This package is Open Source Software. For the full copyright and license
12+
* information, please view the LICENSE file which was distributed with this
13+
* source code.
14+
*/
15+
16+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Command\NodeIndexCommandController;
17+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder;
18+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
19+
use Neos\Flow\Tests\FunctionalTestCase;
20+
21+
abstract class BaseElasticsearchContentRepositoryAdapterTest extends FunctionalTestCase
22+
{
23+
protected const TESTING_INDEX_PREFIX = 'neoscr_testing';
24+
25+
/**
26+
* @var boolean
27+
*/
28+
protected static $testablePersistenceEnabled = true;
29+
30+
/**
31+
* @var NodeIndexCommandController
32+
*/
33+
protected $nodeIndexCommandController;
34+
35+
/**
36+
* @var ElasticSearchClient
37+
*/
38+
protected $searchClient;
39+
40+
protected static $instantiatedIndexes = [];
41+
42+
public function setUp(): void
43+
{
44+
parent::setUp();
45+
46+
$this->nodeIndexCommandController = $this->objectManager->get(NodeIndexCommandController::class);
47+
$this->searchClient = $this->objectManager->get(ElasticSearchClient::class);
48+
}
49+
50+
public function tearDown(): void
51+
{
52+
parent::tearDown();
53+
54+
$this->inject($this->contextFactory, 'contextInstances', []);
55+
56+
if (!$this->isIndexInitialized()) {
57+
// clean up any existing indices
58+
$this->searchClient->request('DELETE', '/' . self::TESTING_INDEX_PREFIX . '*');
59+
}
60+
}
61+
62+
/**
63+
* @param string $method
64+
* @return string
65+
*/
66+
protected function getLogMessagePrefix(string $method): string
67+
{
68+
return substr(strrchr($method, '\\'), 1);
69+
}
70+
71+
protected function indexNodes(): void
72+
{
73+
if ($this->isIndexInitialized()) {
74+
return;
75+
}
76+
77+
$this->nodeIndexCommandController->buildCommand(null, false, null, 'functionaltest');
78+
$this->setIndexInitialized();
79+
}
80+
81+
/**
82+
* @return ElasticSearchQueryBuilder
83+
*/
84+
protected function getQueryBuilder(): ElasticSearchQueryBuilder
85+
{
86+
try {
87+
/** @var ElasticSearchQueryBuilder $elasticSearchQueryBuilder */
88+
$elasticSearchQueryBuilder = $this->objectManager->get(ElasticSearchQueryBuilder::class);
89+
$this->inject($elasticSearchQueryBuilder, 'now', new \DateTimeImmutable('@1735685400')); // Dec. 31, 2024 23:50:00
90+
91+
return $elasticSearchQueryBuilder;
92+
} catch (\Exception $exception) {
93+
static::fail('Setting up the QueryBuilder failed: ' . $exception->getMessage());
94+
}
95+
}
96+
97+
protected function isIndexInitialized(): bool
98+
{
99+
return self::$instantiatedIndexes[get_class($this)] ?? false;
100+
}
101+
102+
protected function setIndexInitialized(): void
103+
{
104+
self::$instantiatedIndexes[get_class($this)] = true;
105+
}
106+
}

Tests/Functional/Eel/ElasticSearchMultiDimensionQueryTest.php

Lines changed: 5 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,15 @@
1313
* source code.
1414
*/
1515

16-
use DateTime;
17-
use DateTimeImmutable;
18-
use Exception;
19-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Command\NodeIndexCommandController;
20-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder;
21-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryResult;
22-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
23-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
16+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\BaseElasticsearchContentRepositoryAdapterTest;
17+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\Assertions;
2418
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\ContentRepositoryMultiDimensionNodeCreationTrait;
19+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\ContentRepositorySetupTrait;
2520
use Neos\ContentRepository\Domain\Model\NodeInterface;
2621

27-
use Neos\Flow\Tests\FunctionalTestCase;
28-
29-
class ElasticSearchMultiDimensionQueryTest extends FunctionalTestCase
22+
class ElasticSearchMultiDimensionQueryTest extends BaseElasticsearchContentRepositoryAdapterTest
3023
{
31-
use ContentRepositoryMultiDimensionNodeCreationTrait;
32-
33-
const TESTING_INDEX_PREFIX = 'neoscr_testing';
34-
35-
/**
36-
* @var ElasticSearchClient
37-
*/
38-
protected $searchClient;
39-
40-
/**
41-
* @var NodeIndexCommandController
42-
*/
43-
protected $nodeIndexCommandController;
44-
45-
/**
46-
* @var boolean
47-
*/
48-
protected static $testablePersistenceEnabled = true;
49-
50-
/**
51-
* @var NodeIndexer
52-
*/
53-
protected $nodeIndexer;
24+
use ContentRepositorySetupTrait, ContentRepositoryMultiDimensionNodeCreationTrait, Assertions;
5425

5526
/**
5627
* @var NodeInterface
@@ -67,37 +38,15 @@ class ElasticSearchMultiDimensionQueryTest extends FunctionalTestCase
6738
*/
6839
protected $siteNodeDk;
6940

70-
/**
71-
* @var boolean
72-
*/
73-
protected static $indexInitialized = false;
74-
75-
7641
public function setUp(): void
7742
{
7843
parent::setUp();
7944

80-
$this->nodeIndexer = $this->objectManager->get(NodeIndexer::class);
81-
$this->searchClient = $this->objectManager->get(ElasticSearchClient::class);
82-
83-
if (self::$indexInitialized !== true) {
84-
// clean up any existing indices
85-
$this->searchClient->request('DELETE', '/' . self::TESTING_INDEX_PREFIX . '*');
86-
}
87-
88-
89-
$this->nodeIndexCommandController = $this->objectManager->get(NodeIndexCommandController::class);
9045
$this->setupContentRepository();
9146
$this->createNodesForNodeSearchTest();
9247
$this->indexNodes();
9348
}
9449

95-
public function tearDown(): void
96-
{
97-
parent::tearDown();
98-
$this->inject($this->contextFactory, 'contextInstances', []);
99-
}
100-
10150
/**
10251
* @test
10352
*/
@@ -147,56 +96,4 @@ public function countDkDimensionNodesTest(): void
14796
static::assertCount(5, $resultDk->toArray());
14897
static::assertNodeNames(['root', 'document1', 'document2-dk', 'document4-de', 'document-untranslated'], $resultDk);
14998
}
150-
151-
152-
protected function indexNodes(): void
153-
{
154-
if (self::$indexInitialized === true) {
155-
return;
156-
}
157-
158-
$this->nodeIndexCommandController->buildCommand(null, false, 'live');
159-
self::$indexInitialized = true;
160-
}
161-
162-
/**
163-
* @param string $method
164-
* @return string
165-
*/
166-
protected function getLogMessagePrefix(string $method): string
167-
{
168-
return substr(strrchr($method, '\\'), 1);
169-
}
170-
171-
/**
172-
* @return ElasticSearchQueryBuilder
173-
*/
174-
protected function getQueryBuilder(): ElasticSearchQueryBuilder
175-
{
176-
try {
177-
$elasticSearchQueryBuilder = $this->objectManager->get(ElasticSearchQueryBuilder::class);
178-
$this->inject($elasticSearchQueryBuilder, 'now', new DateTimeImmutable('@1735685400')); // Dec. 31, 2024 23:50:00
179-
180-
return $elasticSearchQueryBuilder;
181-
} catch (Exception $exception) {
182-
static::fail('Setting up the QueryBuilder failed: ' . $exception->getMessage());
183-
}
184-
}
185-
186-
private static function extractNodeNames(ElasticSearchQueryResult $result): array
187-
{
188-
return array_map(static function (NodeInterface $node) {
189-
return $node->getName();
190-
}, $result->toArray());
191-
}
192-
193-
private static function assertNodeNames(array $expectedNames, ElasticSearchQueryResult $actualResult): void
194-
{
195-
sort($expectedNames);
196-
197-
$actualNames = self::extractNodeNames($actualResult);
198-
sort($actualNames);
199-
200-
self::assertEquals($expectedNames, $actualNames);
201-
}
20299
}

Tests/Functional/Eel/ElasticSearchQueryTest.php

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,29 @@
1313
* source code.
1414
*/
1515

16-
use DateTimeImmutable;
17-
use Exception;
18-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Command\NodeIndexCommandController;
1916
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder;
2017
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryResult;
2118
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
19+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\BaseElasticsearchContentRepositoryAdapterTest;
2220
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\ContentRepositoryNodeCreationTrait;
21+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\ContentRepositorySetupTrait;
2322
use Neos\ContentRepository\Domain\Model\NodeInterface;
2423
use Neos\Flow\Persistence\QueryResultInterface;
25-
use Neos\Flow\Tests\FunctionalTestCase;
2624

27-
class ElasticSearchQueryTest extends FunctionalTestCase
25+
class ElasticSearchQueryTest extends BaseElasticsearchContentRepositoryAdapterTest
2826
{
29-
use ContentRepositoryNodeCreationTrait;
30-
31-
/**
32-
* @var NodeIndexCommandController
33-
*/
34-
protected $nodeIndexCommandController;
35-
36-
/**
37-
* @var boolean
38-
*/
39-
protected static $testablePersistenceEnabled = true;
40-
41-
/**
42-
* @var boolean
43-
*/
44-
protected static $indexInitialized = false;
27+
use ContentRepositorySetupTrait, ContentRepositoryNodeCreationTrait;
4528

4629
public function setUp(): void
4730
{
4831
parent::setUp();
4932
$this->setupContentRepository();
5033

51-
$this->nodeIndexCommandController = $this->objectManager->get(NodeIndexCommandController::class);
52-
5334
$this->createNodesForNodeSearchTest();
54-
sleep(2);
35+
sleep(1);
5536
$this->indexNodes();
5637
}
5738

58-
public function tearDown(): void
59-
{
60-
parent::tearDown();
61-
$this->inject($this->contextFactory, 'contextInstances', []);
62-
}
63-
6439
/**
6540
* @test
6641
*/
@@ -331,38 +306,4 @@ public function cacheLifetimeIsCalculatedCorrectly(): void
331306

332307
static::assertEquals(600, $cacheLifetime);
333308
}
334-
335-
/**
336-
* @param string $method
337-
* @return string
338-
*/
339-
protected function getLogMessagePrefix(string $method): string
340-
{
341-
return substr(strrchr($method, '\\'), 1);
342-
}
343-
344-
protected function indexNodes(): void
345-
{
346-
if (self::$indexInitialized === true) {
347-
return;
348-
}
349-
350-
$this->nodeIndexCommandController->buildCommand(null, false, null, 'functionaltest');
351-
self::$indexInitialized = true;
352-
}
353-
354-
/**
355-
* @return ElasticSearchQueryBuilder
356-
*/
357-
protected function getQueryBuilder(): ElasticSearchQueryBuilder
358-
{
359-
try {
360-
$elasticSearchQueryBuilder = $this->objectManager->get(ElasticSearchQueryBuilder::class);
361-
$this->inject($elasticSearchQueryBuilder, 'now', new DateTimeImmutable('@1735685400')); // Dec. 31, 2024 23:50:00
362-
363-
return $elasticSearchQueryBuilder->query($this->siteNode);
364-
} catch (Exception $exception) {
365-
static::fail('Setting up the QueryBuilder failed: ' . $exception->getMessage());
366-
}
367-
}
368309
}

0 commit comments

Comments
 (0)