Completed
Pull Request — master (#1145)
by
unknown
06:24
created

IndexableTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 36.9 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 31
loc 84
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testIndexableUnknown() 7 7 1
A testValidIndexableCallbacks() 9 9 1
A testInvalidIsIndexableCallbacks() 7 7 1
A provideInvalidIsIndexableCallbacks() 0 10 1
A testObjectIsNotIndexableIfIndexingDisabled() 8 8 1
A provideIsIndexableCallbacks() 0 15 1
A setUp() 0 10 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 project.
5
 *
6
 * (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
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\Provider;
13
14
use FOS\ElasticaBundle\Provider\Indexable;
15
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16
17
class IndexableTest extends \PHPUnit_Framework_TestCase
18
{
19
    public $container;
20
21 View Code Duplication
    public function testIndexableUnknown()
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...
22
    {
23
        $indexable = new Indexable(array(), $this->container);
24
        $index = $indexable->isObjectIndexable('index', 'type', new Entity());
25
26
        $this->assertTrue($index);
27
    }
28
29
    /**
30
     * @dataProvider provideIsIndexableCallbacks
31
     */
32 View Code Duplication
    public function testValidIndexableCallbacks($callback, $return)
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...
33
    {
34
        $indexable = new Indexable(array(
35
            'index/type' => $callback,
36
        ), $this->container);
37
        $index = $indexable->isObjectIndexable('index', 'type', new Entity());
38
39
        $this->assertEquals($return, $index);
40
    }
41
42
    /**
43
     * @dataProvider provideInvalidIsIndexableCallbacks
44
     * @expectedException \InvalidArgumentException
45
     */
46 View Code Duplication
    public function testInvalidIsIndexableCallbacks($callback)
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...
47
    {
48
        $indexable = new Indexable(array(
49
            'index/type' => $callback,
50
        ), $this->container);
51
        $indexable->isObjectIndexable('index', 'type', new Entity());
52
    }
53
54
    public function provideInvalidIsIndexableCallbacks()
55
    {
56
        return array(
57
            array('nonexistentEntityMethod'),
58
            array(array('@indexableService', 'internalMethod')),
59
            array(array(new IndexableDecider(), 'internalMethod')),
60
            array(42),
61
            array('entity.getIsIndexable() && nonexistentEntityFunction()'),
62
        );
63
    }
64
65 View Code Duplication
    public function testObjectIsNotIndexableIfIndexingDisabled()
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...
66
    {
67
        $indexable = new Indexable(array(), $this->container);
68
        $indexable->setIndexingEnabled(false);
69
        $index = $indexable->isObjectIndexable('index', 'type', new Entity());
70
71
        $this->assertFalse($index);
72
    }
73
74
    public function provideIsIndexableCallbacks()
75
    {
76
        return array(
77
            array('isIndexable', false),
78
            array(array(new IndexableDecider(), 'isIndexable'), true),
79
            array(array('@indexableService', 'isIndexable'), true),
80
            array(array('@indexableService'), true),
81
            array(function (Entity $entity) { return $entity->maybeIndex(); }, true),
82
            array('entity.maybeIndex()', true),
83
            array('!object.isIndexable() && entity.property == "abc"', true),
84
            array('entity.property != "abc"', false),
85
            array('["array", "values"]', true),
86
            array('[]', false)
87
        );
88
    }
89
90
    protected function setUp()
91
    {
92
        $this->container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerInterface')
93
            ->getMock();
94
95
        $this->container->expects($this->any())
96
            ->method('get')
97
            ->with('indexableService')
98
            ->will($this->returnValue(new IndexableDecider()));
99
    }
100
}
101
102
class Entity
103
{
104
    public $property = 'abc';
105
106
    public function isIndexable()
107
    {
108
        return false;
109
    }
110
111
    public function maybeIndex()
112
    {
113
        return true;
114
    }
115
}
116
117
class IndexableDecider
118
{
119
    public function isIndexable(Entity $entity)
120
    {
121
        return !$entity->isIndexable();
122
    }
123
124
    protected function internalMethod()
125
    {
126
    }
127
128
    public function __invoke($object)
129
    {
130
        return true;
131
    }
132
}
133