Commit aa022e84 authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by GitHub

WIP Increase test coverage (#593)

* cover Json Facets in tests

* added option 'min' to JsonAggregation

* filter empty buckets from facets during result parsing
parent cb0f6eef
......@@ -30,7 +30,7 @@ class JsonAggregation extends AbstractFacet implements JsonFacetInterface
*
* @return self Provides fluent interface
*/
public function setFunction($function)
public function setFunction(string $function)
{
return $this->setOption('function', $function);
}
......@@ -45,6 +45,35 @@ class JsonAggregation extends AbstractFacet implements JsonFacetInterface
return $this->getOption('function');
}
/**
* Set the min value.
*
* This overwrites the current value.
* This option doesn't exist in Solr originally, but it's useful to filter
* the aggregations returned by Solr.
*
* @param int $min
*
* @return self Provides fluent interface
*/
public function setMin(int $min)
{
return $this->setOption('min', $min);
}
/**
* Get the min value.
*
* This option doesn't exist in Solr originally, but it's useful to filter
* the aggregations returned by Solr.
*
* @return int
*/
public function getMin()
{
return $this->getOption('min');
}
/**
* Serializes nested facets as option "facet" and returns that array structure.
*
......
......@@ -25,6 +25,7 @@ class FacetSet extends AbstractComponent implements FacetSetInterface
FacetSetInterface::FACET_RANGE => 'Solarium\Component\Facet\Range',
FacetSetInterface::FACET_PIVOT => 'Solarium\Component\Facet\Pivot',
FacetSetInterface::FACET_INTERVAL => 'Solarium\Component\Facet\Interval',
FacetSetInterface::JSON_FACET_AGGREGATION => 'Solarium\Component\Facet\JsonAggregation',
FacetSetInterface::JSON_FACET_TERMS => 'Solarium\Component\Facet\JsonTerms',
FacetSetInterface::JSON_FACET_QUERY => 'Solarium\Component\Facet\JsonQuery',
FacetSetInterface::JSON_FACET_RANGE => 'Solarium\Component\Facet\JsonRange',
......@@ -352,6 +353,19 @@ class FacetSet extends AbstractComponent implements FacetSetInterface
return $this->createFacet(FacetSetInterface::FACET_INTERVAL, $options, $add);
}
/**
* Get a json facet aggregation instance.
*
* @param mixed $options
* @param bool $add
*
* @return \Solarium\Component\Facet\JsonAggregation
*/
public function createJsonFacetAggregation($options = null, $add = true)
{
return $this->createFacet(FacetSetInterface::JSON_FACET_AGGREGATION, $options, $add);
}
/**
* Get a json facet terms instance.
*
......
......@@ -2,7 +2,10 @@
namespace Solarium\Component\ResponseParser;
use Solarium\Component\Facet\FacetInterface;
use Solarium\Component\Facet\Field as QueryFacetField;
use Solarium\Component\Facet\JsonAggregation;
use Solarium\Component\Facet\JsonFacetInterface;
use Solarium\Component\Facet\MultiQuery as QueryFacetMultiQuery;
use Solarium\Component\Facet\Pivot as QueryFacetPivot;
use Solarium\Component\Facet\Query as QueryFacetQuery;
......@@ -112,7 +115,7 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
}
if (!empty($data['facets'])) {
$facets += $this->parseJsonFacetSet($data['facets']);
$facets += $this->parseJsonFacetSet($data['facets'], $facetSet->getFacets());
}
return $this->createFacetSet($facets);
......@@ -121,14 +124,15 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
/**
* Parse JSON facets.
*
* @param array $facets
* @param array $facet_data
* @param FacetInterface[] $facets
*
* @return array
*/
protected function parseJsonFacetSet($facets)
protected function parseJsonFacetSet($facet_data, $facets)
{
$buckets_and_aggregations = [];
foreach ($facets as $key => $values) {
foreach ($facet_data as $key => $values) {
if (is_array($values)) {
if (isset($values['buckets'])) {
$buckets = [];
......@@ -138,13 +142,25 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
$count = $bucket['count'];
unset($bucket['val']);
unset($bucket['count']);
$buckets[] = new Bucket($val, $count, new ResultFacetSet($this->parseJsonFacetSet($bucket)));
$buckets[] = new Bucket($val, $count, new ResultFacetSet($this->parseJsonFacetSet($bucket,
(isset($facets[$key]) && $facets[$key] instanceof JsonFacetInterface) ? $facets[$key]->getFacets() : []
)));
}
if ($buckets) {
$buckets_and_aggregations[$key] = new Buckets($buckets);
}
$buckets_and_aggregations[$key] = new Buckets($buckets);
} else {
$buckets_and_aggregations[$key] = new ResultFacetSet($this->parseJsonFacetSet($values));
$buckets_and_aggregations[$key] = new ResultFacetSet($this->parseJsonFacetSet($values,
(isset($facets[$key]) && $facets[$key] instanceof JsonFacetInterface) ? $facets[$key]->getFacets() : []
));
}
} else {
if (isset($facets[$key]) && $facets[$key] instanceof JsonAggregation) {
$min = $facets[$key]->getMin();
if (!is_null($min) && $values < $min) {
continue;
}
}
$buckets_and_aggregations[$key] = new Aggregation($values);
}
}
......
......@@ -327,6 +327,9 @@ class FacetSetTest extends TestCase
],
],
],
'empty_buckets' => [
'buckets' => [],
],
],
];
......@@ -349,5 +352,7 @@ class FacetSetTest extends TestCase
$nested_facets = $buckets[0]->getFacets();
$this->assertEquals(['top_authors', 'highpop'], array_keys($nested_facets));
$this->assertFalse(isset($facets['empty_buckets']));
}
}
<?php
namespace Solarium\Tests\QueryType\Select\Query\Component\Facet;
use PHPUnit\Framework\TestCase;
use Solarium\Component\Facet\JsonAggregation;
use Solarium\Component\FacetSet;
class JsonAggregationTest extends TestCase
{
/**
* @var JsonAggregation
*/
protected $facet;
public function setUp()
{
$this->facet = new JsonAggregation();
}
public function testConfigMode()
{
$options = [
'key' => 'myKey',
'function' => 'unique(field)',
'min' => 5,
];
$this->facet->setOptions($options);
$this->assertSame($options['key'], $this->facet->getKey());
$this->assertSame($options['function'], $this->facet->getFunction());
$this->assertSame($options['min'], $this->facet->getMin());
}
public function testGetType()
{
$this->assertSame(
FacetSet::JSON_FACET_AGGREGATION,
$this->facet->getType()
);
}
public function testSetAndGetFunction()
{
$this->facet->setFunction('sum(field)');
$this->assertSame('sum(field)', $this->facet->getFunction());
}
public function testSetAndGetMin()
{
$this->facet->setMin(100);
$this->assertSame(100, $this->facet->getMin());
}
}
<?php
namespace Solarium\Tests\QueryType\Select\Query\Component\Facet;
use PHPUnit\Framework\TestCase;
use Solarium\Component\Facet\JsonQuery;
use Solarium\Component\FacetSet;
class JsonQueryTest extends TestCase
{
/**
* @var JsonQuery
*/
protected $facet;
public function setUp()
{
$this->facet = new JsonQuery();
}
public function testConfigMode()
{
$options = [
'key' => 'myKey',
'q' => 'category:1',
];
$this->facet->setOptions($options);
$this->assertSame($options['key'], $this->facet->getKey());
$this->assertSame($options['q'], $this->facet->getQuery());
}
public function testGetType()
{
$this->assertSame(
FacetSet::JSON_FACET_QUERY,
$this->facet->getType()
);
}
public function testSetAndGetQuery()
{
$this->facet->setQuery('category:1');
$this->assertSame('category:1', $this->facet->getQuery());
}
public function testSetAndGetQueryWithBind()
{
$this->facet->setQuery('id:%1%', [678]);
$this->assertSame('id:678', $this->facet->getQuery());
}
}
<?php
namespace Solarium\Tests\QueryType\Select\Query\Component\Facet;
use PHPUnit\Framework\TestCase;
use Solarium\Component\Facet\JsonRange;
use Solarium\Component\FacetSet;
class JsonRangeTest extends TestCase
{
/**
* @var JsonRange
*/
protected $facet;
public function setUp()
{
$this->facet = new JsonRange();
}
public function testConfigMode()
{
$options = [
'key' => 'myKey',
'field' => 'content',
'start' => 1,
'end' => 100,
'gap' => 10,
'hardend' => true,
'other' => 'all',
'include' => 'lower',
];
$this->facet->setOptions($options);
$this->assertSame($options['key'], $this->facet->getKey());
$this->assertSame($options['field'], $this->facet->getField());
$this->assertSame($options['start'], $this->facet->getStart());
$this->assertSame($options['end'], $this->facet->getEnd());
$this->assertSame($options['gap'], $this->facet->getGap());
$this->assertSame($options['hardend'], $this->facet->getHardend());
$this->assertSame([$options['other']], $this->facet->getOther());
$this->assertSame([$options['include']], $this->facet->getInclude());
}
public function testGetType()
{
$this->assertSame(
FacetSet::JSON_FACET_RANGE,
$this->facet->getType()
);
}
public function testSetAndGetField()
{
$this->facet->setField('price');
$this->assertSame('price', $this->facet->getField());
}
public function testSetAndGetStart()
{
$this->facet->setStart(1);
$this->assertSame(1, $this->facet->getStart());
}
public function testSetAndGetEnd()
{
$this->facet->setEnd(100);
$this->assertSame(100, $this->facet->getEnd());
}
public function testSetAndGetGap()
{
$this->facet->setGap(10);
$this->assertSame(10, $this->facet->getGap());
}
public function testSetAndGetHardend()
{
$this->facet->setHardend(true);
$this->assertTrue($this->facet->getHardend());
}
public function testSetAndGetOther()
{
$this->facet->setOther('all');
$this->assertSame(['all'], $this->facet->getOther());
}
public function testSetAndGetOtherArray()
{
$this->facet->setOther(['before', 'after']);
$this->assertSame(['before', 'after'], $this->facet->getOther());
}
public function testSetAndGetInclude()
{
$this->facet->setInclude('all');
$this->assertSame(['all'], $this->facet->getInclude());
}
public function testSetAndGetIncludeArray()
{
$this->facet->setInclude(['lower', 'upper']);
$this->assertSame(['lower', 'upper'], $this->facet->getInclude());
}
}
<?php
namespace Solarium\Tests\QueryType\Select\Query\Component\Facet;
use PHPUnit\Framework\TestCase;
use Solarium\Component\Facet\JsonTerms;
use Solarium\Component\FacetSet;
class JsonTermsTest extends TestCase
{
/**
* @var JsonTerms
*/
protected $facet;
public function setUp()
{
$this->facet = new JsonTerms();
}
public function testConfigMode()
{
$options = [
'key' => 'myKey',
'field' => 'text',
'sort' => 'index',
'limit' => 10,
'offset' => 20,
'mincount' => 5,
'missing' => true,
'method' => 'enum',
'refine' => true,
'overrequest' => 3,
'numBuckets' => true,
'allBuckets' => true,
];
$this->facet->setOptions($options);
$this->assertSame($options['key'], $this->facet->getKey());
$this->assertSame($options['field'], $this->facet->getField());
$this->assertSame($options['sort'], $this->facet->getSort());
$this->assertSame($options['limit'], $this->facet->getLimit());
$this->assertSame($options['offset'], $this->facet->getOffset());
$this->assertSame($options['mincount'], $this->facet->getMinCount());
$this->assertSame($options['missing'], $this->facet->getMissing());
$this->assertSame($options['method'], $this->facet->getMethod());
$this->assertSame($options['refine'], $this->facet->getRefine());
$this->assertSame($options['overrequest'], $this->facet->getOverRequest());
$this->assertSame($options['numBuckets'], $this->facet->getNumBuckets());
$this->assertSame($options['allBuckets'], $this->facet->getAllBuckets());
}
public function testGetType()
{
$this->assertSame(
FacetSet::JSON_FACET_TERMS,
$this->facet->getType()
);
}
public function testSetAndGetField()
{
$this->facet->setField('category');
$this->assertSame('category', $this->facet->getField());
}
public function testSetAndGetSort()
{
$this->facet->setSort('index');
$this->assertSame('index', $this->facet->getSort());
}
public function testSetAndGetPrefix()
{
$this->facet->setPrefix('xyz');
$this->assertSame('xyz', $this->facet->getPrefix());
}
public function testSetAndGetLimit()
{
$this->facet->setLimit(12);
$this->assertSame(12, $this->facet->getLimit());
}
public function testSetAndGetOffset()
{
$this->facet->setOffset(40);
$this->assertSame(40, $this->facet->getOffset());
}
public function testSetAndGetMinCount()
{
$this->facet->setMinCount(100);
$this->assertSame(100, $this->facet->getMinCount());
}
public function testSetAndGetMissing()
{
$this->facet->setMissing(true);
$this->assertTrue($this->facet->getMissing());
}
public function testSetAndGetMethod()
{
$this->facet->setMethod('enum');
$this->assertSame('enum', $this->facet->getMethod());
}
public function testSetAndGetRefine()
{
$this->facet->setRefine(true);
$this->assertTrue($this->facet->getRefine());
}
public function testSetAndGetOverRequest()
{
$this->facet->setOverRequest(5);
$this->assertSame(5, $this->facet->getOverRequest());
}
public function testSetAndGetNumBuckets()
{
$this->facet->setNumBuckets(true);
$this->assertTrue($this->facet->getNumBuckets());
}
public function testSetAndGetAllBuckets()
{
$this->facet->setAllBuckets(true);
$this->assertTrue($this->facet->getAllBuckets());
}
}
......@@ -4,6 +4,10 @@ namespace Solarium\Tests\QueryType\Select\Query\Component;
use PHPUnit\Framework\TestCase;
use Solarium\Component\Facet\Field;
use Solarium\Component\Facet\JsonAggregation;
use Solarium\Component\Facet\JsonQuery;
use Solarium\Component\Facet\JsonRange;
use Solarium\Component\Facet\JsonTerms;
use Solarium\Component\Facet\MultiQuery;
use Solarium\Component\Facet\Pivot;
use Solarium\Component\Facet\Query as FacetQuery;
......@@ -343,7 +347,7 @@ class FacetSetTest extends TestCase
$this->assertInstanceOf(Field::class, $result);
$this->assertSame(1, $result->getOption('optionA'));
$this->assertSame(2, $result->getOption('optionB'));
$this->assertSame('id', $result->getOption(FacetSet::FACET_FIELD));
$this->assertSame('id', $result->getOption('field'));
if ($add) {
$this->assertInstanceOf(Field::class, $facetSet->getFacet('key'));
......@@ -364,7 +368,7 @@ class FacetSetTest extends TestCase
$this->assertInstanceOf(FacetQuery::class, $result);
$this->assertSame(1, $result->getOption('optionA'));
$this->assertSame(2, $result->getOption('optionB'));
$this->assertSame('*:*', $result->getOption(FacetSet::FACET_QUERY));
$this->assertSame('*:*', $result->getOption('query'));
if ($add) {
$this->assertInstanceOf(FacetQuery::class, $facetSet->getFacet('key'));
......@@ -433,6 +437,100 @@ class FacetSetTest extends TestCase
}
}
/**
* @dataProvider createFacetAddProvider
*
* @param bool $add
*/
public function testCreateJsonFacetAggregation(bool $add)
{
$options = ['optionA' => 1, 'optionB' => 2, 'key' => 'key'];
$facetSet = new FacetSet([]);
$result = $facetSet->createJsonFacetAggregation($options, $add);
$this->assertInstanceOf(JsonAggregation::class, $result);
$this->assertSame(1, $result->getOption('optionA'));
$this->assertSame(2, $result->getOption('optionB'));
if ($add) {
$this->assertInstanceOf(JsonAggregation::class, $facetSet->getFacet('key'));
} else {
$this->assertEmpty($facetSet->getFacet('key'));
}
}
/**
* @dataProvider createFacetAddProvider
*
* @param bool $add
*/
public function testCreateJsonFacetTerms(bool $add)
{
$options = ['optionA' => 1, 'optionB' => 2, 'key' => 'key'];
$facetSet = new FacetSet([]);
$result = $facetSet->createJsonFacetTerms($options, $add);
$this->assertInstanceOf(JsonTerms::class, $result);
$this->assertSame(1, $result->getOption('optionA'));
$this->assertSame(2, $result->getOption('optionB'));
$this->assertSame('id', $result->getOption('field'));
if ($add) {
$this->assertInstanceOf(JsonTerms::class, $facetSet->getFacet('key'));
} else {
$this->assertEmpty($facetSet->getFacet('key'));
}
}
/**
* @dataProvider createFacetAddProvider
*
* @param bool $add
*/
public function testCreateJsonFacetQuery(bool $add)
{
$options = ['optionA' => 1, 'optionB' => 2, 'key' => 'key'];
$facetSet = new FacetSet([]);
$result = $facetSet->createJsonFacetQuery($options, $add);
$this->assertInstanceOf(JsonQuery::class, $result);
$this->assertSame(1, $result->getOption('optionA'));
$this->assertSame(2, $result->getOption('optionB'));
$this->assertSame('*:*', $result->getOption('q'));
if ($add) {
$this->assertInstanceOf(JsonQuery::class, $facetSet->getFacet('key'));
} else {
$this->assertEmpty($facetSet->getFacet('key'));
}
}
/**
* @dataProvider createFacetAddProvider
*
* @param bool $add
*/
public function testCreateJsonFacetRange(bool $add)
{
$options = ['optionA' => 1, 'optionB' => 2, 'key' => 'key'];
$facetSet = new FacetSet([]);
$result = $facetSet->createJsonFacetRange($options, $add);
$this->assertInstanceOf(JsonRange::class, $result);
$this->assertSame(1, $result->getOption('optionA'));
$this->assertSame(2, $result->getOption('optionB'));
if ($add) {
$this->assertInstanceOf(JsonRange::class, $facetSet->getFacet('key'));
} else {
$this->assertEmpty($facetSet->getFacet('key'));
}
}
public function testSetAndGetExtractFromResponse()
{
$this->facetSet->setExtractFromResponse(true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment