Skip to content

Commit fe70ab3

Browse files
committed
Merge pull request #100 from chyzas/patch_dsl_unit_tests
DSL Filters Unit Tests
2 parents 13a112f + eb439cb commit fe70ab3

18 files changed

+1195
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter;
13+
14+
use ONGR\ElasticsearchBundle\DSL\Filter\AndFilter;
15+
16+
class AndFilterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* Tests GetType method.
20+
*/
21+
public function testGetType()
22+
{
23+
$filter = new AndFilter('', []);
24+
$result = $filter->getType();
25+
$this->assertEquals('and', $result);
26+
}
27+
28+
/**
29+
* Data provider for testToArray function.
30+
*
31+
* @return array
32+
*/
33+
public function getArrayDataProvider()
34+
{
35+
$mockBuildeFfirstFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')
36+
->getMock();
37+
$mockBuildeFfirstFilter->expects($this->any())
38+
->method('getType')
39+
->willReturn('term');
40+
$mockBuildeFfirstFilter->expects($this->any())
41+
->method('toArray')
42+
->willReturn(['test_field' => ['test_value' => 'test']]);
43+
44+
$mockBuilderSecondFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')
45+
->getMock();
46+
$mockBuilderSecondFilter->expects($this->any())
47+
->method('getType')
48+
->willReturn('prefix');
49+
$mockBuilderSecondFilter->expects($this->any())
50+
->method('toArray')
51+
->willReturn(['test_field' => ['test_value' => 'test']]);
52+
53+
return [
54+
// Case #1.
55+
[
56+
[$mockBuildeFfirstFilter],
57+
[],
58+
[
59+
'filters' => [
60+
0 => [
61+
'term' => [
62+
'test_field' => [
63+
'test_value' => 'test',
64+
],
65+
],
66+
],
67+
],
68+
],
69+
],
70+
// Case #2.
71+
[
72+
[$mockBuildeFfirstFilter, $mockBuilderSecondFilter],
73+
[],
74+
[
75+
'filters' => [
76+
0 => [
77+
'term' => [
78+
'test_field' => [
79+
'test_value' => 'test',
80+
],
81+
],
82+
],
83+
1 => [
84+
'prefix' => [
85+
'test_field' => [
86+
'test_value' => 'test',
87+
],
88+
],
89+
],
90+
],
91+
],
92+
],
93+
// Case #3.
94+
[
95+
[$mockBuildeFfirstFilter, $mockBuilderSecondFilter],
96+
['type' => 'acme'],
97+
[
98+
'filters' => [
99+
0 => [
100+
'term' => [
101+
'test_field' => [
102+
'test_value' => 'test',
103+
],
104+
],
105+
],
106+
1 => [
107+
'prefix' => [
108+
'test_field' => [
109+
'test_value' => 'test',
110+
],
111+
],
112+
],
113+
],
114+
'type' => 'acme',
115+
],
116+
],
117+
];
118+
}
119+
120+
/**
121+
* Test for filter toArray() method.
122+
*
123+
* @param BuilderInterface[] $filters Array.
124+
* @param array $parameters Optional parameters.
125+
* @param array $expected Expected values.
126+
*
127+
* @dataProvider getArrayDataProvider
128+
*/
129+
public function testToArray($filters, $parameters, $expected)
130+
{
131+
$filter = new AndFilter($filters, $parameters);
132+
$result = $filter->toArray();
133+
$this->assertEquals($expected, $result);
134+
}
135+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter;
13+
14+
use ONGR\ElasticsearchBundle\DSL\Filter\ExistsFilter;
15+
16+
class ExistsFilterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* Tests GetType method.
20+
*/
21+
public function testGetType()
22+
{
23+
$filter = new ExistsFilter('foo', 'bar');
24+
$this->assertEquals('exists', $filter->getType());
25+
}
26+
27+
/**
28+
* Test for filter toArray() method.
29+
*/
30+
public function testToArray()
31+
{
32+
$filter = new ExistsFilter('foo', 'bar');
33+
$expectedResult = ['foo' => 'bar'];
34+
$this->assertEquals($expectedResult, $filter->toArray());
35+
}
36+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter;
13+
14+
use ONGR\ElasticsearchBundle\DSL\Filter\IdsFilter;
15+
16+
class IdsFilterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* Tests GetType method.
20+
*/
21+
public function testGetType()
22+
{
23+
$filter = new IdsFilter([], []);
24+
$result = $filter->getType();
25+
$this->assertEquals('ids', $result);
26+
}
27+
28+
/**
29+
* Data provider to testGetToArray.
30+
*
31+
* @return array
32+
*/
33+
public function getArrayDataProvider()
34+
{
35+
return [
36+
// Case #1.
37+
[['acme', 'bar'], ['type' => 'acme'], ['values' => ['acme', 'bar'], 'type' => 'acme']],
38+
// Case #2.
39+
[[], [], ['values' => []]],
40+
// Case #3.
41+
[['acme'], [], ['values' => ['acme']]],
42+
];
43+
}
44+
45+
/**
46+
* Test for filter toArray() method.
47+
*
48+
* @param string[] $values Ids' values.
49+
* @param array $parameters Optional parameters.
50+
* @param array $expected Expected result.
51+
*
52+
* @dataProvider getArrayDataProvider
53+
*/
54+
public function testToArray($values, $parameters, $expected)
55+
{
56+
$filter = new IdsFilter($values, $parameters);
57+
$result = $filter->toArray();
58+
$this->assertEquals($expected, $result);
59+
}
60+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter;
13+
14+
use ONGR\ElasticsearchBundle\DSL\Filter\IndicesFilter;
15+
16+
class IndicesFilterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* Tests GetType method.
20+
*/
21+
public function testGetType()
22+
{
23+
$filter = new IndicesFilter([], '', null);
24+
$this->assertEquals('indices', $filter->getType());
25+
}
26+
27+
/**
28+
* Tests if Indices qty is greater than one.
29+
*/
30+
public function testToArrayIfIndicesQtyIsGreaterThanOne()
31+
{
32+
$mockBuilder = $this->indicesQtyMockBuilder(['test_field' => ['test_value' => 'test']]);
33+
34+
$filter = new IndicesFilter(['foo', 'bar'], $mockBuilder, null);
35+
$expectedResult = [
36+
'indices' => [0 => 'foo', 1 => 'bar'],
37+
'filter' => ['term' => ['test_field' => ['test_value' => 'test']]],
38+
];
39+
$result = $filter->toArray();
40+
$this->assertEquals($expectedResult, $result);
41+
}
42+
43+
/**
44+
* Test if Indices qty is less than one.
45+
*/
46+
public function testToArrayIfIndicesQtyIsLessThanOne()
47+
{
48+
$mockBuilder = $this->indicesQtyMockBuilder(['test_field' => ['test_value' => 'test']]);
49+
$filter = new IndicesFilter(['foo'], $mockBuilder, null);
50+
$expectedResult = ['index' => 'foo', 'filter' => ['term' => ['test_field' => ['test_value' => 'test']]]];
51+
$result = $filter->toArray();
52+
$this->assertEquals($expectedResult, $result);
53+
}
54+
55+
/**
56+
* Test.
57+
*/
58+
public function testWhenNoMatchFilterIsNotNull()
59+
{
60+
$mockBuilder = $this->indicesQtyMockBuilder(['tag' => 'wow']);
61+
$noMatchFilterMockBuilder = $this->indicesQtyMockBuilder(['tag' => 'kow']);
62+
$filter = new IndicesFilter(['foo'], $mockBuilder, $noMatchFilterMockBuilder);
63+
$expectedResult = [
64+
'index' => 'foo',
65+
'filter' => ['term' => ['tag' => 'wow']],
66+
'no_match_filter' => ['term' => ['tag' => 'kow']],
67+
];
68+
$result = $filter->toArray();
69+
$this->assertEquals($expectedResult, $result);
70+
}
71+
72+
/**
73+
* Test.
74+
*/
75+
public function testWhenNoMatchFilterIsEmpty()
76+
{
77+
$mockBuilder = $this->indicesQtyMockBuilder(['tag' => 'wow']);
78+
$filter = new IndicesFilter(['foo'], $mockBuilder, '');
79+
$expectedResult = [
80+
'index' => 'foo',
81+
'filter' => ['term' => ['tag' => 'wow']],
82+
'no_match_filter' => '',
83+
];
84+
$result = $filter->toArray();
85+
$this->assertEquals($expectedResult, $result);
86+
}
87+
88+
/**
89+
* Mock Builder.
90+
*
91+
* @param array $param Expected values.
92+
*
93+
* @return \PHPUnit_Framework_MockObject_MockObject
94+
*/
95+
private function indicesQtyMockBuilder(array $param = [])
96+
{
97+
$mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')
98+
->getMock();
99+
$mockBuilder->expects($this->any())
100+
->method('getType')
101+
->willReturn('term');
102+
$mockBuilder->expects($this->any())
103+
->method('toArray')
104+
->willReturn($param);
105+
106+
return $mockBuilder;
107+
}
108+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Filter;
13+
14+
use ONGR\ElasticsearchBundle\DSL\Filter\LimitFilter;
15+
16+
class LimitFilterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* Tests GetType method.
20+
*/
21+
public function testGetType()
22+
{
23+
$filter = new LimitFilter(0);
24+
$this->assertEquals('limit', $filter->getType());
25+
}
26+
27+
/**
28+
* Test for filter toArray() method.
29+
*/
30+
public function testToArray()
31+
{
32+
$filter = new LimitFilter(0);
33+
$expectedResult = ['value' => 0];
34+
$this->assertEquals($expectedResult, $filter->toArray());
35+
}
36+
}

0 commit comments

Comments
 (0)