class FilterFactoryTest in Elasticsearch Connector 8.5
Same name and namespace in other branches
- 8.7 tests/src/Unit/ElasticSearch/Parameters/Factory/FilterFactoryTest.php \Drupal\Tests\elasticsearch_connector\Unit\ElasticSearch\Parameters\Factory\FilterFactoryTest
- 8.6 tests/src/Unit/ElasticSearch/Parameters/Factory/FilterFactoryTest.php \Drupal\Tests\elasticsearch_connector\Unit\ElasticSearch\Parameters\Factory\FilterFactoryTest
This test is long because it tests a long method. It just repeats the same pattern over and over where a condition is mocked to test each of the ramifications.
@coversDefaultClass \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\FilterFactory
@group elasticsearch_connector
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\elasticsearch_connector\Unit\ElasticSearch\Parameters\Factory\FilterFactoryTest
Expanded class hierarchy of FilterFactoryTest
File
- tests/
src/ Unit/ ElasticSearch/ Parameters/ Factory/ FilterFactoryTest.php, line 18
Namespace
Drupal\Tests\elasticsearch_connector\Unit\ElasticSearch\Parameters\FactoryView source
class FilterFactoryTest extends UnitTestCase {
/**
* @covers ::filterFromCondition
*/
public function testFilterFromConditionA() {
// Empty and not empty operators.
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(FALSE);
$condition
->getOperator()
->willReturn('<>');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'exists' => [
'field' => 'foo',
],
];
$this
->assertEquals($expected_filter, $filter);
// Thest the = operator.
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(FALSE);
$condition
->getOperator()
->willReturn('=');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'bool' => [
'must_not' => [
'exists' => [
'field' => 'foo',
],
],
],
];
$this
->assertEquals($expected_filter, $filter);
// Other operators will throw an exception.
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(FALSE);
$condition
->getOperator()
->willReturn('>');
$condition
->getField()
->willReturn('foo');
$this
->setExpectedException(\Exception::class, 'Incorrect filter criteria');
FilterFactory::filterFromCondition($condition
->reveal());
}
/**
* @covers ::filterFromCondition
*/
public function testFilterFromConditionB() {
// Normal filters
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn('bar');
$condition
->getOperator()
->willReturn('=');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'term' => [
'foo' => 'bar',
],
];
$this
->assertEquals($expected_filter, $filter);
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn([
'bar',
'baz',
]);
$condition
->getOperator()
->willReturn('IN');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'terms' => [
'foo' => [
'bar',
'baz',
],
],
];
$this
->assertEquals($expected_filter, $filter);
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn('bar');
$condition
->getOperator()
->willReturn('<>');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'not' => [
'filter' => [
'term' => [
'foo' => 'bar',
],
],
],
];
$this
->assertEquals($expected_filter, $filter);
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(1);
$condition
->getOperator()
->willReturn('>');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'range' => [
'foo' => [
'from' => 1,
'to' => NULL,
'include_lower' => FALSE,
'include_upper' => FALSE,
],
],
];
$this
->assertEquals($expected_filter, $filter);
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(1);
$condition
->getOperator()
->willReturn('>=');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'range' => [
'foo' => [
'from' => 1,
'to' => NULL,
'include_lower' => TRUE,
'include_upper' => FALSE,
],
],
];
$this
->assertEquals($expected_filter, $filter);
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(1);
$condition
->getOperator()
->willReturn('<');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'range' => [
'foo' => [
'from' => NULL,
'to' => 1,
'include_lower' => FALSE,
'include_upper' => FALSE,
],
],
];
$this
->assertEquals($expected_filter, $filter);
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(1);
$condition
->getOperator()
->willReturn('<=');
$condition
->getField()
->willReturn('foo');
$filter = FilterFactory::filterFromCondition($condition
->reveal());
$expected_filter = [
'range' => [
'foo' => [
'from' => NULL,
'to' => 1,
'include_lower' => FALSE,
'include_upper' => TRUE,
],
],
];
$this
->assertEquals($expected_filter, $filter);
// Other operators will throw an exception.
/** @var \Prophecy\Prophecy\ObjectProphecy $condition */
$condition = $this
->prophesize(Condition::class);
$condition
->getValue()
->willReturn(FALSE);
$condition
->getOperator()
->willReturn('other-operator');
$condition
->getField()
->willReturn('foo');
$this
->setExpectedException(\Exception::class, 'Incorrect filter criteria');
FilterFactory::filterFromCondition($condition
->reveal());
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FilterFactoryTest:: |
public | function | @covers ::filterFromCondition | |
FilterFactoryTest:: |
public | function | @covers ::filterFromCondition | |
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. | |
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. | |
UnitTestCase:: |
protected | function | 340 |