class SearchApiGranularTest in Facets 8
Unit test for granular query type.
@group facets
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\facets\Unit\Plugin\query_type\SearchApiGranularTest
Expanded class hierarchy of SearchApiGranularTest
File
- tests/
src/ Unit/ Plugin/ query_type/ SearchApiGranularTest.php, line 24
Namespace
Drupal\Tests\facets\Unit\Plugin\query_typeView source
class SearchApiGranularTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$processor_id = 'granularity_item';
$processor_definitions = [
$processor_id => [
'id' => $processor_id,
'class' => GranularItemProcessor::class,
],
];
$granularityProcessor = new GranularItemProcessor([
'granularity' => 10,
], 'granularity_item', []);
$processor_manager = $this
->prophesize(ProcessorPluginManager::class);
$processor_manager
->getDefinitions()
->willReturn($processor_definitions);
$processor_manager
->createInstance('granularity_item', Argument::any())
->willReturn($granularityProcessor);
$container = new ContainerBuilder();
$container
->set('plugin.manager.facets.processor', $processor_manager
->reveal());
\Drupal::setContainer($container);
}
/**
* Tests string query type without executing the query with an "AND" operator.
*/
public function testQueryTypeAnd() {
$backend = $this
->prophesize(BackendInterface::class);
$backend
->getSupportedFeatures()
->willReturn([]);
$server = $this
->prophesize(ServerInterface::class);
$server
->getBackend()
->willReturn($backend);
$index = $this
->prophesize(IndexInterface::class);
$index
->getServerInstance()
->willReturn($server);
$query = $this
->prophesize(SearchApiQuery::class);
$query
->getIndex()
->willReturn($index);
$facet = new Facet([
'query_operator' => 'AND',
'widget' => 'links',
], 'facets_facet');
$facet
->addProcessor([
'processor_id' => 'granularity_item',
'weights' => [],
'settings' => [
'granularity' => 10,
],
]);
// Results for the widget.
$original_results = [
[
'count' => 3,
'filter' => '2',
],
[
'count' => 5,
'filter' => '4',
],
[
'count' => 7,
'filter' => '9',
],
[
'count' => 9,
'filter' => '11',
],
];
// Facets the widget should produce.
$grouped_results = [
0 => [
'count' => 15,
'filter' => '0',
],
10 => [
'count' => 9,
'filter' => 10,
],
];
$query_type = new SearchApiGranular([
'facet' => $facet,
'query' => $query
->reveal(),
'results' => $original_results,
], 'search_api_string', []);
$built_facet = $query_type
->build();
$this
->assertInstanceOf(FacetInterface::class, $built_facet);
$results = $built_facet
->getResults();
$this
->assertSame('array', gettype($results));
foreach ($grouped_results as $k => $result) {
$this
->assertInstanceOf(ResultInterface::class, $results[$k]);
$this
->assertEquals($result['count'], $results[$k]
->getCount());
$this
->assertEquals($result['filter'], $results[$k]
->getDisplayValue());
}
}
/**
* Tests string query type without results.
*/
public function testEmptyResults() {
$query = new SearchApiQuery([], 'search_api_query', []);
$facet = new Facet([], 'facets_facet');
$query_type = new SearchApiGranular([
'facet' => $facet,
'query' => $query,
], 'search_api_string', []);
$built_facet = $query_type
->build();
$this
->assertInstanceOf(FacetInterface::class, $built_facet);
$results = $built_facet
->getResults();
$this
->assertSame('array', gettype($results));
$this
->assertEmpty($results);
}
/**
* Tests the calculateResultFilter method.
*
* @dataProvider provideDataForCalculateResultFilter
*/
public function testCalculateResultFilter($input, $expected_result) {
$query = new SearchApiQuery([], 'search_api_query', []);
$facet = new Facet([
'query_operator' => 'AND',
'widget' => 'links',
], 'facets_facet');
$facet
->addProcessor([
'processor_id' => 'granularity_item',
'weights' => [],
'settings' => [],
]);
$facet
->getProcessors()['granularity_item']
->setConfiguration([
'granularity' => 3,
'min_value' => 5,
'max_value' => 15,
]);
$query_type = new SearchApiGranular([
'facet' => $facet,
'query' => $query,
], 'search_api_string', []);
$result = $query_type
->calculateResultFilter($input);
$this
->assertSame($expected_result, $result);
}
/**
* Provides testdata.
*
* @return array
* Test data.
*/
public function provideDataForCalculateResultFilter() {
return [
'normal' => [
10,
[
'display' => 8.0,
'raw' => 8.0,
],
],
'under_min' => [
4,
FALSE,
],
'over_max' => [
20,
FALSE,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SearchApiGranularTest:: |
public | function | Provides testdata. | |
SearchApiGranularTest:: |
public | function |
Overrides UnitTestCase:: |
|
SearchApiGranularTest:: |
public | function | Tests the calculateResultFilter method. | |
SearchApiGranularTest:: |
public | function | Tests string query type without results. | |
SearchApiGranularTest:: |
public | function | Tests string query type without executing the query with an "AND" operator. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |