Completed
Pull Request — master (#1111)
by Karel
11:07 queued 07:06
created

testCreateHybridPaginatorAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Finder;
4
5
use Elastica\Query;
6
use FOS\ElasticaBundle\Finder\TransformedFinder;
7
8
class TransformedFinderTest extends \PHPUnit_Framework_TestCase
9
{
10
    private function createMockTransformer($transformMethod)
11
    {
12
        $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
13
14
        $transformer
15
            ->expects($this->once())
16
            ->method($transformMethod)
17
            ->with(array());
18
19
        return $transformer;
20
    }
21
22
    private function createMockFinderForSearch($transformer, $query, $limit)
23
    {
24
        $searchable = $this->getMock('Elastica\SearchableInterface');
25
26
        $finder = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder')
27
            ->setConstructorArgs(array($searchable, $transformer))
28
            ->setMethods(array('search'))
29
            ->getMock();
30
31
        $finder
32
            ->expects($this->once())
33
            ->method('search')
34
            ->with($query, $limit)
35
            ->will($this->returnValue(array()));
36
37
        return $finder;
38
    }
39
40
    private function createMockResultSet()
41
    {
42
        $resultSet = $this
43
            ->getMockBuilder('Elastica\ResultSet')
44
            ->disableOriginalConstructor()
45
            ->setMethods(array('getResults'))
46
            ->getMock();
47
48
        $resultSet->expects($this->once())->method('getResults')->will($this->returnValue(array()));
49
50
        return $resultSet;
51
    }
52
53
    public function testFindMethodTransformsSearchResults()
54
    {
55
        $transformer = $this->createMockTransformer('transform');
56
        $query = Query::create('');
57
        $limit = 10;
58
59
        $finder = $this->createMockFinderForSearch($transformer, $query, $limit);
60
61
        $finder->find($query, $limit);
62
    }
63
64
    public function testFindHybridMethodTransformsSearchResults()
65
    {
66
        $transformer = $this->createMockTransformer('hybridTransform');
67
        $query = Query::create('');
68
        $limit = 10;
69
70
        $finder = $this->createMockFinderForSearch($transformer, $query, $limit);
71
72
        $finder->findHybrid($query, $limit);
73
    }
74
75
    public function testMoreLikeThisTransformsSearchResultsFromIndex()
76
    {
77
        $searchable = $this
78
            ->getMockBuilder('Elastica\Type')
79
            ->disableOriginalConstructor()
80
            ->setMethods(array('moreLikeThis'))
81
            ->getMock();
82
83
        $searchable->expects($this->once())
84
            ->method('moreLikeThis')
85
            ->with($this->isInstanceOf('Elastica\Document'), $this->isType('array'), $this->isType('array'))
86
            ->will($this->returnValue($this->createMockResultSet()));
87
88
        $transformer = $this->createMockTransformer('transform');
89
90
        $finder = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder')
91
            ->setConstructorArgs(array($searchable, $transformer))
92
            ->setMethods(array('search'))
93
            ->getMock();
94
95
        $finder->moreLikeThis(1);
96
    }
97
98
    public function testSearchMethodCreatesAQueryAndReturnsResultsFromSearchableDependency()
99
    {
100
        $searchable = $this->getMock('Elastica\SearchableInterface');
101
        $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
102
103
        $searchable->expects($this->once())
104
            ->method('search')
105
            ->with($this->isInstanceOf('Elastica\Query'))
106
            ->will($this->returnValue($this->createMockResultSet()));
107
108
        $finder = new TransformedFinder($searchable, $transformer);
109
110
        $method = new \ReflectionMethod($finder, 'search');
111
        $method->setAccessible(true);
112
113
        $results = $method->invoke($finder, '', 10);
114
115
        $this->assertInternalType('array', $results);
116
    }
117
118 View Code Duplication
    public function testFindPaginatedReturnsAConfiguredPagerfantaObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $searchable = $this->getMock('Elastica\SearchableInterface');
121
        $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
122
123
        $finder = new TransformedFinder($searchable, $transformer);
124
125
        $pagerfanta = $finder->findPaginated('');
126
127
        $this->assertInstanceOf('Pagerfanta\Pagerfanta', $pagerfanta);
128
    }
129
130 View Code Duplication
    public function testCreatePaginatorAdapter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $searchable = $this->getMock('Elastica\SearchableInterface');
133
        $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
134
135
        $finder = new TransformedFinder($searchable, $transformer);
136
137
        $this->assertInstanceOf('FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter', $finder->createPaginatorAdapter(''));
138
    }
139
140 View Code Duplication
    public function testCreateHybridPaginatorAdapter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $searchable = $this->getMock('Elastica\SearchableInterface');
143
        $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
144
145
        $finder = new TransformedFinder($searchable, $transformer);
146
147
        $this->assertInstanceOf('FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter', $finder->createHybridPaginatorAdapter(''));
148
    }
149
}
150