Skip to content

Commit

Permalink
[TASK] Move ObjectManager to constructor in AbstractFacet
Browse files Browse the repository at this point in the history
As long as EXT:solr doesn't support DI and the ObjectManager is
still required, the ObjectManager is set/instantiated in the
constructor, preventing initialization issues.

Resolves: #3235
  • Loading branch information
dkd-friedrich authored and dkd-kaehm committed Apr 21, 2022
1 parent b15f244 commit 35405f3
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 37 deletions.
17 changes: 6 additions & 11 deletions Classes/Domain/Search/ResultSet/Facets/AbstractFacet.php
Expand Up @@ -16,6 +16,8 @@
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets;

use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
Expand Down Expand Up @@ -89,29 +91,22 @@ abstract class AbstractFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManager $objectManager
*/
public function __construct(
SearchResultSet $resultSet,
string $name,
string $field,
string $label = '',
array $configuration = []
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
$this->resultSet = $resultSet;
$this->name = $name;
$this->field = $field;
$this->label = $label;
$this->configuration = $configuration;
}

/**
* Injects the object manager
*
* @param ObjectManagerInterface $objectManager
*/
public function injectObjectManager(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
$this->objectManager = $objectManager ?? GeneralUtility::makeInstance(ObjectManager::class);
}

/**
Expand Down
Expand Up @@ -20,6 +20,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItemCollection;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
* Class AbstractOptionsFacet
Expand All @@ -42,15 +43,17 @@ class AbstractOptionsFacet extends AbstractFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
SearchResultSet $resultSet,
string $name,
string $field,
string $label = '',
array $configuration = []
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
parent::__construct($resultSet, $name, $field, $label, $configuration);
parent::__construct($resultSet, $name, $field, $label, $configuration, $objectManager);
$this->options = new OptionCollection();
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItemCollection;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
* Value object that represent the options facet.
Expand Down Expand Up @@ -59,15 +60,17 @@ class HierarchyFacet extends AbstractFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
SearchResultSet $resultSet,
string $name,
string $field,
string $label = '',
array $configuration = []
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
parent::__construct($resultSet, $name, $field, $label, $configuration);
parent::__construct($resultSet, $name, $field, $label, $configuration, $objectManager);
$this->childNodes = new NodeCollection();
$this->allNodes = new NodeCollection();
}
Expand Down
Expand Up @@ -50,7 +50,7 @@ public function parse(SearchResultSet $resultSet, string $facetName, array $face
}

/** @var $facet HierarchyFacet */
$facet = $this->objectManager->get(HierarchyFacet::class, $resultSet, $facetName, $fieldName, $label, $facetConfiguration);
$facet = $this->objectManager->get(HierarchyFacet::class, $resultSet, $facetName, $fieldName, $label, $facetConfiguration, $this->objectManager);

$hasActiveOptions = count($optionsFromRequest) > 0;
$facet->setIsUsed($hasActiveOptions);
Expand Down
Expand Up @@ -17,6 +17,7 @@

use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\AbstractOptionsFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
* Value object that represent a options facet.
Expand All @@ -42,9 +43,16 @@ class OptionsFacet extends AbstractOptionsFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManagerInterface $objectManager
*/
public function __construct(SearchResultSet $resultSet, $name, $field, $label = '', array $configuration = [])
{
parent::__construct($resultSet, $name, $field, $label, $configuration);
public function __construct(
SearchResultSet $resultSet,
$name,
$field,
$label = '',
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
parent::__construct($resultSet, $name, $field, $label, $configuration, $objectManager);
}
}
Expand Up @@ -76,7 +76,8 @@ public function parse(SearchResultSet $resultSet, string $facetName, array $face
$facetName,
$fieldName,
$label,
$facetConfiguration
$facetConfiguration,
$this->objectManager
);

$hasActiveOptions = count($optionsFromRequest) > 0;
Expand Down
Expand Up @@ -17,6 +17,7 @@

use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\AbstractOptionsFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
* Class QueryGroupFacet
Expand All @@ -42,9 +43,16 @@ class QueryGroupFacet extends AbstractOptionsFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManagerInterface $objectManager
*/
public function __construct(SearchResultSet $resultSet, $name, $field, $label = '', array $configuration = [])
{
parent::__construct($resultSet, $name, $field, $label, $configuration);
public function __construct(
SearchResultSet $resultSet,
$name,
$field,
$label = '',
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
parent::__construct($resultSet, $name, $field, $label, $configuration, $objectManager);
}
}
Expand Up @@ -56,7 +56,8 @@ public function parse(SearchResultSet $resultSet, string $facetName, array $face
$facetName,
$fieldName,
$label,
$facetConfiguration
$facetConfiguration,
$this->objectManager
);

$activeFacets = $resultSet->getUsedSearchRequest()->getActiveFacetNames();
Expand Down
Expand Up @@ -65,7 +65,8 @@ protected function getParsedFacet(
$facetName,
$fieldName,
$label,
$facetConfiguration
$facetConfiguration,
$this->objectManager
);

$facet->setIsAvailable(count($valuesFromResponse) > 0);
Expand Down
Expand Up @@ -20,6 +20,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItemCollection;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
* Value object that represent a date range facet.
Expand Down Expand Up @@ -50,15 +51,17 @@ class DateRangeFacet extends AbstractFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
SearchResultSet $resultSet,
string $name,
string $field,
string $label = '',
array $configuration = []
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
parent::__construct($resultSet, $name, $field, $label, $configuration);
parent::__construct($resultSet, $name, $field, $label, $configuration, $objectManager);
}

/**
Expand Down
Expand Up @@ -20,6 +20,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetItemCollection;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;

/**
* Value object that represent a date range facet.
Expand Down Expand Up @@ -50,15 +51,17 @@ class NumericRangeFacet extends AbstractFacet
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
SearchResultSet $resultSet,
string $name,
string $field,
string $label = '',
array $configuration = []
array $configuration = [],
ObjectManagerInterface $objectManager = null
) {
parent::__construct($resultSet, $name, $field, $label, $configuration);
parent::__construct($resultSet, $name, $field, $label, $configuration, $objectManager);
}

/**
Expand Down
21 changes: 13 additions & 8 deletions Tests/Unit/Domain/Search/ResultSet/Facets/FacetCollectionTest.php
Expand Up @@ -19,6 +19,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\OptionsFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Class FacetCollectionTest
Expand All @@ -35,9 +36,10 @@ public function canAddAndRetrieveFacetByKey()
{
$facetCollection = new FacetCollection();
$resultSetMock = $this->getDumbMock(SearchResultSet::class);
$objectManagerMock = $this->createMock(ObjectManager::class);

$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'left']);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left']);
$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'left'], $objectManagerMock);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left'], $objectManagerMock);
$facetCollection->addFacet($colorFacet);
$facetCollection->addFacet($brandFacet);

Expand All @@ -52,9 +54,10 @@ public function canAddAndRetrieveFacetByPosition()
{
$facetCollection = new FacetCollection();
$resultSetMock = $this->getDumbMock(SearchResultSet::class);
$objectManagerMock = $this->createMock(ObjectManager::class);

$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'left']);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left']);
$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'left'], $objectManagerMock);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left'], $objectManagerMock);
$facetCollection->addFacet($colorFacet);
$facetCollection->addFacet($brandFacet);

Expand All @@ -69,9 +72,10 @@ public function canRetrieveFacetOfCollectionCopyByKey()
{
$facetCollection = new FacetCollection();
$resultSetMock = $this->getDumbMock(SearchResultSet::class);
$objectManagerMock = $this->createMock(ObjectManager::class);

$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'top']);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left']);
$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'top'], $objectManagerMock);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left'], $objectManagerMock);
$facetCollection->addFacet($colorFacet);
$facetCollection->addFacet($brandFacet);

Expand All @@ -87,9 +91,10 @@ public function canRetrieveFacetOfCollectionCopyByPosition()
{
$facetCollection = new FacetCollection();
$resultSetMock = $this->getDumbMock(SearchResultSet::class);
$objectManagerMock = $this->createMock(ObjectManager::class);

$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'top']);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left']);
$colorFacet = new OptionsFacet($resultSetMock, 'color', 'color_s', '', ['groupName' => 'top'], $objectManagerMock);
$brandFacet = new OptionsFacet($resultSetMock, 'brand', 'brand_s', '', ['groupName' => 'left'], $objectManagerMock);
$facetCollection->addFacet($colorFacet);
$facetCollection->addFacet($brandFacet);

Expand Down
Expand Up @@ -19,6 +19,8 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\OptionsFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Unit test for the OptionsFacet
Expand All @@ -27,6 +29,18 @@
*/
class OptionCollectionTest extends UnitTest
{
protected function setUp(): void
{
parent::setUp();
GeneralUtility::setSingletonInstance(ObjectManager::class, $this->createMock(ObjectManager::class));
}

protected function tearDown(): void
{
GeneralUtility::purgeInstances();
parent::tearDown();
}

/**
* @test
*/
Expand Down
Expand Up @@ -19,6 +19,8 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\OptionsFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Unit test for the OptionsFacet
Expand All @@ -28,6 +30,17 @@
*/
class OptionsFacetTest extends UnitTest
{
protected function setUp(): void
{
parent::setUp();
GeneralUtility::setSingletonInstance(ObjectManager::class, $this->createMock(ObjectManager::class));
}

protected function tearDown(): void
{
GeneralUtility::purgeInstances();
parent::tearDown();
}

/**
* @test
Expand Down
Expand Up @@ -19,6 +19,8 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\QueryGroup\QueryGroupFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Unit test for the QueryGroupFacet options collection
Expand All @@ -33,6 +35,8 @@ class OptionCollectionTest extends UnitTest
*/
public function canGetManualSortedCopy()
{
GeneralUtility::setSingletonInstance(ObjectManager::class, $this->createMock(ObjectManager::class));

$searchResultSetMock = $this->getDumbMock(SearchResultSet::class);
$facet = new QueryGroupFacet($searchResultSetMock, 'age', 'created');

Expand Down

0 comments on commit 35405f3

Please sign in to comment.