diff --git a/Bool/BoolTest.php b/Bool/BoolTest.php index c493dc9651d560b791cb60ec5773d9c067d0b486..492dc38fbe4acf6b07c14620fb00696eaa4d44a7 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 4f11f12db554c829129992a4745a25e04fa6b1a3..747f2cedd899e2be07280f8aa26385a3afab3a11 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 0000000000000000000000000000000000000000..3b9507b92a2f623aac6dbadf3f6a138b66e2a9fb --- /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 0000000000000000000000000000000000000000..4fe6d8293e056581e46f6550a17bc9ccee8bccc3 --- /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 0000000000000000000000000000000000000000..57a8b77837150dca23efe0bdbec668d136f3c0ad --- /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 0000000000000000000000000000000000000000..13366530dc815857ff75b2bfa177e54a8f14a2c6 --- /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 0000000000000000000000000000000000000000..545df36f1628deaff62455dbbc096092f40d57dd --- /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 0000000000000000000000000000000000000000..b90070261a476c4a2fc2617b32ae0a404767ff9d --- /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 601ac2df1e9c3e43ea5592316606570510101dfc..363a368317069dbdc96c3783b09e0ba9e71af79e 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); }