Completed
Pull Request — 2.6 (#3147)
by
unknown
10:56 queued 06:16
created

IndexTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 169
Duplicated Lines 27.22 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 46
loc 169
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A createIndex() 0 4 1
A testCreateIndex() 0 10 1
A testCreatePrimary() 0 6 1
A testCreateUnique() 0 6 1
A testFulfilledByUnique() 9 9 1
A testFulfilledByPrimary() 9 9 1
A testFulfilledByIndex() 0 11 1
A testCompoundedPrimaryKeyDoesNotFullfillSingleUniqueIndex() 0 8 1
A testFulfilledWithPartial() 14 14 1
A testFulfilledByCompounded() 0 8 1
A testOverrulesWithPartial() 14 14 1
A testFlags() 0 15 1
A testIndexQuotes() 0 11 1
A testOptions() 0 13 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
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Schema\Index;
6
7
class IndexTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function createIndex($unique = false, $primary = false, $options = array())
10
    {
11
        return new Index("foo", array("bar", "baz"), $unique, $primary, array(), $options);
12
    }
13
14
    public function testCreateIndex()
15
    {
16
        $idx = $this->createIndex();
17
        $this->assertEquals("foo", $idx->getName());
18
        $columns = $idx->getColumns();
19
        $this->assertEquals(2, count($columns));
20
        $this->assertEquals(array("bar", "baz"), $columns);
21
        $this->assertFalse($idx->isUnique());
22
        $this->assertFalse($idx->isPrimary());
23
    }
24
25
    public function testCreatePrimary()
26
    {
27
        $idx = $this->createIndex(false, true);
28
        $this->assertTrue($idx->isUnique());
29
        $this->assertTrue($idx->isPrimary());
30
    }
31
32
    public function testCreateUnique()
33
    {
34
        $idx = $this->createIndex(true, false);
35
        $this->assertTrue($idx->isUnique());
36
        $this->assertFalse($idx->isPrimary());
37
    }
38
39
    /**
40
     * @group DBAL-50
41
     */
42 View Code Duplication
    public function testFulfilledByUnique()
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...
43
    {
44
        $idx1 = $this->createIndex(true, false);
45
        $idx2 = $this->createIndex(true, false);
46
        $idx3 = $this->createIndex();
47
48
        $this->assertTrue($idx1->isFullfilledBy($idx2));
49
        $this->assertFalse($idx1->isFullfilledBy($idx3));
50
    }
51
52
    /**
53
     * @group DBAL-50
54
     */
55 View Code Duplication
    public function testFulfilledByPrimary()
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...
56
    {
57
        $idx1 = $this->createIndex(true, true);
58
        $idx2 = $this->createIndex(true, true);
59
        $idx3 = $this->createIndex(true, false);
60
61
        $this->assertTrue($idx1->isFullfilledBy($idx2));
62
        $this->assertFalse($idx1->isFullfilledBy($idx3));
63
    }
64
65
    /**
66
     * @group DBAL-50
67
     */
68
    public function testFulfilledByIndex()
69
    {
70
        $idx1 = $this->createIndex();
71
        $idx2 = $this->createIndex();
72
        $pri = $this->createIndex(true, true);
73
        $uniq = $this->createIndex(true);
74
75
        $this->assertTrue($idx1->isFullfilledBy($idx2));
76
        $this->assertTrue($idx1->isFullfilledBy($pri));
77
        $this->assertTrue($idx1->isFullfilledBy($uniq));
78
    }
79
80
    public function testCompoundedPrimaryKeyDoesNotFullfillSingleUniqueIndex()
81
    {
82
        $primary = new Index("the_primary", array("foo", "bar"), true, true);
83
        $unique = new Index("uniq_foo", array("foo"), true, false);
84
85
        self::assertFalse($primary->isFullfilledBy($unique));
86
        self::assertFalse($unique->isFullfilledBy($primary));
87
    }
88
89 View Code Duplication
    public function testFulfilledWithPartial()
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...
90
    {
91
        $without = new Index('without', array('col1', 'col2'), true, false, array(), array());
92
        $partial = new Index('partial', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
93
        $another = new Index('another', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
94
95
        $this->assertFalse($partial->isFullfilledBy($without));
96
        $this->assertFalse($without->isFullfilledBy($partial));
97
98
        $this->assertTrue($partial->isFullfilledBy($partial));
99
100
        $this->assertTrue($partial->isFullfilledBy($another));
101
        $this->assertTrue($another->isFullfilledBy($partial));
102
    }
103
104
    public function testFulfilledByCompounded()
105
    {
106
        $compounded = new Index('compounded', array('col1', 'col2'), true, false, array(), array());
107
        $single = new Index('single', array('col1'), true, false, array(), array());
108
109
        self::assertTrue($single->isFullfilledBy($compounded));
110
        self::assertFalse($compounded->isFullfilledBy($single));
111
    }
112
113 View Code Duplication
    public function testOverrulesWithPartial()
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...
114
    {
115
        $without = new Index('without', array('col1', 'col2'), true, false, array(), array());
116
        $partial = new Index('partial', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
117
        $another = new Index('another', array('col1', 'col2'), true, false, array(), array('where' => 'col1 IS NULL'));
118
119
        $this->assertFalse($partial->overrules($without));
120
        $this->assertFalse($without->overrules($partial));
121
122
        $this->assertTrue($partial->overrules($partial));
123
124
        $this->assertTrue($partial->overrules($another));
125
        $this->assertTrue($another->overrules($partial));
126
    }
127
128
    /**
129
     * @group DBAL-220
130
     */
131
    public function testFlags()
132
    {
133
        $idx1 = $this->createIndex();
134
        $this->assertFalse($idx1->hasFlag('clustered'));
135
        $this->assertEmpty($idx1->getFlags());
136
137
        $idx1->addFlag('clustered');
138
        $this->assertTrue($idx1->hasFlag('clustered'));
139
        $this->assertTrue($idx1->hasFlag('CLUSTERED'));
140
        $this->assertSame(array('clustered'), $idx1->getFlags());
141
142
        $idx1->removeFlag('clustered');
143
        $this->assertFalse($idx1->hasFlag('clustered'));
144
        $this->assertEmpty($idx1->getFlags());
145
    }
146
147
    /**
148
     * @group DBAL-285
149
     */
150
    public function testIndexQuotes()
151
    {
152
        $index = new Index("foo", array("`bar`", "`baz`"));
153
154
        $this->assertTrue($index->spansColumns(array("bar", "baz")));
155
        $this->assertTrue($index->hasColumnAtPosition("bar", 0));
156
        $this->assertTrue($index->hasColumnAtPosition("baz", 1));
157
158
        $this->assertFalse($index->hasColumnAtPosition("bar", 1));
159
        $this->assertFalse($index->hasColumnAtPosition("baz", 0));
160
    }
161
162
    public function testOptions()
163
    {
164
        $idx1 = $this->createIndex();
165
        $this->assertFalse($idx1->hasOption('where'));
166
        $this->assertEmpty($idx1->getOptions());
167
168
        $idx2 = $this->createIndex(false, false, array('where' => 'name IS NULL'));
169
        $this->assertTrue($idx2->hasOption('where'));
170
        $this->assertTrue($idx2->hasOption('WHERE'));
171
        $this->assertSame('name IS NULL', $idx2->getOption('where'));
172
        $this->assertSame('name IS NULL', $idx2->getOption('WHERE'));
173
        $this->assertSame(array('where' => 'name IS NULL'), $idx2->getOptions());
174
    }
175
}
176