From c656ba2c948f2adc32432797670d764d718d9a43 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com>
Date: Fri, 3 Apr 2015 16:39:27 +0300
Subject: [PATCH] Merge branch 'LTRocky-fixed2'

---
 Bool/BoolTest.php            |  4 +-
 Filter/PostFilterTest.php    | 44 +++++++++++++++++--
 ParametersTraitTest.php      | 44 +++++++++++++++++++
 Query/HasChildQueryTest.php  | 29 +++++++++++++
 Query/HasParentQueryTest.php | 29 +++++++++++++
 Query/IndicesQueryTest.php   | 72 +++++++++++++++++++++++++++++++
 Query/NestedQueryTest.php    | 43 +++++++++++++++++++
 Query/QueryTest.php          | 83 ++++++++++++++++++++++++++++++++++++
 Suggester/ContextTest.php    |  2 -
 9 files changed, 342 insertions(+), 8 deletions(-)
 create mode 100644 ParametersTraitTest.php
 create mode 100644 Query/HasChildQueryTest.php
 create mode 100644 Query/HasParentQueryTest.php
 create mode 100644 Query/IndicesQueryTest.php
 create mode 100644 Query/NestedQueryTest.php
 create mode 100644 Query/QueryTest.php

diff --git a/Bool/BoolTest.php b/Bool/BoolTest.php
index c493dc9..492dc38 100644
--- a/Bool/BoolTest.php
+++ b/Bool/BoolTest.php
@@ -26,9 +26,9 @@ class BoolTest extends \PHPUnit_Framework_TestCase
     public function testBoolIsRelevant()
     {
         $bool = new Bool();
-        $this->assertEquals(false, $bool->isRelevant());
+        $this->assertFalse($bool->isRelevant());
         $bool->addToBool(new MissingFilter('test'));
-        $this->assertEquals(true, $bool->isRelevant());
+        $this->assertTrue($bool->isRelevant());
     }
 
     /**
diff --git a/Filter/PostFilterTest.php b/Filter/PostFilterTest.php
index 4f11f12..747f2ce 100644
--- a/Filter/PostFilterTest.php
+++ b/Filter/PostFilterTest.php
@@ -20,8 +20,8 @@ class PostFilterTest extends \PHPUnit_Framework_TestCase
      */
     public function testIfGetType()
     {
-        $filter = new PostFilter();
-        $this->assertEquals('post_filter', $filter->getType());
+        $postFilter = new PostFilter();
+        $this->assertEquals('post_filter', $postFilter->getType());
     }
 
     /**
@@ -29,7 +29,43 @@ class PostFilterTest extends \PHPUnit_Framework_TestCase
      */
     public function testIfIsRelevantFunctionIsReturningFalse()
     {
-        $bool = new PostFilter();
-        $this->assertFalse($bool->isRelevant());
+        $postFilter = new PostFilter();
+        $this->assertFalse($postFilter->isRelevant());
+    }
+
+    /**
+     * Test addFilter method.
+     */
+    public function testAddFilter()
+    {
+        $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter')
+            ->setMethods(['addToBool'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $missingFilterMock->expects($this->once())
+            ->method('addToBool')
+            ->withAnyParameters();
+
+        $postFilter = new PostFilter();
+        $postFilter->setFilter($missingFilterMock);
+        $postFilter->addFilter($missingFilterMock, 'test');
+    }
+
+    /**
+     * Test setBoolParameters method.
+     */
+    public function testSetBoolParameters()
+    {
+        $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter')
+            ->setMethods(['setParameters'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $missingFilterMock->expects($this->once())
+            ->method('setParameters')
+            ->withAnyParameters();
+
+        $postFilter = new PostFilter();
+        $postFilter->setFilter($missingFilterMock);
+        $postFilter->setBoolParameters(['test_param']);
     }
 }
diff --git a/ParametersTraitTest.php b/ParametersTraitTest.php
new file mode 100644
index 0000000..3b9507b
--- /dev/null
+++ b/ParametersTraitTest.php
@@ -0,0 +1,44 @@
+<?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\ParametersTrait;
+
+/**
+ * Test for ParametersTrait.
+ */
+class ParametersTraitTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var ParametersTrait
+     */
+    private $parametersTraitMock;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setUp()
+    {
+        $this->parametersTraitMock = $this->getMockForTrait('ONGR\ElasticsearchBundle\DSL\ParametersTrait');
+    }
+
+    /**
+     * Tests addParameter method.
+     */
+    public function testGetAndAddParameter()
+    {
+        $this->parametersTraitMock->addParameter('acme', 123);
+        $this->assertEquals(123, $this->parametersTraitMock->getParameter('acme'));
+        $this->parametersTraitMock->addParameter('bar', 321);
+        $this->assertEquals(321, $this->parametersTraitMock->getParameter('bar'));
+    }
+}
diff --git a/Query/HasChildQueryTest.php b/Query/HasChildQueryTest.php
new file mode 100644
index 0000000..4fe6d82
--- /dev/null
+++ b/Query/HasChildQueryTest.php
@@ -0,0 +1,29 @@
+<?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\Query;
+
+use ONGR\ElasticsearchBundle\DSL\Query\HasChildQuery;
+
+class HasChildQueryTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Tests whether __constructor calls setParameters method.
+     */
+    public function testConstructor()
+    {
+        $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter')
+            ->setConstructorArgs(['test_field'])
+            ->getMock();
+        $query = new HasChildQuery('test_type', $missingFilterMock, ['test_parameter1']);
+        $this->assertEquals(['test_parameter1'], $query->getParameters());
+    }
+}
diff --git a/Query/HasParentQueryTest.php b/Query/HasParentQueryTest.php
new file mode 100644
index 0000000..57a8b77
--- /dev/null
+++ b/Query/HasParentQueryTest.php
@@ -0,0 +1,29 @@
+<?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\Query;
+
+use ONGR\ElasticsearchBundle\DSL\Query\HasParentQuery;
+
+class HasParentQueryTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Tests whether __constructor calls setParameters method.
+     */
+    public function testConstructor()
+    {
+        $missingFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter')
+            ->setConstructorArgs(['test_field'])
+            ->getMock();
+        $query = new HasParentQuery('test_type', $missingFilter, ['test_parameter1']);
+        $this->assertEquals(['test_parameter1'], $query->getParameters());
+    }
+}
diff --git a/Query/IndicesQueryTest.php b/Query/IndicesQueryTest.php
new file mode 100644
index 0000000..1336653
--- /dev/null
+++ b/Query/IndicesQueryTest.php
@@ -0,0 +1,72 @@
+<?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\Query;
+
+use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery;
+use ONGR\ElasticsearchBundle\DSL\Query\IndicesQuery;
+use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait;
+
+class IndicesQueryTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Data provider for testToArrayManyIndices function.
+     *
+     * @return array
+     */
+    public function getArrayWithManyIndicesDataProvider()
+    {
+        $queryMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\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/Query/NestedQueryTest.php b/Query/NestedQueryTest.php
new file mode 100644
index 0000000..545df36
--- /dev/null
+++ b/Query/NestedQueryTest.php
@@ -0,0 +1,43 @@
+<?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\Query;
+
+use ONGR\ElasticsearchBundle\DSL\Query\NestedQuery;
+
+class NestedQueryTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Tests toArray method.
+     */
+    public function testToArray()
+    {
+        $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter')
+            ->setConstructorArgs(['test_field'])
+            ->getMock();
+        $missingFilterMock->expects($this->any())
+            ->method('getType')
+            ->willReturn('test_type');
+        $missingFilterMock->expects($this->any())
+            ->method('toArray')
+            ->willReturn(['testKey' => 'testValue']);
+
+        $result = [
+            'path' => 'test_path',
+            'query' => [
+                'test_type' => ['testKey' => 'testValue'],
+            ],
+        ];
+
+        $query = new NestedQuery('test_path', $missingFilterMock);
+        $this->assertEquals($result, $query->toArray());
+    }
+}
diff --git a/Query/QueryTest.php b/Query/QueryTest.php
new file mode 100644
index 0000000..b900702
--- /dev/null
+++ b/Query/QueryTest.php
@@ -0,0 +1,83 @@
+<?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\Query;
+
+use ONGR\ElasticsearchBundle\DSL\Query\Query;
+
+class QueryTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Tests setBoolParameters method.
+     */
+    public function testSetBoolParameters()
+    {
+        $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\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\ElasticsearchBundle\DSL\Filter\MissingFilter')
+            ->disableOriginalConstructor()
+            ->setMethods(['addToBool'])
+            ->getMock();
+        $missingFilterMock->expects($this->once())
+            ->method('addToBool')
+            ->withAnyParameters();
+        $postFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\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\ElasticsearchBundle\DSL\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/Suggester/ContextTest.php b/Suggester/ContextTest.php
index 601ac2d..363a368 100644
--- a/Suggester/ContextTest.php
+++ b/Suggester/ContextTest.php
@@ -32,7 +32,6 @@ class ContextTest extends \PHPUnit_Framework_TestCase
         $context->setContext(new Phrase('', ''));
 
         $result = $context->toArray();
-
         $this->assertArrayHasKey($name, $result);
 
         $data = $result[$name];
@@ -40,7 +39,6 @@ class ContextTest extends \PHPUnit_Framework_TestCase
 
         $completion = $data['completion'];
         $this->assertArrayHasKey('size', $completion);
-
         $this->assertEquals($completion['size'], 123);
     }
 
-- 
GitLab