From 6ff5eceef1958955acbed4da254e7be4b3029ed8 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas <martynas.sudintas@nfq.lt> Date: Thu, 22 Jan 2015 16:58:49 +0200 Subject: [PATCH] implemented FriendlyBuilderBag --- Aggregation/FilterAggregationTest.php | 2 +- Aggregation/GlobalAggregationTest.php | 2 +- Aggregation/RangeAggregationTest.php | 2 +- FriendlyBuilderBagTest.php | 105 ++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 FriendlyBuilderBagTest.php diff --git a/Aggregation/FilterAggregationTest.php b/Aggregation/FilterAggregationTest.php index ac05f99..745869b 100644 --- a/Aggregation/FilterAggregationTest.php +++ b/Aggregation/FilterAggregationTest.php @@ -69,7 +69,7 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase ->method('getName') ->willReturn('agg_test_agg2'); - $aggregation->aggregations->addAggregation($aggregation2); + $aggregation->aggregations->add($aggregation2); $result = [ 'agg_test_agg' => [ diff --git a/Aggregation/GlobalAggregationTest.php b/Aggregation/GlobalAggregationTest.php index 255d74d..1f64af0 100644 --- a/Aggregation/GlobalAggregationTest.php +++ b/Aggregation/GlobalAggregationTest.php @@ -41,7 +41,7 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase // Case #1 nested global aggregation. $aggregation = new GlobalAggregation('test_agg'); $aggregation2 = new GlobalAggregation('test_agg_2'); - $aggregation->aggregations->addAggregation($aggregation2); + $aggregation->aggregations->add($aggregation2); $result = [ 'agg_test_agg' => [ diff --git a/Aggregation/RangeAggregationTest.php b/Aggregation/RangeAggregationTest.php index 339552f..0114009 100644 --- a/Aggregation/RangeAggregationTest.php +++ b/Aggregation/RangeAggregationTest.php @@ -88,7 +88,7 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $aggregation2 = new RangeAggregation('test_agg_2'); $aggregation2->addRange('20', '20'); - $aggregation->aggregations->addAggregation($aggregation2); + $aggregation->aggregations->add($aggregation2); $result = [ 'agg_test_agg' => [ diff --git a/FriendlyBuilderBagTest.php b/FriendlyBuilderBagTest.php new file mode 100644 index 0000000..e8c3f74 --- /dev/null +++ b/FriendlyBuilderBagTest.php @@ -0,0 +1,105 @@ +<?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; + + +use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderBag; +use ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface; + +class FriendlyBuilderBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if bag knows if he has a builder. + */ + public function testHas() + { + $bag = new FriendlyBuilderBag( + [ + $this->getBuilder('foo'), + ] + ); + $this->assertTrue($bag->has('foo')); + } + + /** + * Tests if bag can remove a builder. + */ + public function testRemove() + { + $bag = new FriendlyBuilderBag( + [ + $this->getBuilder('foo'), + $this->getBuilder('baz'), + ] + ); + + $bag->remove('foo'); + + $this->assertFalse($bag->has('foo'), 'Foo builder should not exist anymore.'); + $this->assertTrue($bag->has('baz'), 'Baz builder should exist.'); + } + + /** + * Tests if bag can clear it's builders. + */ + public function testClear() + { + $bag = new FriendlyBuilderBag( + [ + $this->getBuilder('foo'), + $this->getBuilder('baz'), + ] + ); + + $bag->clear(); + + $this->assertEmpty($bag->all()); + } + + /** + * Tests if bag can get a builder. + */ + public function testGet() + { + $bag = new FriendlyBuilderBag( + [ + $this->getBuilder('baz'), + ] + ); + + $this->assertNotEmpty($bag->get('baz')); + } + + /** + * Returns builder. + * + * @param string $name + * + * @return \PHPUnit_Framework_MockObject_MockObject|FriendlyBuilderInterface + */ + private function getBuilder($name) + { + $friendlyBuilderMock = $this->getMock('ONGR\ElasticsearchBundle\DSL\FriendlyBuilderInterface'); + + $friendlyBuilderMock + ->expects($this->once()) + ->method('getName') + ->will($this->returnValue($name)); + + $friendlyBuilderMock + ->expects($this->any()) + ->method('toArray') + ->will($this->returnValue([])); + + return $friendlyBuilderMock; + } +} -- GitLab