[TASK] Process feedback and fix unit-tests

This commit is contained in:
Benjamin Serfhos 2018-10-24 17:41:47 +02:00
parent 028914e789
commit a50cbf25a2
9 changed files with 54 additions and 24 deletions

View file

@ -55,10 +55,6 @@ class MappingFactory implements Singleton
$mapping->setType($type); $mapping->setType($type);
$configuration = $this->getConfiguration($type->getName()); $configuration = $this->getConfiguration($type->getName());
if (isset($configuration['_all'])) {
$mapping->setAllField($configuration['_all']);
unset($configuration['_all']);
}
$mapping->setProperties($configuration); $mapping->setProperties($configuration);
return $mapping; return $mapping;

View file

@ -25,6 +25,7 @@ use Codappix\SearchCore\Connection\ConnectionInterface;
use Codappix\SearchCore\Connection\FacetRequestInterface; use Codappix\SearchCore\Connection\FacetRequestInterface;
use Codappix\SearchCore\Connection\SearchRequestInterface; use Codappix\SearchCore\Connection\SearchRequestInterface;
use Codappix\SearchCore\Domain\Search\SearchService; use Codappix\SearchCore\Domain\Search\SearchService;
use Codappix\SearchCore\Utility\ArrayUtility as CustomArrayUtility;
use TYPO3\CMS\Core\Utility\ArrayUtility; use TYPO3\CMS\Core\Utility\ArrayUtility;
/** /**
@ -101,7 +102,7 @@ class SearchRequest implements SearchRequestInterface
public function setFilter(array $filter) public function setFilter(array $filter)
{ {
$filter = ArrayUtility::removeArrayEntryByValue($filter, ''); $filter = ArrayUtility::removeArrayEntryByValue($filter, '');
$this->filter = ArrayUtility::removeNullValuesRecursive($filter); $this->filter = CustomArrayUtility::removeEmptyElementsRecursively($filter);
} }
/** /**

View file

@ -124,9 +124,11 @@ class QueryFactory
'query' => $searchRequest->getSearchTerm(), 'query' => $searchRequest->getSearchTerm(),
]; ];
$fieldsToQuery = GeneralUtility::trimExplode(',', $this->configuration->getIfExists('searching.fields.query'), true); try {
if (!empty($fieldsToQuery)) { $fieldsToQuery = GeneralUtility::trimExplode(',', $this->configuration->get('searching.fields.query'), true);
$matchExpression['fields'] = $fieldsToQuery; $matchExpression['fields'] = $fieldsToQuery;
} catch (InvalidArgumentException $e) {
// Nothing configured
} }
$minimumShouldMatch = $this->configuration->getIfExists('searching.minimumShouldMatch'); $minimumShouldMatch = $this->configuration->getIfExists('searching.minimumShouldMatch');

View file

@ -0,0 +1,35 @@
<?php
namespace Codappix\SearchCore\Utility;
/**
* Utility: Array
* @package Codappix\SearchCore\Utility
*/
class ArrayUtility
{
/**
* Recursively removes empty array elements.
*
* @param array $array
* @return array the modified array
* @see \TYPO3\CMS\Extbase\Utility\ArrayUtility::removeEmptyElementsRecursively Removed in TYPO3 v9
*/
public static function removeEmptyElementsRecursively(array $array): array
{
$result = $array;
foreach ($result as $key => $value) {
if (is_array($value)) {
$result[$key] = self::removeEmptyElementsRecursively($value);
if ($result[$key] === []) {
unset($result[$key]);
}
} elseif ($value === null) {
unset($result[$key]);
}
}
return $result;
}
}

View file

@ -46,10 +46,6 @@ class MappingFactoryTest extends AbstractUnitTestCase
{ {
$indexName = 'someIndex'; $indexName = 'someIndex';
$configuration = [ $configuration = [
'_all' => [
'type' => 'text',
'analyzer' => 'ngram4',
],
'channel' => [ 'channel' => [
'type' => 'keyword', 'type' => 'keyword',
], ],
@ -64,16 +60,8 @@ class MappingFactoryTest extends AbstractUnitTestCase
->method('get') ->method('get')
->with('indexing.' . $indexName . '.mapping') ->with('indexing.' . $indexName . '.mapping')
->willReturn($configuration); ->willReturn($configuration);
$mapping = $this->subject->getMapping($type)->toArray()[$indexName]; $mapping = $this->subject->getMapping($type)->toArray()[$indexName];
$this->assertArraySubset(
[
'_all' => $configuration['_all']
],
$mapping,
true,
'Configuration of _all field was not set for mapping.'
);
$this->assertArraySubset( $this->assertArraySubset(
[ [
'channel' => $configuration['channel'] 'channel' => $configuration['channel']

View file

@ -22,7 +22,7 @@ namespace Codappix\Tests\Unit\Controller;
use Codappix\SearchCore\Controller\SearchController; use Codappix\SearchCore\Controller\SearchController;
use Codappix\SearchCore\Domain\Model\SearchRequest; use Codappix\SearchCore\Domain\Model\SearchRequest;
use Codappix\SearchCore\Domain\Search\SearchService; use Codappix\SearchCore\Domain\Search\CachedSearchService;
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase; use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
use TYPO3\CMS\Extbase\Mvc\Web\Request; use TYPO3\CMS\Extbase\Mvc\Web\Request;
use TYPO3\CMS\Extbase\Object\ObjectManager; use TYPO3\CMS\Extbase\Object\ObjectManager;
@ -54,7 +54,7 @@ class SearchControllerTest extends AbstractUnitTestCase
parent::setUp(); parent::setUp();
$searchService = $this->getMockBuilder(SearchService::class) $searchService = $this->getMockBuilder(CachedSearchService::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->request = new Request(); $this->request = new Request();

View file

@ -82,6 +82,7 @@ class AbstractIndexerTest extends AbstractUnitTestCase
$expectedRecord['new_test_field'] = 'test'; $expectedRecord['new_test_field'] = 'test';
$expectedRecord['new_test_field2'] = 'test' . PHP_EOL . 'test'; $expectedRecord['new_test_field2'] = 'test' . PHP_EOL . 'test';
$expectedRecord['search_abstract'] = ''; $expectedRecord['search_abstract'] = '';
$expectedRecord['search_document_type'] = 'testTable';
$this->dataProcessorService->expects($this->any()) $this->dataProcessorService->expects($this->any())
->method('executeDataProcessor') ->method('executeDataProcessor')
@ -137,6 +138,7 @@ class AbstractIndexerTest extends AbstractUnitTestCase
$record = ['field 1' => 'test']; $record = ['field 1' => 'test'];
$expectedRecord = $record; $expectedRecord = $record;
$expectedRecord['search_abstract'] = ''; $expectedRecord['search_abstract'] = '';
$expectedRecord['search_document_type'] = 'testTable';
$this->configuration->expects($this->exactly(2)) $this->configuration->expects($this->exactly(2))
->method('get') ->method('get')

View file

@ -45,6 +45,10 @@ class SearchRequestTest extends AbstractUnitTestCase
); );
} }
/**
* Data provider for emptyFilterWillNotBeSet()
* @return array
*/
public function possibleEmptyFilter() public function possibleEmptyFilter()
{ {
return [ return [

View file

@ -428,7 +428,7 @@ class QueryFactoryTest extends AbstractUnitTestCase
['searching.fieldValueFactor'] ['searching.fieldValueFactor']
) )
->will($this->onConsecutiveCalls( ->will($this->onConsecutiveCalls(
'_all, field1, field2', 'field1, field2',
$this->throwException(new InvalidArgumentException), $this->throwException(new InvalidArgumentException),
$this->throwException(new InvalidArgumentException), $this->throwException(new InvalidArgumentException),
$this->throwException(new InvalidArgumentException), $this->throwException(new InvalidArgumentException),
@ -445,7 +445,6 @@ class QueryFactoryTest extends AbstractUnitTestCase
'type' => 'most_fields', 'type' => 'most_fields',
'query' => 'SearchWord', 'query' => 'SearchWord',
'fields' => [ 'fields' => [
'_all',
'field1', 'field1',
'field2', 'field2',
], ],
@ -662,6 +661,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
); );
} }
/**
* @return void
*/
protected function configureConfigurationMockWithDefault() protected function configureConfigurationMockWithDefault()
{ {
$this->configuration->expects($this->any()) $this->configuration->expects($this->any())