|
| 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 | +} |
0 commit comments