Completed
Pull Request — master (#1)
by Joao
04:35
created

BaseCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 161
Duplicated Lines 51.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 5
dl 83
loc 161
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 4 1
A testVersion0() 0 5 1
A testUpVersion1() 0 7 1
A testUpVersion2() 0 7 1
A testDownVersion1() 0 7 1
A testDownVersion0() 0 7 1
A getExpectedUsersVersion0() 0 7 1
A getExpectedUsersVersion1() 0 7 1
B assertVersion0() 29 29 2
B assertVersion1() 29 29 2
B assertVersion2() 25 25 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
// backward compatibility
4
if (!class_exists('\PHPUnit\Framework\TestCase')) {
5
    class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
6
}
7
8
abstract class BaseCommand extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    protected $uri = null;
11
12
    /**
13
     * @var \ByJG\DbMigration\Migration
14
     */
15
    protected $migrate = null;
16
17
    public function setUp()
18
    {
19
        // create Migrate object in the parent!!!
20
21
        $this->migrate->prepareEnvironment();
22
    }
23
24
    public function tearDown()
25
    {
26
        $this->migrate->getDbCommand()->dropDatabase();
27
    }
28
29
    public function testVersion0()
30
    {
31
        $this->migrate->reset(0);
32
        $this->assertVersion0();
33
    }
34
35
    public function testUpVersion1()
36
    {
37
        $this->migrate->reset(0);
38
        $this->assertVersion0();
39
        $this->migrate->up(1);
40
        $this->assertVersion1();
41
    }
42
43
    public function testUpVersion2()
44
    {
45
        $this->migrate->reset(0);
46
        $this->assertVersion0();
47
        $this->migrate->up(2);
48
        $this->assertVersion2();
49
    }
50
51
    public function testDownVersion1()
52
    {
53
        $this->migrate->reset();
54
        $this->assertVersion2();
55
        $this->migrate->down(1);
56
        $this->assertVersion1();
57
    }
58
59
    public function testDownVersion0()
60
    {
61
        $this->migrate->reset();
62
        $this->assertVersion2();
63
        $this->migrate->down(0);
64
        $this->assertVersion0();
65
    }
66
67
    protected function getExpectedUsersVersion0()
68
    {
69
        return [
70
            ["id" => 1, "name" => 'John Doe', 'createdate' => '20160110'],
71
            ["id" => 2, "name" => 'Jane Doe', 'createdate' => '20151230']
72
        ];
73
    }
74
75
    protected function getExpectedUsersVersion1()
76
    {
77
        return [
78
            ["id" => 1, "name" => 'John Doe', 'createdate' => '2016-01-10'],
79
            ["id" => 2, "name" => 'Jane Doe', 'createdate' => '2015-12-30']
80
        ];
81
    }
82
83 View Code Duplication
    protected function assertVersion0()
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...
84
    {
85
        $version = $this->migrate->getDbDriver()->getScalar('select version from migration_version');
86
        $this->assertEquals(0, $version);
87
88
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
89
90
        $this->assertTrue($iterator->hasNext());
91
        $row = $iterator->moveNext();
92
        $this->assertEquals(
93
            $this->getExpectedUsersVersion0()[0],
94
            $row->toArray()
95
        );
96
97
        $this->assertTrue($iterator->hasNext());
98
        $row = $iterator->moveNext();
99
        $this->assertEquals(
100
            $this->getExpectedUsersVersion0()[1],
101
            $row->toArray()
102
        );
103
104
        $this->assertFalse($iterator->hasNext());
105
106
        try {
107
            $this->migrate->getDbDriver()->getIterator('select * from roles');
108
        } catch (PDOException $ex) {
109
            $this->assertTrue(true);
110
        }
111
    }
112
113 View Code Duplication
    protected function assertVersion1()
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
        $version = $this->migrate->getDbDriver()->getScalar('select version from migration_version');
116
        $this->assertEquals(1, $version);
117
118
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
119
120
        $this->assertTrue($iterator->hasNext());
121
        $row = $iterator->moveNext();
122
        $this->assertEquals(
123
            $this->getExpectedUsersVersion1()[0],
124
            $row->toArray()
125
        );
126
127
        $this->assertTrue($iterator->hasNext());
128
        $row = $iterator->moveNext();
129
        $this->assertEquals(
130
            $this->getExpectedUsersVersion1()[1],
131
            $row->toArray()
132
        );
133
134
        $this->assertFalse($iterator->hasNext());
135
136
        try {
137
            $this->migrate->getDbDriver()->getIterator('select * from roles');
138
        } catch (PDOException $ex) {
139
            $this->assertTrue(true);
140
        }
141
    }
142
143 View Code Duplication
    protected function assertVersion2()
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...
144
    {
145
        $version = $this->migrate->getDbDriver()->getScalar('select version from migration_version');
146
        $this->assertEquals(2, $version);
147
148
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
149
150
        $this->assertTrue($iterator->hasNext());
151
        $row = $iterator->moveNext();
152
        $this->assertEquals(
153
            $this->getExpectedUsersVersion1()[0],
154
            $row->toArray()
155
        );
156
157
        $this->assertTrue($iterator->hasNext());
158
        $row = $iterator->moveNext();
159
        $this->assertEquals(
160
            $this->getExpectedUsersVersion1()[1],
161
            $row->toArray()
162
        );
163
164
        $this->assertFalse($iterator->hasNext());
165
166
        $this->migrate->getDbDriver()->getIterator('select * from roles');
167
    }
168
}
169