Completed
Push — master ( 8e3086...7f1dfb )
by Joao
20s
created

BaseDatabase::testCreateVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use ByJG\DbMigration\Migration;
4
5
abstract class BaseDatabase extends \PHPUnit\Framework\TestCase
6
{
7
    protected $uri = null;
8
9
    /**
10
     * @var Migration
11
     */
12
    protected $migrate = null;
13
14
    protected $migrationTable = "migration_version";
15
16
    /**
17
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
18
     */
19
    public function setUp()
20
    {
21
        // create Migrate object in the parent!!!
22
23
        $this->migrate->prepareEnvironment();
24
    }
25
26
    /**
27
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
28
     */
29
    public function tearDown()
30
    {
31
        $this->migrate->getDbCommand()->dropDatabase();
32
    }
33
34
    /**
35
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
36
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
37
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
38
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
39
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
40
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
41
     */
42
    public function testVersion0()
43
    {
44
        $this->migrate->reset(0);
45
        $this->assertVersion0();
46
    }
47
48
    /**
49
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
50
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
51
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
52
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
53
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
54
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
55
     */
56
    public function testUpVersion1()
57
    {
58
        $this->migrate->reset(0);
59
        $this->assertVersion0();
60
        $this->migrate->up(1);
61
        $this->assertVersion1();
62
    }
63
64
    /**
65
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
66
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
67
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
68
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
69
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
70
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
71
     */
72
    public function testUpVersion2()
73
    {
74
        $this->migrate->reset(0);
75
        $this->assertVersion0();
76
        $this->migrate->up(2);
77
        $this->assertVersion2();
78
    }
79
80
    /**
81
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
82
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
83
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
84
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
85
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
86
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
87
     */
88
    public function testDownVersion1()
89
    {
90
        $this->migrate->reset();
91
        $this->assertVersion2();
92
        $this->migrate->down(1);
93
        $this->assertVersion1();
94
    }
95
96
    /**
97
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
98
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
99
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
100
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
101
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
102
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
103
     */
104
    public function testDownVersion0()
105
    {
106
        $this->migrate->reset();
107
        $this->assertVersion2();
108
        $this->migrate->down(0);
109
        $this->assertVersion0();
110
    }
111
112
    protected function getExpectedUsersVersion0()
113
    {
114
        return [
115
            ["id" => 1, "name" => 'John Doe', 'createdate' => '20160110'],
116
            ["id" => 2, "name" => 'Jane Doe', 'createdate' => '20151230']
117
        ];
118
    }
119
120
    protected function getExpectedUsersVersion1()
121
    {
122
        return [
123
            ["id" => 1, "name" => 'John Doe', 'createdate' => '2016-01-10'],
124
            ["id" => 2, "name" => 'Jane Doe', 'createdate' => '2015-12-30']
125
        ];
126
    }
127
128
    /**
129
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
130
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
131
     */
132 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...
133
    {
134
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
135
        $this->assertEquals(0, $version);
136
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
137
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
138
139
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
140
141
        $this->assertTrue($iterator->hasNext());
142
        $row = $iterator->moveNext();
143
        $this->assertEquals(
144
            $this->getExpectedUsersVersion0()[0],
145
            $row->toArray()
146
        );
147
148
        $this->assertTrue($iterator->hasNext());
149
        $row = $iterator->moveNext();
150
        $this->assertEquals(
151
            $this->getExpectedUsersVersion0()[1],
152
            $row->toArray()
153
        );
154
155
        $this->assertFalse($iterator->hasNext());
156
157
        try {
158
            $this->migrate->getDbDriver()->getIterator('select * from roles');
159
        } catch (PDOException $ex) {
160
            $this->assertTrue(true);
161
        }
162
    }
163
164
    /**
165
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
166
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
167
     */
168 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...
169
    {
170
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
171
        $this->assertEquals(1, $version);
172
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
173
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
174
175
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
176
177
        $this->assertTrue($iterator->hasNext());
178
        $row = $iterator->moveNext();
179
        $this->assertEquals(
180
            $this->getExpectedUsersVersion1()[0],
181
            $row->toArray()
182
        );
183
184
        $this->assertTrue($iterator->hasNext());
185
        $row = $iterator->moveNext();
186
        $this->assertEquals(
187
            $this->getExpectedUsersVersion1()[1],
188
            $row->toArray()
189
        );
190
191
        $this->assertFalse($iterator->hasNext());
192
193
        try {
194
            $this->migrate->getDbDriver()->getIterator('select * from roles');
195
        } catch (PDOException $ex) {
196
            $this->assertTrue(true);
197
        }
198
    }
199
200
    /**
201
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
202
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
203
     */
204 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...
205
    {
206
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
207
        $this->assertEquals(2, $version);
208
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
209
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
210
211
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
212
213
        $this->assertTrue($iterator->hasNext());
214
        $row = $iterator->moveNext();
215
        $this->assertEquals(
216
            $this->getExpectedUsersVersion1()[0],
217
            $row->toArray()
218
        );
219
220
        $this->assertTrue($iterator->hasNext());
221
        $row = $iterator->moveNext();
222
        $this->assertEquals(
223
            $this->getExpectedUsersVersion1()[1],
224
            $row->toArray()
225
        );
226
227
        $this->assertFalse($iterator->hasNext());
228
229
        $this->migrate->getDbDriver()->getIterator('select * from roles');
230
    }
231
232
    /**
233
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
234
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
235
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
236
     * @expectedException \ByJG\DbMigration\Exception\DatabaseNotVersionedException
237
     */
238
    public function testGetCurrentVersionIsEmpty()
239
    {
240
        $this->migrate->getCurrentVersion();
241
    }
242
243
    /**
244
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
245
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
246
     */
247
    public function testCreateVersion()
248
    {
249
        $this->migrate->createVersion();
250
        $records = $this->migrate->getDbDriver()->getIterator("select * from " . $this->migrationTable)->toArray();
251
        $this->assertEquals([
252
            [
253
                'version' => '0',
254
                'status' => Migration::VERSION_STATUS_UNKNOWN
255
            ]
256
        ], $records);
257
258
        // Check Bug (cannot create twice)
259
        $this->migrate->createVersion();
260
        $records = $this->migrate->getDbDriver()->getIterator("select * from " . $this->migrationTable)->toArray();
261
        $this->assertEquals([
262
            [
263
                'version' => '0',
264
                'status' => Migration::VERSION_STATUS_UNKNOWN
265
            ]
266
        ], $records);
267
    }
268
}
269