From aef789f06539a071118a9604d0ab42177fa54a60 Mon Sep 17 00:00:00 2001 From: Mantas Jonusas <mantas.jonusas@ongr.io> Date: Mon, 29 Dec 2014 10:01:49 +0200 Subject: [PATCH] DSL Filter Unit Tests Unit DSL Filter Tests --- Filter/AndFilterTest.php | 135 ++++++++++++++++++++++++++++++++++ Filter/ExistsFilterTest.php | 36 +++++++++ Filter/IdsFilterTest.php | 60 +++++++++++++++ Filter/IndicesFilterTest.php | 108 +++++++++++++++++++++++++++ Filter/LimitFilterTest.php | 36 +++++++++ Filter/MatchAllFilterTest.php | 38 ++++++++++ Filter/MissingFilterTest.php | 57 ++++++++++++++ Filter/NotFilterTest.php | 95 ++++++++++++++++++++++++ Filter/OrFilterTest.php | 135 ++++++++++++++++++++++++++++++++++ Filter/PostFilterTest.php | 35 +++++++++ Filter/PrefixFilterTest.php | 61 +++++++++++++++ Filter/QueryFilterTest.php | 67 +++++++++++++++++ Filter/RangeFilterTest.php | 57 ++++++++++++++ Filter/RegexpFilterTest.php | 58 +++++++++++++++ Filter/ScriptFilterTest.php | 59 +++++++++++++++ Filter/TermFilterTest.php | 61 +++++++++++++++ Filter/TermsFilterTest.php | 61 +++++++++++++++ Filter/TypeFilterTest.php | 36 +++++++++ 18 files changed, 1195 insertions(+) create mode 100644 Filter/AndFilterTest.php create mode 100644 Filter/ExistsFilterTest.php create mode 100644 Filter/IdsFilterTest.php create mode 100644 Filter/IndicesFilterTest.php create mode 100644 Filter/LimitFilterTest.php create mode 100644 Filter/MatchAllFilterTest.php create mode 100644 Filter/MissingFilterTest.php create mode 100644 Filter/NotFilterTest.php create mode 100644 Filter/OrFilterTest.php create mode 100644 Filter/PostFilterTest.php create mode 100644 Filter/PrefixFilterTest.php create mode 100644 Filter/QueryFilterTest.php create mode 100644 Filter/RangeFilterTest.php create mode 100644 Filter/RegexpFilterTest.php create mode 100644 Filter/ScriptFilterTest.php create mode 100644 Filter/TermFilterTest.php create mode 100644 Filter/TermsFilterTest.php create mode 100644 Filter/TypeFilterTest.php diff --git a/Filter/AndFilterTest.php b/Filter/AndFilterTest.php new file mode 100644 index 0000000..6767f08 --- /dev/null +++ b/Filter/AndFilterTest.php @@ -0,0 +1,135 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\AndFilter; + +class AndFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new AndFilter('', []); + $result = $filter->getType(); + $this->assertEquals('and', $result); + } + + /** + * Data provider for testToArray function. + * + * @return array + */ + public function getArrayDataProvider() + { + $mockBuildeFfirstFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuildeFfirstFilter->expects($this->any()) + ->method('getType') + ->willReturn('term'); + $mockBuildeFfirstFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + $mockBuilderSecondFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilderSecondFilter->expects($this->any()) + ->method('getType') + ->willReturn('prefix'); + $mockBuilderSecondFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + return [ + // Case #1. + [ + [$mockBuildeFfirstFilter], + [], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + ], + // Case #2. + [ + [$mockBuildeFfirstFilter, $mockBuilderSecondFilter], + [], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 1 => [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + ], + // Case #3. + [ + [$mockBuildeFfirstFilter, $mockBuilderSecondFilter], + ['type' => 'acme'], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 1 => [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + 'type' => 'acme', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param BuilderInterface[] $filters Array. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($filters, $parameters, $expected) + { + $filter = new AndFilter($filters, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/ExistsFilterTest.php b/Filter/ExistsFilterTest.php new file mode 100644 index 0000000..53267d3 --- /dev/null +++ b/Filter/ExistsFilterTest.php @@ -0,0 +1,36 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\ExistsFilter; + +class ExistsFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new ExistsFilter('foo', 'bar'); + $this->assertEquals('exists', $filter->getType()); + } + + /** + * Test for filter toArray() method. + */ + public function testToArray() + { + $filter = new ExistsFilter('foo', 'bar'); + $expectedResult = ['foo' => 'bar']; + $this->assertEquals($expectedResult, $filter->toArray()); + } +} diff --git a/Filter/IdsFilterTest.php b/Filter/IdsFilterTest.php new file mode 100644 index 0000000..e30a272 --- /dev/null +++ b/Filter/IdsFilterTest.php @@ -0,0 +1,60 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\IdsFilter; + +class IdsFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new IdsFilter([], []); + $result = $filter->getType(); + $this->assertEquals('ids', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [['acme', 'bar'], ['type' => 'acme'], ['values' => ['acme', 'bar'], 'type' => 'acme']], + // Case #2. + [[], [], ['values' => []]], + // Case #3. + [['acme'], [], ['values' => ['acme']]], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string[] $values Ids' values. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($values, $parameters, $expected) + { + $filter = new IdsFilter($values, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/IndicesFilterTest.php b/Filter/IndicesFilterTest.php new file mode 100644 index 0000000..5034963 --- /dev/null +++ b/Filter/IndicesFilterTest.php @@ -0,0 +1,108 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\IndicesFilter; + +class IndicesFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new IndicesFilter([], '', null); + $this->assertEquals('indices', $filter->getType()); + } + + /** + * Tests if Indices qty is greater than one. + */ + public function testToArrayIfIndicesQtyIsGreaterThanOne() + { + $mockBuilder = $this->indicesQtyMockBuilder(['test_field' => ['test_value' => 'test']]); + + $filter = new IndicesFilter(['foo', 'bar'], $mockBuilder, null); + $expectedResult = [ + 'indices' => [0 => 'foo', 1 => 'bar'], + 'filter' => ['term' => ['test_field' => ['test_value' => 'test']]], + ]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Test if Indices qty is less than one. + */ + public function testToArrayIfIndicesQtyIsLessThanOne() + { + $mockBuilder = $this->indicesQtyMockBuilder(['test_field' => ['test_value' => 'test']]); + $filter = new IndicesFilter(['foo'], $mockBuilder, null); + $expectedResult = ['index' => 'foo', 'filter' => ['term' => ['test_field' => ['test_value' => 'test']]]]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Test. + */ + public function testWhenNoMatchFilterIsNotNull() + { + $mockBuilder = $this->indicesQtyMockBuilder(['tag' => 'wow']); + $noMatchFilterMockBuilder = $this->indicesQtyMockBuilder(['tag' => 'kow']); + $filter = new IndicesFilter(['foo'], $mockBuilder, $noMatchFilterMockBuilder); + $expectedResult = [ + 'index' => 'foo', + 'filter' => ['term' => ['tag' => 'wow']], + 'no_match_filter' => ['term' => ['tag' => 'kow']], + ]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Test. + */ + public function testWhenNoMatchFilterIsEmpty() + { + $mockBuilder = $this->indicesQtyMockBuilder(['tag' => 'wow']); + $filter = new IndicesFilter(['foo'], $mockBuilder, ''); + $expectedResult = [ + 'index' => 'foo', + 'filter' => ['term' => ['tag' => 'wow']], + 'no_match_filter' => '', + ]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Mock Builder. + * + * @param array $param Expected values. + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + private function indicesQtyMockBuilder(array $param = []) + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('term'); + $mockBuilder->expects($this->any()) + ->method('toArray') + ->willReturn($param); + + return $mockBuilder; + } +} diff --git a/Filter/LimitFilterTest.php b/Filter/LimitFilterTest.php new file mode 100644 index 0000000..9d33e70 --- /dev/null +++ b/Filter/LimitFilterTest.php @@ -0,0 +1,36 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\LimitFilter; + +class LimitFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new LimitFilter(0); + $this->assertEquals('limit', $filter->getType()); + } + + /** + * Test for filter toArray() method. + */ + public function testToArray() + { + $filter = new LimitFilter(0); + $expectedResult = ['value' => 0]; + $this->assertEquals($expectedResult, $filter->toArray()); + } +} diff --git a/Filter/MatchAllFilterTest.php b/Filter/MatchAllFilterTest.php new file mode 100644 index 0000000..5e75aae --- /dev/null +++ b/Filter/MatchAllFilterTest.php @@ -0,0 +1,38 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\MatchAllFilter; + +class MatchAllFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method, it should return 'match_all'. + */ + public function testGetType() + { + $filter = new MatchAllFilter(); + $result = $filter->getType(); + $this->assertEquals('match_all', $result); + } + + /** + * Test toArray method. + */ + public function testToArrayItShouldReturnStdClass() + { + $filter = new MatchAllFilter(); + $result = $filter->toArray(); + $expectedResult = new \stdClass(); + $this->assertEquals($expectedResult, $result); + } +} diff --git a/Filter/MissingFilterTest.php b/Filter/MissingFilterTest.php new file mode 100644 index 0000000..c11fced --- /dev/null +++ b/Filter/MissingFilterTest.php @@ -0,0 +1,57 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter; + +class MissingFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new MissingFilter('', []); + $this->assertEquals('missing', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case 1. + ['', [], ['field' => '']], + // Case 2. + ['user', ['bar' => 'foo'], ['field' => 'user', 'bar' => 'foo']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field + * @param array $parameters + * @param array $expected + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $parameters, $expected) + { + $filter = new MissingFilter($field, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/NotFilterTest.php b/Filter/NotFilterTest.php new file mode 100644 index 0000000..5edf07d --- /dev/null +++ b/Filter/NotFilterTest.php @@ -0,0 +1,95 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\NotFilter; + +class NotFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new NotFilter('', []); + $this->assertEquals('not', $filter->getType()); + } + + /** + * Data provider for testToArray function. + * + * @return array + */ + public function getArrayDataProvider() + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('range'); + $mockBuilder->expects($this->any()) + ->method('toArray') + ->willReturn(['postDate' => ['from' => '2010-03-01', 'to' => '2010-04-01']]); + + return [ + // Case #1. + [ + $mockBuilder, + [], + [ + 'filter' => [ + 'range' => [ + 'postDate' => [ + 'from' => '2010-03-01', + 'to' => '2010-04-01', + ], + ], + ], + ], + ], + // Case #2. + [ + $mockBuilder, + [ + 'type' => 'acme', + ], + [ + 'filter' => [ + 'range' => [ + 'postDate' => [ + 'from' => '2010-03-01', + 'to' => '2010-04-01', + ], + ], + ], + 'type' => 'acme', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param BuilderInterface $filter Filter. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArrayMethod($filter, $parameters, $expected) + { + $filter = new NotFilter($filter, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $filter->toArray()); + } +} diff --git a/Filter/OrFilterTest.php b/Filter/OrFilterTest.php new file mode 100644 index 0000000..44f8117 --- /dev/null +++ b/Filter/OrFilterTest.php @@ -0,0 +1,135 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\OrFilter; + +class OrFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new OrFilter('', []); + $result = $filter->getType(); + $this->assertEquals('or', $result); + } + + /** + * Data provider for testToArray function. + * + * @return array + */ + public function getArrayDataProvider() + { + $mockBuilderFirstFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilderFirstFilter->expects($this->any()) + ->method('getType') + ->willReturn('term'); + $mockBuilderFirstFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + $mockBuilderSecondFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilderSecondFilter->expects($this->any()) + ->method('getType') + ->willReturn('prefix'); + $mockBuilderSecondFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + return [ + // Case #1. + [ + [$mockBuilderFirstFilter], + [], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + ], + // Case #2. + [ + [$mockBuilderFirstFilter, $mockBuilderSecondFilter], + [], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 1 => [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + ], + // Case #3. + [ + [$mockBuilderFirstFilter, $mockBuilderSecondFilter], + ['type' => 'acme'], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 1 => [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + 'type' => 'acme', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param BuilderInterface[] $filters Array. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($filters, $parameters, $expected) + { + $filter = new OrFilter($filters, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/PostFilterTest.php b/Filter/PostFilterTest.php new file mode 100644 index 0000000..4f11f12 --- /dev/null +++ b/Filter/PostFilterTest.php @@ -0,0 +1,35 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter; + +class PostFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testIfGetType() + { + $filter = new PostFilter(); + $this->assertEquals('post_filter', $filter->getType()); + } + + /** + * Test if function is returning False. + */ + public function testIfIsRelevantFunctionIsReturningFalse() + { + $bool = new PostFilter(); + $this->assertFalse($bool->isRelevant()); + } +} diff --git a/Filter/PrefixFilterTest.php b/Filter/PrefixFilterTest.php new file mode 100644 index 0000000..afd4651 --- /dev/null +++ b/Filter/PrefixFilterTest.php @@ -0,0 +1,61 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\PrefixFilter; + +class PrefixFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new PrefixFilter('', '', []); + $result = $filter->getType(); + $this->assertEquals('prefix', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', '', [], ['' => '']], + // Case #2. + ['prefix', 'foo', [], ['prefix' => 'foo']], + // Case #3. + ['prefix', 'foo', ['type' => 'acme'], ['prefix' => 'foo', 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param string $value Field value. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $value, $parameters, $expected) + { + $filter = new PrefixFilter($field, $value, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/QueryFilterTest.php b/Filter/QueryFilterTest.php new file mode 100644 index 0000000..0a5909a --- /dev/null +++ b/Filter/QueryFilterTest.php @@ -0,0 +1,67 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\QueryFilter; + +class QueryFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test query with '_cache' parameter. + */ + public function testToArrayWithGetTypeFqueryWithCache() + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('fquery'); + $filter = new QueryFilter($mockBuilder, ['_cache' => true]); + $result = $filter->toArray(); + $expectedResult = ['query' => ['fquery' => null], '_cache' => true]; + $this->assertEquals($expectedResult, $result); + } + + /** + * Test query without '_cache' parameter. + */ + public function testToArrayWithGetTypeQueryWithoutCache() + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('query'); + $filter = new QueryFilter($mockBuilder, []); + $result = $filter->toArray(); + $expectedResult = ['query' => null]; + $this->assertEquals($expectedResult, $result); + } + + /** + * Test GetType function, returns 'fquery'. + */ + public function testGetTypeWhenReturnsStringFquery() + { + $filter = new QueryFilter('', ['_cache' => true]); + $this->assertEquals('fquery', $filter->getType()); + } + + /** + * Test GetType function, returns 'query'. + */ + public function testgetTypeWhenReturnsStringQuery() + { + $filter = new QueryFilter('', []); + $this->assertEquals('query', $filter->getType()); + } +} diff --git a/Filter/RangeFilterTest.php b/Filter/RangeFilterTest.php new file mode 100644 index 0000000..5a62625 --- /dev/null +++ b/Filter/RangeFilterTest.php @@ -0,0 +1,57 @@ +<?php +/** + * Created by PhpStorm. + * User: chyzas + * Date: 14.12.22 + * Time: 19.35 + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\RangeFilter; + +class RangeFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new RangeFilter('', [], []); + $this->assertEquals('range', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', [], [], ['' => []]], + // Case #2. + ['foo', [1, 5], [], ['foo' => [0 => 1, 1 => 5]]], + // Case #3. + ['test', ['foo', 'bar'], ['type' => 'acme'], ['test' => [0 => 'foo', 1 => 'bar'], 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param array $range Range values. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $range, $parameters, $expected) + { + $filter = new RangeFilter($field, $range, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/RegexpFilterTest.php b/Filter/RegexpFilterTest.php new file mode 100644 index 0000000..b54eede --- /dev/null +++ b/Filter/RegexpFilterTest.php @@ -0,0 +1,58 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\RegexpFilter; + +class RegexpFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new RegexpFilter('', '\w', []); + $this->assertEquals('regexp', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', '\w', [], ['' => ['value' => '\w']]], + // Case #2. + ['regexp', '\w', ['flags' => 'foo'], ['regexp' => ['value' => '\w', 'flags' => 'foo']]], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param string $regexp Regular expression. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $regexp, $parameters, $expected) + { + $filter = new RegexpFilter($field, $regexp, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/ScriptFilterTest.php b/Filter/ScriptFilterTest.php new file mode 100644 index 0000000..bcbdf68 --- /dev/null +++ b/Filter/ScriptFilterTest.php @@ -0,0 +1,59 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\ScriptFilter; + +class ScriptFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new ScriptFilter(''); + $this->assertEquals('script', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', [], ['script' => '']], + // Case #2. + ['foo', [], ['script' => 'foo']], + // Case #3. + ['foo', ['type' => 'acme'], ['script' => 'foo', 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $script Script. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($script, $parameters, $expected) + { + $filter = new ScriptFilter($script, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/TermFilterTest.php b/Filter/TermFilterTest.php new file mode 100644 index 0000000..11af22b --- /dev/null +++ b/Filter/TermFilterTest.php @@ -0,0 +1,61 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\TermFilter; + +class TermFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new TermFilter('', '', []); + $result = $filter->getType(); + $this->assertEquals('term', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', '', [], ['' => '']], + // Case #2. + ['term', 'foo', [], ['term' => 'foo']], + // Case #3. + ['term', 'foo', ['type' => 'acme'], ['term' => 'foo', 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param string $term Field value. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $term, $parameters, $expected) + { + $filter = new TermFilter($field, $term, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/TermsFilterTest.php b/Filter/TermsFilterTest.php new file mode 100644 index 0000000..e9f7cdf --- /dev/null +++ b/Filter/TermsFilterTest.php @@ -0,0 +1,61 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\TermsFilter; + +class TermsFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new TermsFilter('', [], []); + $result = $filter->getType(); + $this->assertEquals('terms', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', [], [], ['' => []]], + // Case #2. + ['tags', ['foo', 'bar'], [], ['tags' => [0 => 'foo', 1 => 'bar']]], + // Case #3. + ['tags', ['foo', 'bar'], ['type' => 'acme'], ['tags' => [0 => 'foo', 1 => 'bar'], 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param array $terms An array of terms. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $terms, $parameters, $expected) + { + $filter = new TermsFilter($field, $terms, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/Filter/TypeFilterTest.php b/Filter/TypeFilterTest.php new file mode 100644 index 0000000..99bbf0a --- /dev/null +++ b/Filter/TypeFilterTest.php @@ -0,0 +1,36 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\TypeFilter; + +class TypeFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new TypeFilter(''); + $this->assertEquals('type', $filter->getType()); + } + + /** + * Test for filter toArray() method. + */ + public function testToArray() + { + $filter = new TypeFilter('foo'); + $expectedResult = ['value' => 'foo']; + $this->assertEquals($expectedResult, $filter->toArray()); + } +} -- GitLab