diff --git a/tests/Aggregation/FilterAggregationTest.php b/tests/Aggregation/FilterAggregationTest.php index 753551c8de0c67c6c20b327d7b0494675505e7ca..64cf4ac7cbe6957c329d0f80c86c147f2f7203f5 100644 --- a/tests/Aggregation/FilterAggregationTest.php +++ b/tests/Aggregation/FilterAggregationTest.php @@ -42,7 +42,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setFilter($filter); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'filter' => [ 'test_filter' => [ 'test_field' => ['test_value' => 'test'], @@ -66,22 +66,22 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase ->getMockForAbstractClass(); $aggregation2->expects($this->any()) ->method('toArray') - ->willReturn(['agg_test_agg2' => ['avg' => []]]); + ->willReturn(['test_agg2' => ['avg' => []]]); $aggregation2->expects($this->any()) ->method('getName') - ->willReturn('agg_test_agg2'); + ->willReturn('test_agg2'); $aggregation->addAggregation($aggregation2); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'filter' => [ 'test_filter' => [ 'test_field' => ['test_value' => 'test'], ], ], 'aggregations' => [ - 'agg_test_agg2' => [ + 'test_agg2' => [ 'avg' => [], ], ], @@ -154,7 +154,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new FilterAggregation('test', $builderInterface); $this->assertSame( [ - 'agg_test' => [ + 'test' => [ 'filter' => [null => null], ], ], diff --git a/tests/Aggregation/FiltersAggregationTest.php b/tests/Aggregation/FiltersAggregationTest.php index af8c21f149c63ede67bc4be8dcaef1fd7036f8ac..667fbbde1a9e5683cfcd5a7dacdc6a5c11b5ed83 100644 --- a/tests/Aggregation/FiltersAggregationTest.php +++ b/tests/Aggregation/FiltersAggregationTest.php @@ -75,7 +75,7 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addFilter($filter, 'second'); $results = $aggregation->toArray(); $expected = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'filters' => [ 'filters' => [ 'first' => [ @@ -121,7 +121,7 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $this->assertSame( [ - 'agg_test' => [ + 'test' => [ 'filters' => [ 'filters' => [ 'filter1' => ['type1' => null], @@ -144,7 +144,7 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $this->assertSame( [ - 'agg_test' => [ + 'test' => [ 'filters' => [ 'filters' => [ ['type1' => null], diff --git a/tests/Aggregation/GeoBoundsAggregationTest.php b/tests/Aggregation/GeoBoundsAggregationTest.php index d38aa25fd3151a5a50fee6dbed83a36526ca2e26..8d8e258b0c0e1ca181e22fc01e0cf0e1cea82372 100644 --- a/tests/Aggregation/GeoBoundsAggregationTest.php +++ b/tests/Aggregation/GeoBoundsAggregationTest.php @@ -48,7 +48,7 @@ class GeoBoundsAggregationTest extends \PHPUnit_Framework_TestCase $agg->setField('bar'); $agg->setWrapLongitude(true); $result = [ - 'agg_foo' => [ + 'foo' => [ 'geo_bounds' => [ 'field' => 'bar', 'wrap_longitude' => true, @@ -59,7 +59,7 @@ class GeoBoundsAggregationTest extends \PHPUnit_Framework_TestCase $agg->setWrapLongitude(false); $result = [ - 'agg_foo' => [ + 'foo' => [ 'geo_bounds' => [ 'field' => 'bar', 'wrap_longitude' => false, diff --git a/tests/Aggregation/GeoDistanceAggregationTest.php b/tests/Aggregation/GeoDistanceAggregationTest.php index 19ab6994c388e75d5cff3090665d2a64855f4fa8..a610f7b00cf3e0f741e9002bc17f32592f629c34 100644 --- a/tests/Aggregation/GeoDistanceAggregationTest.php +++ b/tests/Aggregation/GeoDistanceAggregationTest.php @@ -133,7 +133,7 @@ class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase $this->assertSame( [ - 'agg_test' => [ + 'test' => [ 'geo_distance' => [ 'field' => 'fieldName', 'origin' => 'originValue', diff --git a/tests/Aggregation/GlobalAggregationTest.php b/tests/Aggregation/GlobalAggregationTest.php index 49ae84b47b5a6f621bb33d216cf2cc5662da2719..b425af00d1de5ebfb38dba4b30df2d9b83cf74d2 100644 --- a/tests/Aggregation/GlobalAggregationTest.php +++ b/tests/Aggregation/GlobalAggregationTest.php @@ -28,7 +28,7 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new GlobalAggregation('test_agg'); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'global' => new \stdClass(), ], ]; @@ -44,10 +44,10 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($aggregation2); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'global' => new \stdClass(), 'aggregations' => [ - 'agg_test_agg_2' => [ + 'test_agg_2' => [ 'global' => new \stdClass(), ], ], diff --git a/tests/Aggregation/Ipv4RangeAggregationTest.php b/tests/Aggregation/Ipv4RangeAggregationTest.php index 7542584b5e035a454026dfc9d5450ef719af75cf..3f33dd17b2a73a5b1da82901ce31fe6686fc517c 100644 --- a/tests/Aggregation/Ipv4RangeAggregationTest.php +++ b/tests/Aggregation/Ipv4RangeAggregationTest.php @@ -34,7 +34,7 @@ class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new Ipv4RangeAggregation('test', 'fieldName', [['from' => 'fromValue']]); $this->assertSame( [ - 'agg_test' => [ + 'test' => [ 'ip_range' => [ 'field' => 'fieldName', 'ranges' => [['from' => 'fromValue']], @@ -47,7 +47,7 @@ class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new Ipv4RangeAggregation('test', 'fieldName', ['maskValue']); $this->assertSame( [ - 'agg_test' => [ + 'test' => [ 'ip_range' => [ 'field' => 'fieldName', 'ranges' => [['mask' => 'maskValue']], diff --git a/tests/Aggregation/NestedAggregationTest.php b/tests/Aggregation/NestedAggregationTest.php index 941060d162ea524eb178cb77b386d1fbe0d35a2b..166a5c914fad122df024a91ac6db34e07c906a1f 100644 --- a/tests/Aggregation/NestedAggregationTest.php +++ b/tests/Aggregation/NestedAggregationTest.php @@ -26,7 +26,7 @@ class NestedAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setPath('test_path'); $expectedResult = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'nested' => ['path' => 'test_path'], ], ]; @@ -54,7 +54,7 @@ class NestedAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($termMock); $expectedResult = [ - 'agg_test_nested_agg' => [ + 'test_nested_agg' => [ 'nested' => ['path' => 'test_path'], 'aggregations' => [ 'terms' => [], diff --git a/tests/Aggregation/PercentileRanksAggregationTest.php b/tests/Aggregation/PercentileRanksAggregationTest.php index 94ce741f69db060974bc22fc50eff59d94e285f7..b96f21637fbe3aafca57cb7974295d8e41ca7caf 100644 --- a/tests/Aggregation/PercentileRanksAggregationTest.php +++ b/tests/Aggregation/PercentileRanksAggregationTest.php @@ -80,7 +80,7 @@ class PercentileRanksAggregationTest extends \PHPUnit_Framework_TestCase $this->agg->setValues(['bar']); $this->assertSame( [ - 'agg_foo' => [ + 'foo' => [ 'percentile_ranks' => [ 'field' => 'bar', 'values' => ['bar'], diff --git a/tests/Aggregation/RangeAggregationTest.php b/tests/Aggregation/RangeAggregationTest.php index c66244d0811bdfe58b7ac11e09ad6d84b41a25c9..5debcce7a7506fa8259958bfcfb2ee0fb74807d8 100644 --- a/tests/Aggregation/RangeAggregationTest.php +++ b/tests/Aggregation/RangeAggregationTest.php @@ -25,7 +25,7 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addRange('10', 20); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'range' => [ 'field' => 'test_field', 'ranges' => [ @@ -54,7 +54,7 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addRange(null, '20', 'range_2'); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'range' => [ 'field' => 'test_field', 'ranges' => [ @@ -90,7 +90,7 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($aggregation2); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'range' => [ 'field' => 'test_field', 'ranges' => [ @@ -102,7 +102,7 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase 'keyed' => false, ], 'aggregations' => [ - 'agg_test_agg_2' => [ + 'test_agg_2' => [ 'range' => [ 'ranges' => [ [ @@ -208,7 +208,7 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new RangeAggregation('foo', 'fieldValue', [['from' => 'now', 'key' => 'nowkey']], true); $this->assertSame( [ - 'agg_foo' => [ + 'foo' => [ 'range' => [ 'keyed' => true, 'ranges' => [ diff --git a/tests/Aggregation/ReverseNestedAggregationTest.php b/tests/Aggregation/ReverseNestedAggregationTest.php index 2b401ec06d6b3cd31c8920440c8b42e966f2e6e1..6cb8caa5fe26de2ec58a34121c9903c20dd022d1 100644 --- a/tests/Aggregation/ReverseNestedAggregationTest.php +++ b/tests/Aggregation/ReverseNestedAggregationTest.php @@ -26,7 +26,7 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setPath('test_path'); $expectedResult = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'reverse_nested' => ['path' => 'test_path'], ], ]; @@ -54,7 +54,7 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($termMock); $expectedResult = [ - 'agg_test_nested_agg' => [ + 'test_nested_agg' => [ 'reverse_nested' => ['path' => 'test_path'], 'aggregations' => [ 'terms' => [], @@ -84,7 +84,7 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addAggregation($termMock); $expectedResult = [ - 'agg_test_nested_agg' => [ + 'test_nested_agg' => [ 'reverse_nested' => new \stdClass(), 'aggregations' => [ 'terms' => [], diff --git a/tests/Aggregation/StatsAggregationTest.php b/tests/Aggregation/StatsAggregationTest.php index f826b397310bbb89a08c4cfda0155737f420b8b3..21b6cfc3791e28315166fad3bcb18a61f26997aa 100644 --- a/tests/Aggregation/StatsAggregationTest.php +++ b/tests/Aggregation/StatsAggregationTest.php @@ -24,7 +24,7 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setField('test_field'); $expectedResult = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'stats' => ['field' => 'test_field'], ], ]; @@ -40,7 +40,7 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue'); $this->assertSame( [ - 'agg_foo' => [ + 'foo' => [ 'stats' => [ 'field' => 'fieldValue', 'script' => 'scriptValue', diff --git a/tests/Aggregation/TermsAggregationTest.php b/tests/Aggregation/TermsAggregationTest.php index c96ebfe0d4d249873de28cb4adb62e591e70fb76..41df7ec359d18e5ebdc5033ffee3af4f6ea6e2ce 100644 --- a/tests/Aggregation/TermsAggregationTest.php +++ b/tests/Aggregation/TermsAggregationTest.php @@ -25,7 +25,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->setField('test_field'); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => ['field' => 'test_field'], ], ]; @@ -44,7 +44,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('size', 1); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'size' => 1, @@ -60,7 +60,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('size', 0); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'size' => 0, @@ -83,7 +83,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('min_doc_count', 10); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'size' => 1, @@ -107,7 +107,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('exclude', 'pizza_.*'); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'include' => 'test_.*', @@ -143,7 +143,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'include' => [ @@ -172,7 +172,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('order', ['_count' => 'asc']); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'order' => ['_count' => 'asc'], @@ -194,7 +194,7 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase $aggregation->addParameter('order', ['_term' => 'desc']); $result = [ - 'agg_test_agg' => [ + 'test_agg' => [ 'terms' => [ 'field' => 'test_field', 'order' => ['_term' => 'desc'], diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php index a1975eb647aa3b80436fa30128eadda20b816ad9..cd63b502d2b0932580cc935b5e917ce02cb467e9 100644 --- a/tests/Aggregation/TopHitsAggregationTest.php +++ b/tests/Aggregation/TopHitsAggregationTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\TopHitsAggregation; +use ONGR\ElasticsearchDSL\Sort\FieldSort; use ONGR\ElasticsearchDSL\Sort\Sorts; /** @@ -24,15 +25,15 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase */ public function testToArray() { - $sorts = new Sorts(); - $aggregation = new TopHitsAggregation('test', 0, 1); + $sort = new FieldSort('acme'); + $aggregation = new TopHitsAggregation('acme', 0, 1, $sort); $expectedAgg = new \stdClass(); $expectedAgg->size = 0; $expectedAgg->from = 1; - $expectedAgg->sort = $sorts->toArray(); + $expectedAgg->sort = $sort->toArray(); $expected = [ - 'agg_test' => [ + 'acme' => [ 'top_hits' => $expectedAgg, ], ]; diff --git a/tests/NamedBuilderBagTest.php b/tests/BuilderBagTest.php similarity index 54% rename from tests/NamedBuilderBagTest.php rename to tests/BuilderBagTest.php index 8dd7d9874394dd5d72df0101565319fc217c751a..b2f7a9569c134b55e377f131c447e598a1508611 100644 --- a/tests/NamedBuilderBagTest.php +++ b/tests/BuilderBagTest.php @@ -11,22 +11,20 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL; -use ONGR\ElasticsearchDSL\NamedBuilderBag; -use ONGR\ElasticsearchDSL\NamedBuilderInterface; +use ONGR\ElasticsearchDSL\BuilderBag; +use ONGR\ElasticsearchDSL\BuilderInterface; -class NamedBuilderBagTest extends \PHPUnit_Framework_TestCase +class BuilderBagTest extends \PHPUnit_Framework_TestCase { /** * Tests if bag knows if he has a builder. */ public function testHas() { - $bag = new NamedBuilderBag( - [ - $this->getBuilder('foo'), - ] - ); - $this->assertTrue($bag->has('foo')); + $bag = new BuilderBag(); + $fooBuilder = $this->getBuilder('foo'); + $builderName = $bag->add($fooBuilder); + $this->assertTrue($bag->has($builderName)); } /** @@ -34,17 +32,18 @@ class NamedBuilderBagTest extends \PHPUnit_Framework_TestCase */ public function testRemove() { - $bag = new NamedBuilderBag( - [ - $this->getBuilder('foo'), - $this->getBuilder('baz'), - ] - ); - $bag->remove('foo'); + $bag = new BuilderBag(); + $fooBuilder = $this->getBuilder('foo'); + $acmeBuilder = $this->getBuilder('acme'); + $fooBuilderName = $bag->add($fooBuilder); + $acmeBuilderName = $bag->add($acmeBuilder); + + $bag->remove($fooBuilderName); - $this->assertFalse($bag->has('foo'), 'Foo builder should not exist anymore.'); - $this->assertTrue($bag->has('baz'), 'Baz builder should exist.'); + $this->assertFalse($bag->has($fooBuilderName), 'Foo builder should not exist anymore.'); + $this->assertTrue($bag->has($acmeBuilderName), 'Acme builder should exist.'); + $this->assertCount(1, $bag->all()); } /** @@ -52,7 +51,7 @@ class NamedBuilderBagTest extends \PHPUnit_Framework_TestCase */ public function testClear() { - $bag = new NamedBuilderBag( + $bag = new BuilderBag( [ $this->getBuilder('foo'), $this->getBuilder('baz'), @@ -69,13 +68,11 @@ class NamedBuilderBagTest extends \PHPUnit_Framework_TestCase */ public function testGet() { - $bag = new NamedBuilderBag( - [ - $this->getBuilder('baz'), - ] - ); + $bag = new BuilderBag(); + $bazBuilder = $this->getBuilder('baz'); + $builderName = $bag->add($bazBuilder); - $this->assertNotEmpty($bag->get('baz')); + $this->assertNotEmpty($bag->get($builderName)); } /** @@ -83,14 +80,14 @@ class NamedBuilderBagTest extends \PHPUnit_Framework_TestCase * * @param string $name * - * @return \PHPUnit_Framework_MockObject_MockObject|NamedBuilderInterface + * @return \PHPUnit_Framework_MockObject_MockObject|BuilderInterface */ private function getBuilder($name) { - $friendlyBuilderMock = $this->getMock('ONGR\ElasticsearchDSL\NamedBuilderInterface'); + $friendlyBuilderMock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface'); $friendlyBuilderMock - ->expects($this->once()) + ->expects($this->any()) ->method('getName') ->will($this->returnValue($name)); diff --git a/tests/Filter/PostFilterTest.php b/tests/Filter/PostFilterTest.php deleted file mode 100644 index ecc354d26b649e9e85d1b347338b4e22c056c62b..0000000000000000000000000000000000000000 --- a/tests/Filter/PostFilterTest.php +++ /dev/null @@ -1,57 +0,0 @@ -<?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\ElasticsearchDSL\Tests\Unit\DSL\Filter; - -use ONGR\ElasticsearchDSL\Filter\PostFilter; - -class PostFilterTest extends \PHPUnit_Framework_TestCase -{ - /** - * Tests GetType method. - */ - public function testIfGetType() - { - $postFilter = new PostFilter(); - $this->assertEquals('post_filter', $postFilter->getType()); - } - - /** - * Test if function is returning False. - */ - public function testIfIsRelevantFunctionIsReturningFalse() - { - $postFilter = new PostFilter(); - $this->assertFalse($postFilter->isRelevant()); - } - - /** - * Test addFilter method. - */ - public function testAddFilter() - { - $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter') - ->disableOriginalConstructor() - ->getMock(); - $missingFilterMock - ->expects($this->once()) - ->method('toArray') - ->willReturn([]); - $missingFilterMock - ->expects($this->once()) - ->method('getType') - ->willReturn('test_type'); - - $postFilter = new PostFilter(); - $postFilter->setFilter($missingFilterMock); - $this->assertEquals(['test_type' => []], $postFilter->toArray()); - } -} diff --git a/tests/Highlight/FieldTest.php b/tests/Highlight/FieldTest.php deleted file mode 100644 index 26d58983c7dab75eea5db8cb9253e467c0527815..0000000000000000000000000000000000000000 --- a/tests/Highlight/FieldTest.php +++ /dev/null @@ -1,80 +0,0 @@ -<?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\ElasticsearchDSL\Tests\Unit\DSL\Highlight; - -use ONGR\ElasticsearchDSL\Filter\TermFilter; -use ONGR\ElasticsearchDSL\Highlight\Field; - -/** - * Unit test for Field. - */ -class FieldTest extends \PHPUnit_Framework_TestCase -{ - /** - * Tests getType method. - */ - public function testGetType() - { - $field = new Field('test'); - - $field->setHighlighterType(Field::TYPE_FVH); - $this->assertEquals(Field::TYPE_FVH, $field->getType()); - - $field->setHighlighterType(Field::TYPE_PLAIN); - $this->assertEquals(Field::TYPE_PLAIN, $field->getType()); - - $field->setHighlighterType(Field::TYPE_POSTINGS); - $this->assertEquals(Field::TYPE_POSTINGS, $field->getType()); - - $initValue = $field->getType(); - - $field->setHighlighterType('wrongValue'); - $this->assertEquals($initValue, $field->getType()); - } - - /** - * Tests toArray method. - */ - public function testFieldToArray() - { - $field = new Field('test'); - $field->setFragmentSize(5); - $field->setNumberOfFragments(5); - $field->setHighlightQuery(new TermFilter('key1', 'value1')); - $field->setNoMatchSize(3); - $field->setForceSource(true); - - $result = [ - 'fragment_size' => 5, - 'number_of_fragments' => 5, - 'matched_fields' => ['test'], - 'highlight_query' => [ - 'term' => [ - 'key1' => 'value1', - ], - ], - 'no_match_size' => 3, - 'force_source' => true, - ]; - $this->assertEquals($result, $field->toArray()); - } - - /** - * Tests getName method. - */ - public function testFieldGetName() - { - $field = new Field('test'); - $result = $field->getName(); - $this->assertEquals('test', $result); - } -} diff --git a/tests/Highlight/HighlightTest.php b/tests/Highlight/HighlightTest.php deleted file mode 100644 index ae3bb159545e31fd7f5549005022c7265e47de20..0000000000000000000000000000000000000000 --- a/tests/Highlight/HighlightTest.php +++ /dev/null @@ -1,52 +0,0 @@ -<?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\ElasticsearchDSL\Tests\Unit\DSL\Highlight; - -use ONGR\ElasticsearchDSL\Highlight\Field; -use ONGR\ElasticsearchDSL\Highlight\Highlight; - -/** - * Unit test for Highlight. - */ -class HighlightTest extends \PHPUnit_Framework_TestCase -{ - /** - * Tests toArray method. - */ - public function testHighlightToArray() - { - $highlight = new Highlight([new Field('name')]); - $highlight->setOrder('test'); - $highlight->setHighlighterType('postings'); - $highlight->setFragmentSize(5); - $highlight->setNumberOfFragments(5); - $highlight->setTagsSchema('styled'); - $highlight->setTag('tag', 'class'); - $highlight->setTag('only_tag'); - - $result = [ - 'order' => 'test', - 'type' => 'postings', - 'fragment_size' => 5, - 'number_of_fragments' => 5, - 'tags_schema' => 'styled', - 'post_tags' => ['</tag>', '</only_tag>'], - 'pre_tags' => ['<tag class="class">', '<only_tag>'], - 'fields' => [ - 'name' => [ - 'matched_fields' => ['name'], - ], - ], - ]; - $this->assertEquals($result, $highlight->toArray()); - } -} diff --git a/tests/Query/IndicesQueryTest.php b/tests/Query/IndicesQueryTest.php deleted file mode 100644 index 3d12065bec11f16b10aad88b7aa64a9cf2e962dd..0000000000000000000000000000000000000000 --- a/tests/Query/IndicesQueryTest.php +++ /dev/null @@ -1,72 +0,0 @@ -<?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\ElasticsearchDSL\Tests\Unit\DSL\Query; - -use ONGR\ElasticsearchDSL\Query\FilteredQuery; -use ONGR\ElasticsearchDSL\Query\IndicesQuery; -use ONGR\ElasticsearchDSL\Test\EncapsulationTestAwareTrait; - -class IndicesQueryTest extends \PHPUnit_Framework_TestCase -{ - /** - * Data provider for testToArrayManyIndices function. - * - * @return array - */ - public function getArrayWithManyIndicesDataProvider() - { - $queryMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Query') - ->getMock(); - $queryMock->expects($this->any()) - ->method('toArray') - ->willReturn(['testKey' => 'testValue']); - $queryMock->expects($this->any()) - ->method('getType') - ->willReturn('testType'); - - return [ - [ - $queryMock, - [ - 'test_indice1', - 'test_indice2', - ], - [ - 'indices' => [ - 'test_indice1', - 'test_indice2', - ], - 'query' => [ - 'testType' => [ - 'testKey' => 'testValue', - ], - ], - ], - ], - ]; - } - - /** - * Test toArray() method when the number of indices > 1. - * - * @param Query $query Query for testing. - * @param array $parameters Optional parameters. - * @param array $expected Expected values. - * - * @dataProvider getArrayWithManyIndicesDataProvider - */ - public function testToArrayWithManyIndices($query, $parameters, $expected) - { - $query = new IndicesQuery($parameters, $query); - $this->assertEquals($expected, $query->toArray()); - } -} diff --git a/tests/Query/QueryTest.php b/tests/Query/QueryTest.php deleted file mode 100644 index c27a031532a6c076f09efc3bc8f0a314b002bd98..0000000000000000000000000000000000000000 --- a/tests/Query/QueryTest.php +++ /dev/null @@ -1,86 +0,0 @@ -<?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\ElasticsearchDSL\Tests\Unit\DSL\Query; - -use ONGR\ElasticsearchDSL\Query\Query; - -class QueryTest extends \PHPUnit_Framework_TestCase -{ - /** - * Tests setBoolParameters method. - */ - public function testSetBoolParameters() - { - $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter') - ->setConstructorArgs(['test_field']) - ->getMock(); - $missingFilterMock->expects($this->once()) - ->method('setParameters'); - - $query = new Query(); - $query->setQuery($missingFilterMock); - $query->setBoolParameters([false]); - } - - /** - * Tests addQuery method. - */ - public function testAddQuery() - { - $missingFilterMock = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter') - ->disableOriginalConstructor() - ->setMethods(['add']) - ->getMock(); - $missingFilterMock - ->expects($this->once()) - ->method('add') - ->withAnyParameters(); - $postFilterMock = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\Filter\PostFilter') - ->disableOriginalConstructor() - ->getMock(); - - $query = new Query(); - $query->setQuery($missingFilterMock); - $query->addQuery($postFilterMock); - } - - /** - * Tests getType method. - */ - public function testGetType() - { - $query = new Query(); - $this->assertEquals('query', $query->getType()); - } - - /** - * Tests toArray method. - */ - public function testToArray() - { - $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter') - ->disableOriginalConstructor() - ->getMock(); - $missingFilterMock->expects($this->once()) - ->method('getType') - ->willReturn('test_type'); - $missingFilterMock->expects($this->once()) - ->method('toArray') - ->willReturn('test_array'); - - $query = new Query(); - $query->setQuery($missingFilterMock); - $this->assertEquals(['test_type' => 'test_array'], $query->toArray()); - } -} diff --git a/tests/SearchEndpoint/AggregationsEndpointTest.php b/tests/SearchEndpoint/AggregationsEndpointTest.php index 616d872b672911dfb56bfa0afa18e45cd4cbc002..1a3f5993b3db57f70e0decc3d468c31a04fe1f53 100644 --- a/tests/SearchEndpoint/AggregationsEndpointTest.php +++ b/tests/SearchEndpoint/AggregationsEndpointTest.php @@ -32,94 +32,4 @@ class AggregationsEndpointTest extends \PHPUnit_Framework_TestCase new AggregationsEndpoint() ); } - - /** - * Tests AddBuilder. - */ - public function testAddBuilder() - { - $instance = new AggregationsEndpoint(); - - /** @var NamedBuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); - $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); - $key = $instance->addBuilder($builderInterface1); - $this->assertSame('namedBuilder', $key); - $this->assertSame($builderInterface1, $instance->getBuilder($key)); - - /** @var BuilderInterface|MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - - $this->setExpectedException('InvalidArgumentException', 'Builder must be named builder'); - $instance->addBuilder($builderInterface2); - } - - /** - * Tests removing builders. - */ - public function testRemoveBuilder() - { - $instance = new AggregationsEndpoint(); - /** @var NamedBuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); - $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); - $key = $instance->addBuilder($builderInterface1); - - $this->assertNotNull($instance->getBuilder($key)); - $this->assertSame($instance, $instance->removeBuilder($key)); - $this->assertNull($instance->getBuilder($key)); - } - - /** - * Tests getting all builders. - */ - public function testGetBuilders() - { - $instance = new AggregationsEndpoint(); - $this->assertSame([], $instance->getBuilders()); - - /** @var NamedBuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); - $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); - - $instance->addBuilder($builderInterface1); - $this->assertSame(['namedBuilder' => $builderInterface1], $instance->getBuilders()); - - /** @var NamedBuilderInterface|MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); - $builderInterface2->expects($this->any())->method('getName')->willReturn('namedBuilder2'); - - $instance->addBuilder($builderInterface2); - $this->assertSame( - [ - 'namedBuilder' => $builderInterface1, - 'namedBuilder2' => $builderInterface2, - ], - $instance->getBuilders() - ); - } - - /** - * Tests normalization builder. - */ - public function testNormalization() - { - $instance = new AggregationsEndpoint(); - /** @var NormalizerInterface|MockObject $normalizerInterface */ - $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' - ); - - $this->assertNull($instance->normalize($normalizerInterface)); - - /** @var NamedBuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); - $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); - $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(0))->method('getType')->willReturn('test'); - - $instance->addBuilder($builderInterface1); - - $this->assertSame(['array' => 'data'], $instance->normalize($normalizerInterface)); - } } diff --git a/tests/SearchEndpoint/BuilderContainerAwareTraitTest.php b/tests/SearchEndpoint/BuilderContainerAwareTraitTest.php deleted file mode 100644 index d6d6a3f3c5fa56c04aacbe2a5f6075e66d35aa53..0000000000000000000000000000000000000000 --- a/tests/SearchEndpoint/BuilderContainerAwareTraitTest.php +++ /dev/null @@ -1,96 +0,0 @@ -<?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\ElasticsearchDSL\Tests\Unit\SearchEndpoint; - -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\SearchEndpoint\BuilderContainerAwareTrait; - -/** - * Class BuilderContainerAwareTraitTest. - */ -class BuilderContainerAwareTraitTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var BuilderContainerAwareTrait|\PHPUnit_Framework_MockObject_MockObject - */ - private $instance; - - /** - * {@inheritdoc} - */ - protected function setUp() - { - parent::setUp(); - - $this->instance = $this->getMockForTrait('ONGR\\ElasticsearchDSL\\SearchEndpoint\\BuilderContainerAwareTrait'); - } - - /** - * Tests builder parameters. - */ - public function testBuilderParameters() - { - $this->assertSame([], $this->instance->getBuilderParameters('non_existing_builder')); - $this->assertSame( - $this->instance, - $this->instance->setBuilderParameters('key', [ 'builder' => 'parameter' ]) - ); - $this->assertSame([ 'builder' => 'parameter' ], $this->instance->getBuilderParameters('key')); - } - - /** - * Tests interactions with builders. - */ - public function testBuilders() - { - $this->assertSame([], $this->instance->getBuilders()); - $this->assertNull($this->instance->getBuilder('non_existing_builder')); - $this->assertSame($this->instance, $this->instance->removeBuilder('non_existing_builder')); - - /** @var BuilderInterface $builder1 */ - $builder1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $key1 = $this->instance->addBuilder($builder1, ['parameter' => 'value']); - $this->assertSame($builder1, $this->instance->getBuilder($key1)); - $this->assertSame(['parameter' => 'value'], $this->instance->getBuilderParameters($key1)); - $builders = $this->instance->getBuilders(); - $this->assertCount(1, $builders); - $this->assertArrayHasKey($key1, $builders); - $this->assertSame($builder1, $builders[$key1]); - - /** @var BuilderInterface $builder2 */ - $builder2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $key2 = $this->instance->addBuilder($builder2, ['parameter2' => 'value2']); - $this->assertSame($builder2, $this->instance->getBuilder($key2)); - $this->assertSame(['parameter2' => 'value2'], $this->instance->getBuilderParameters($key2)); - $builders = $this->instance->getBuilders(); - $this->assertCount(2, $builders); - $this->assertArrayHasKey($key1, $builders); - $this->assertArrayHasKey($key2, $builders); - $this->assertSame($builder1, $builders[$key1]); - $this->assertSame($builder2, $builders[$key2]); - - $this->instance->removeBuilder($key2); - $this->assertNull($this->instance->getBuilder($key2)); - $this->assertSame([], $this->instance->getBuilderParameters($key2)); - $builders = $this->instance->getBuilders(); - $this->assertCount(1, $builders); - $this->assertArrayHasKey($key1, $builders); - $this->assertArrayNotHasKey($key2, $builders); - $this->assertSame($builder1, $builders[$key1]); - $this->assertSame($builder1, ($this->instance->getBuilder($key1))); - - $this->instance->removeBuilder($key1); - $this->assertNull($this->instance->getBuilder($key1)); - $this->assertSame([], $this->instance->getBuilderParameters($key1)); - $this->assertSame([], $this->instance->getBuilders()); - } -} diff --git a/tests/SearchEndpoint/FilterEndpointTest.php b/tests/SearchEndpoint/FilterEndpointTest.php index 3de6d22c04195b3d47d2fade62b4104338f062e1..b8b46a8365e1ac94ab17cdcb14eb7f2fda2b6979 100644 --- a/tests/SearchEndpoint/FilterEndpointTest.php +++ b/tests/SearchEndpoint/FilterEndpointTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\Filter\MatchAllFilter; use ONGR\ElasticsearchDSL\Query\FilteredQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -34,7 +35,7 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase } /** - * Tests if correct order is returned. + * Tests if correct order is returned. It's very important that filters must be executed first. */ public function testGetOrder() { @@ -55,31 +56,15 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase $this->assertNull($instance->normalize($normalizerInterface)); $this->assertFalse($instance->hasReference('filtered_query')); - /** @var BuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(1))->method('getType')->willReturn('test'); - $instance->addBuilder($builderInterface1); + $matchAllFilter = new MatchAllFilter(); + $instance->add($matchAllFilter); $this->assertNull($instance->normalize($normalizerInterface)); $this->assertTrue($instance->hasReference('filtered_query')); - /** @var FilteredQuery $reference */ - $reference = $instance->getReference('filtered_query'); - $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference); - $this->assertSame($builderInterface1, $reference->getFilter()); - - $instance = new FilterEndpoint(); - /** @var BuilderInterface|MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface2->expects($this->exactly(1))->method('toArray')->willReturn(['array2' => 'data2']); - $builderInterface2->expects($this->exactly(1))->method('getType')->willReturn('test2'); - $instance->addBuilder($builderInterface1); - $instance->addBuilder($builderInterface2); - $this->assertNull($instance->normalize($normalizerInterface)); - $this->assertTrue($instance->hasReference('filtered_query')); + /** @var FilteredQuery $reference */ $reference = $instance->getReference('filtered_query'); $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference); - $this->assertInstanceOf('ONGR\ElasticsearchDSL\Filter\BoolFilter', $reference->getFilter()); + $this->assertSame($matchAllFilter, $reference->getFilter()); } } diff --git a/tests/SearchEndpoint/HighlightEndpointTest.php b/tests/SearchEndpoint/HighlightEndpointTest.php index 90967c66de6da76a721fde5066f6f909dec2cb2b..496e14dce0ec02381397343c7fba6f7448e3e862 100644 --- a/tests/SearchEndpoint/HighlightEndpointTest.php +++ b/tests/SearchEndpoint/HighlightEndpointTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\Highlight\Highlight; use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; @@ -29,23 +30,6 @@ class HighlightEndpointTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint', new HighlightEndpoint()); } - /** - * Tests adding builder. - */ - public function testAddBuilder() - { - $instance = new HighlightEndpoint(); - - /** @var BuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $key = $instance->addBuilder($builderInterface1); - $this->assertNotNull($key); - $this->assertSame($builderInterface1, $instance->getBuilder($key)); - - $this->setExpectedException('OverflowException', 'Only one highlight is expected'); - $instance->addBuilder($builderInterface1); - } - /** * Tests adding builder. */ @@ -59,12 +43,13 @@ class HighlightEndpointTest extends \PHPUnit_Framework_TestCase $this->assertNull($instance->normalize($normalizerInterface)); - /** @var BuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(0))->method('getType')->willReturn('test'); + $highlight = new Highlight(); + $highlight->addField('acme'); + $instance->add($highlight); - $instance->addBuilder($builderInterface1); - $this->assertSame(['array' => 'data'], $instance->normalize($normalizerInterface)); + $this->assertEquals( + json_encode($highlight->toArray()), + json_encode($instance->normalize($normalizerInterface)) + ); } } diff --git a/tests/SearchEndpoint/PostFilterEndpointTest.php b/tests/SearchEndpoint/PostFilterEndpointTest.php index 857cdbc8e0b83cd9fcbd5173f04f076282616821..4808c5fb9ca08d89ec41055c3f2ac0c331eb71a9 100644 --- a/tests/SearchEndpoint/PostFilterEndpointTest.php +++ b/tests/SearchEndpoint/PostFilterEndpointTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\Filter\MatchAllFilter; use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; @@ -29,6 +30,15 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', new PostFilterEndpoint()); } + /** + * Tests if correct order is returned. It's very important that filters must be executed second. + */ + public function testGetOrder() + { + $instance = new PostFilterEndpoint(); + $this->assertEquals(2, $instance->getOrder()); + } + /** * Test normalization. */ @@ -41,12 +51,12 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase ); $this->assertNull($instance->normalize($normalizerInterface)); - /** @var BuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(1))->method('getType')->willReturn('test'); + $matchAll = new MatchAllFilter(); + $instance->add($matchAll); - $instance->addBuilder($builderInterface1); - $this->assertSame(['test' => ['array' => 'data']], $instance->normalize($normalizerInterface)); + $this->assertEquals( + json_encode([$matchAll->getType() => $matchAll->toArray()]), + json_encode($instance->normalize($normalizerInterface)) + ); } } diff --git a/tests/SearchEndpoint/QueryEndpointTest.php b/tests/SearchEndpoint/QueryEndpointTest.php index e22cf4a668b32bd7e4ca231990e80bd4310cb41b..759efb77243e69c2ddceb5b601130f7d0b637d0b 100644 --- a/tests/SearchEndpoint/QueryEndpointTest.php +++ b/tests/SearchEndpoint/QueryEndpointTest.php @@ -14,6 +14,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\FilteredQuery; +use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -32,12 +33,12 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase } /** - * Tests if correct order is returned. + * Tests if correct order is returned. Query must be executed after filter and post filter. */ public function testGetOrder() { $instance = new QueryEndpoint(); - $this->assertEquals(2, $instance->getOrder()); + $this->assertEquals(3, $instance->getOrder()); } /** @@ -52,92 +53,12 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase ); $this->assertNull($instance->normalize($normalizerInterface)); - /** @var BuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface1->expects($this->exactly(3))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(3))->method('getType')->willReturn('test'); - $instance->addBuilder($builderInterface1); - $data = $instance->normalize($normalizerInterface); - $this->assertEquals(['test' => ['array' => 'data']], $data); + $matchAll = new MatchAllQuery(); + $instance->add($matchAll); - /** @var BuilderInterface|MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface2->expects($this->exactly(2))->method('toArray')->willReturn(['array2' => 'data2']); - $builderInterface2->expects($this->exactly(2))->method('getType')->willReturn('test2'); - - $instance->addBuilder($builderInterface2); - $data = $instance->normalize($normalizerInterface); $this->assertEquals( - [ - 'bool' => [ - 'must' => [ - [ 'test' => [ 'array' => 'data' ] ], - [ 'test2' => [ 'array2' => 'data2' ] ], - ], - ], - ], - $data - ); - - /** @var BuilderInterface|MockObject $builderInterface3 */ - $builderInterface3 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface3->expects($this->once())->method('toArray')->willReturn(['array3' => 'data3']); - $builderInterface3->expects($this->once())->method('getType')->willReturn('test3'); - $instance->addBuilder($builderInterface3, ['bool_type' => BoolQuery::SHOULD]); - $instance->setParameters(['some' => 'parameter']); - $data = $instance->normalize($normalizerInterface); - $this->assertEquals( - [ - 'bool' => [ - 'must' => [ - [ 'test' => [ 'array' => 'data' ] ], - [ 'test2' => [ 'array2' => 'data2' ] ], - ], - 'should' => [ - [ 'test3' => [ 'array3' => 'data3' ] ], - ], - 'some' => 'parameter', - ], - ], - $data - ); - } - - /** - * Tests filtered query reference. - */ - public function testFilteredQuery() - { - $instance = new QueryEndpoint(); - /** @var NormalizerInterface|MockObject $normalizerInterface */ - $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' - ); - /** @var BuilderInterface|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(1))->method('getType')->willReturn('test'); - - /** @var BuilderInterface|MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - $builderInterface2->expects($this->exactly(1))->method('toArray')->willReturn(['array2' => 'data2']); - $builderInterface2->expects($this->exactly(1))->method('getType')->willReturn('test2'); - - $filteredQuery = new FilteredQuery($builderInterface1, $builderInterface2); - $instance->addReference('filtered_query', $filteredQuery); - - $this->assertSame( - [ - 'filtered' => [ - 'filter' => [ - 'test2' => ['array2' => 'data2'], - ], - 'query' => [ - 'test' => ['array' => 'data'], - ], - ], - ], + [$matchAll->getType() => $matchAll->toArray()], $instance->normalize($normalizerInterface) ); } diff --git a/tests/SearchEndpoint/SearchEndpointFactoryTest.php b/tests/SearchEndpoint/SearchEndpointFactoryTest.php index db96ada847b867ddc237aa88e5c876558534680c..9d134dc9d1f209f746e66ac8a00c8f6d946f5d73 100644 --- a/tests/SearchEndpoint/SearchEndpointFactoryTest.php +++ b/tests/SearchEndpoint/SearchEndpointFactoryTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; +use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; /** @@ -33,6 +34,6 @@ class SearchEndpointFactoryTest extends \PHPUnit_Framework_TestCase */ public function testFactory() { - SearchEndpointFactory::get('aggregations'); + SearchEndpointFactory::get(AggregationsEndpoint::NAME); } } diff --git a/tests/SearchEndpoint/SortEndpointTest.php b/tests/SearchEndpoint/SortEndpointTest.php index 7ae8f11fae92caf392e1ae6dce38c5c35eb44e41..f3079cd94a7e244d376c72c13fc5db450f48e547 100644 --- a/tests/SearchEndpoint/SortEndpointTest.php +++ b/tests/SearchEndpoint/SortEndpointTest.php @@ -13,7 +13,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; -use ONGR\ElasticsearchDSL\Sort\AbstractSort; +use ONGR\ElasticsearchDSL\Sort\FieldSort; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -31,48 +31,23 @@ class SortEndpointTest extends \PHPUnit_Framework_TestCase } /** - * Tests AddBuilder. + * Tests endpoint normalization. */ - public function testAddBuilder() + public function testNormalize() { $instance = new SortEndpoint(); - /** @var AbstractSort|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockBuilder('ONGR\ElasticsearchDSL\Sort\AbstractSort') - ->disableOriginalConstructor() - ->getMockForAbstractClass(); - - $key = $instance->addBuilder($builderInterface1); - $this->assertNotNull($key); - $this->assertSame($builderInterface1, $instance->getBuilder($key)); - - /** @var BuilderInterface|MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - - $this->setExpectedException('InvalidArgumentException', 'Sort must must a subtype of AbstractSort'); - $instance->addBuilder($builderInterface2); - } - - /** - * Tests if endpoint return correct normalized data. - */ - public function testEndpoint() - { - $instance = new SortEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' ); - $this->assertNull($instance->normalize($normalizerInterface)); - /** @var AbstractSort|MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockBuilder('ONGR\ElasticsearchDSL\Sort\AbstractSort') - ->disableOriginalConstructor() - ->getMockForAbstractClass(); - $builderInterface1->expects($this->exactly(0))->method('toArray')->willReturn(['array' => 'data']); - $builderInterface1->expects($this->exactly(2))->method('getType')->willReturn('test'); - $instance->addBuilder($builderInterface1); + $sort = new FieldSort('acme', ['order' => FieldSort::ASC]); + $instance->add($sort); - $this->assertSame(['test' => ['order' => 'asc']], $instance->normalize($normalizerInterface)); + $this->assertEquals( + [$sort->toArray()], + $instance->normalize($normalizerInterface) + ); } }