Completed
Push — master ( a79949...86c63e )
by Joao
12s queued 10s
created

BaseDatabase::testIsDatabaseVersioned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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