Completed
Push — master ( 564fde...8048fc )
by Karel
06:22
created

TransformedFinderTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 24.37 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 4
dl 29
loc 119
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createMockTransformer() 0 11 1
A createMockFinderForSearch() 0 17 1
A createMockResultSet() 0 12 1
A testFindMethodTransformsSearchResults() 0 10 1
A testFindHybridMethodTransformsSearchResults() 0 10 1
A testCreatePaginatorAdapter() 9 9 1
A testCreateHybridPaginatorAdapter() 9 9 1
A testSearchMethodCreatesAQueryAndReturnsResultsFromSearchableDependency() 0 19 1
A testFindPaginatedReturnsAConfiguredPagerfantaObject() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\ElasticaBundle\Tests\Finder;
13
14
use Elastica\Query;
15
use FOS\ElasticaBundle\Finder\TransformedFinder;
16
17
class TransformedFinderTest extends \PHPUnit_Framework_TestCase
18
{
19
    private function createMockTransformer($transformMethod)
20
    {
21
        $transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock();
22
23
        $transformer
24
            ->expects($this->once())
25
            ->method($transformMethod)
26
            ->with(array());
27
28
        return $transformer;
29
    }
30
31
    private function createMockFinderForSearch($transformer, $query, $limit)
32
    {
33
        $searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock();
34
35
        $finder = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder')
36
            ->setConstructorArgs(array($searchable, $transformer))
37
            ->setMethods(array('search'))
38
            ->getMock();
39
40
        $finder
41
            ->expects($this->once())
42
            ->method('search')
43
            ->with($query, $limit)
44
            ->will($this->returnValue(array()));
45
46
        return $finder;
47
    }
48
49
    private function createMockResultSet()
50
    {
51
        $resultSet = $this
52
            ->getMockBuilder('Elastica\ResultSet')
53
            ->disableOriginalConstructor()
54
            ->setMethods(array('getResults'))
55
            ->getMock();
56
57
        $resultSet->expects($this->once())->method('getResults')->will($this->returnValue(array()));
58
59
        return $resultSet;
60
    }
61
62
    public function testFindMethodTransformsSearchResults()
63
    {
64
        $transformer = $this->createMockTransformer('transform');
65
        $query = Query::create('');
66
        $limit = 10;
67
68
        $finder = $this->createMockFinderForSearch($transformer, $query, $limit);
69
70
        $finder->find($query, $limit);
71
    }
72
73
    public function testFindHybridMethodTransformsSearchResults()
74
    {
75
        $transformer = $this->createMockTransformer('hybridTransform');
76
        $query = Query::create('');
77
        $limit = 10;
78
79
        $finder = $this->createMockFinderForSearch($transformer, $query, $limit);
80
81
        $finder->findHybrid($query, $limit);
82
    }
83
84
    public function testSearchMethodCreatesAQueryAndReturnsResultsFromSearchableDependency()
85
    {
86
        $searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock();
87
        $transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock();
88
89
        $searchable->expects($this->once())
90
            ->method('search')
91
            ->with($this->isInstanceOf('Elastica\Query'))
92
            ->will($this->returnValue($this->createMockResultSet()));
93
94
        $finder = new TransformedFinder($searchable, $transformer);
95
96
        $method = new \ReflectionMethod($finder, 'search');
97
        $method->setAccessible(true);
98
99
        $results = $method->invoke($finder, '', 10);
100
101
        $this->assertInternalType('array', $results);
102
    }
103
104 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...
105
    {
106
        $searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock();
107
        $transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock();
108
109
        $finder = new TransformedFinder($searchable, $transformer);
110
111
        $pagerfanta = $finder->findPaginated('');
112
113
        $this->assertInstanceOf('Pagerfanta\Pagerfanta', $pagerfanta);
114
    }
115
116 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...
117
    {
118
        $searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock();
119
        $transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock();
120
121
        $finder = new TransformedFinder($searchable, $transformer);
122
123
        $this->assertInstanceOf('FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter', $finder->createPaginatorAdapter(''));
124
    }
125
126 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...
127
    {
128
        $searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock();
129
        $transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock();
130
131
        $finder = new TransformedFinder($searchable, $transformer);
132
133
        $this->assertInstanceOf('FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter', $finder->createHybridPaginatorAdapter(''));
134
    }
135
}
136