diff --git a/Filter/AndFilterTest.php b/Filter/AndFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6767f08adff9139c6a6320aef23ac5fc871ac3f1 --- /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 0000000000000000000000000000000000000000..53267d300e76570023c37caa19d62191f8670d99 --- /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 0000000000000000000000000000000000000000..e30a272f2eff9051480f4a5bd867e7e8c14a9ae6 --- /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 0000000000000000000000000000000000000000..503496345864244681f2d66966fefb4bd82afb35 --- /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 0000000000000000000000000000000000000000..9d33e70eb8dad205ef20a4ba0cb8baaf4cdbd50f --- /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 0000000000000000000000000000000000000000..5e75aae76853b2a8414215479089d33e96fe559f --- /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 0000000000000000000000000000000000000000..c11fced3b4c464f1e2362fb223f6fe4d63b3e8c6 --- /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 0000000000000000000000000000000000000000..5edf07d7a50270227073d98c3cda6a0e73c56511 --- /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 0000000000000000000000000000000000000000..44f8117d335ad871164dcc31dcbe373d9df9e9d7 --- /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 0000000000000000000000000000000000000000..4f11f12db554c829129992a4745a25e04fa6b1a3 --- /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 0000000000000000000000000000000000000000..afd46515cce974eba0c4add07bf309d42b8fde51 --- /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 0000000000000000000000000000000000000000..0a5909af6ab51f60ef137be0b281118e3130f2dc --- /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 0000000000000000000000000000000000000000..5a62625e4412288770a0a655fc70b2118ee9da6e --- /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 0000000000000000000000000000000000000000..b54eede9cce7ba61fa733ce2a130603f3ae0479d --- /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 0000000000000000000000000000000000000000..bcbdf68a711b11a2dc00b8f7dd4b467d947d6635 --- /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 0000000000000000000000000000000000000000..11af22b3cebd9da5c2cdba3140d3edb79863f831 --- /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 0000000000000000000000000000000000000000..e9f7cdf0ff6d4fe7b345e141746ebe79414abd0f --- /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 0000000000000000000000000000000000000000..99bbf0aefad40d3bd53a67b852294a28c697cb29 --- /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()); + } +}