BaseDatabase   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 18
eloc 111
c 5
b 0
f 0
dl 0
loc 284
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateVersion() 0 20 1
A getExpectedUsersVersion1() 0 5 1
A getExpectedPostsVersion2() 0 4 1
A testIsDatabaseVersioned() 0 5 1
A testDownVersion1() 0 6 1
A tearDown() 0 3 1
A assertVersion2() 0 34 1
A testUpVersion2() 0 6 1
A testDownVersion0() 0 6 1
A assertVersion0() 0 29 2
A testVersion0() 0 4 1
A testUpVersion1() 0 6 1
A setUp() 0 5 1
A assertVersion1() 0 29 2
A getExpectedUsersVersion0() 0 5 1
A testGetCurrentVersionIsEmpty() 0 4 1
1
<?php
2
3
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
4
use ByJG\DbMigration\Migration;
5
6
abstract class BaseDatabase extends \PHPUnit\Framework\TestCase
7
{
8
    protected $uri = null;
9
10
    /**
11
     * @var Migration
12
     */
13
    protected $migrate = null;
14
15
    protected $migrationTable = "migration_version";
16
17
    /**
18
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
19
     */
20
    public function setUp(): void
21
    {
22
        // create Migrate object in the parent!!!
23
24
        $this->migrate->prepareEnvironment();
25
    }
26
27
    /**
28
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
29
     */
30
    public function tearDown(): void
31
    {
32
        $this->migrate->getDbCommand()->dropDatabase();
33
    }
34
35
    /**
36
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
37
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
38
     * @throws DatabaseNotVersionedException
39
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
40
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
41
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
42
     */
43
    public function testVersion0()
44
    {
45
        $this->migrate->reset(0);
46
        $this->assertVersion0();
47
    }
48
49
    public function testIsDatabaseVersioned()
50
    {
51
        $this->assertFalse($this->migrate->isDatabaseVersioned());
52
        $this->migrate->reset();
53
        $this->assertTrue($this->migrate->isDatabaseVersioned());
54
    }
55
56
    /**
57
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
58
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
59
     * @throws DatabaseNotVersionedException
60
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
61
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
62
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
63
     */
64
    public function testUpVersion1()
65
    {
66
        $this->migrate->reset(0);
67
        $this->assertVersion0();
68
        $this->migrate->up(1);
69
        $this->assertVersion1();
70
    }
71
72
    /**
73
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
74
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
75
     * @throws DatabaseNotVersionedException
76
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
77
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
78
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
79
     */
80
    public function testUpVersion2()
81
    {
82
        $this->migrate->reset(0);
83
        $this->assertVersion0();
84
        $this->migrate->up(2);
85
        $this->assertVersion2();
86
    }
87
88
    /**
89
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
90
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
91
     * @throws DatabaseNotVersionedException
92
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
93
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
94
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
95
     */
96
    public function testDownVersion1()
97
    {
98
        $this->migrate->reset();
99
        $this->assertVersion2();
100
        $this->migrate->down(1);
101
        $this->assertVersion1();
102
    }
103
104
    /**
105
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
106
     * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
107
     * @throws DatabaseNotVersionedException
108
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
109
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
110
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
111
     */
112
    public function testDownVersion0()
113
    {
114
        $this->migrate->reset();
115
        $this->assertVersion2();
116
        $this->migrate->down(0);
117
        $this->assertVersion0();
118
    }
119
120
    protected function getExpectedUsersVersion0()
121
    {
122
        return [
123
            ["id" => 1, "name" => 'John Doe', 'createdate' => '20160110'],
124
            ["id" => 2, "name" => 'Jane Doe', 'createdate' => '20151230']
125
        ];
126
    }
127
128
    protected function getExpectedUsersVersion1()
129
    {
130
        return [
131
            ["id" => 1, "name" => 'John Doe', 'createdate' => '2016-01-10'],
132
            ["id" => 2, "name" => 'Jane Doe', 'createdate' => '2015-12-30']
133
        ];
134
    }
135
136
    protected function getExpectedPostsVersion2()
137
    {
138
        return [
139
            ["id" => 1, "userid" => 1, "title" => 'Testing', 'post' => "<!-- wp:paragraph -->\\n<p>This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>\\n<!-- /wp:paragraph -->\\n\\n<!-- wp:quote -->\\n<blockquote class=\"wp-block-quote\"><p>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin' caught in the rain.)</p></blockquote>\\n<!-- /wp:quote -->\\n\\n<!-- wp:paragraph -->\\n<p>...or something like this:</p>\\n<!-- /wp:paragraph -->\\n\\n<!-- wp:quote -->\\n<blockquote class=\"wp-block-quote\"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>\\n<!-- /wp:quote -->\\n\\n<!-- wp:paragraph -->\\n<p>As a new WordPress user, you should go to <a href=\"http://home.home.lcl/wordpress/wp-admin/\">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>\\n<!-- /wp:paragraph -->"],
140
        ];
141
    }
142
143
    /**
144
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
145
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
146
     */
147
    protected function assertVersion0()
148
    {
149
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
150
        $this->assertEquals(0, $version);
151
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
152
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
153
154
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
155
156
        $this->assertTrue($iterator->hasNext());
157
        $row = $iterator->moveNext();
158
        $this->assertEquals(
159
            $this->getExpectedUsersVersion0()[0],
160
            $row->toArray()
161
        );
162
163
        $this->assertTrue($iterator->hasNext());
164
        $row = $iterator->moveNext();
165
        $this->assertEquals(
166
            $this->getExpectedUsersVersion0()[1],
167
            $row->toArray()
168
        );
169
170
        $this->assertFalse($iterator->hasNext());
171
172
        try {
173
            $this->migrate->getDbDriver()->getIterator('select * from roles');
174
        } catch (PDOException $ex) {
175
            $this->assertTrue(true);
176
        }
177
    }
178
179
    /**
180
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
181
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
182
     */
183
    protected function assertVersion1()
184
    {
185
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
186
        $this->assertEquals(1, $version);
187
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
188
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
189
190
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
191
192
        $this->assertTrue($iterator->hasNext());
193
        $row = $iterator->moveNext();
194
        $this->assertEquals(
195
            $this->getExpectedUsersVersion1()[0],
196
            $row->toArray()
197
        );
198
199
        $this->assertTrue($iterator->hasNext());
200
        $row = $iterator->moveNext();
201
        $this->assertEquals(
202
            $this->getExpectedUsersVersion1()[1],
203
            $row->toArray()
204
        );
205
206
        $this->assertFalse($iterator->hasNext());
207
208
        try {
209
            $this->migrate->getDbDriver()->getIterator('select * from roles');
210
        } catch (PDOException $ex) {
211
            $this->assertTrue(true);
212
        }
213
    }
214
215
    /**
216
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
217
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
218
     */
219
    protected function assertVersion2()
220
    {
221
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
222
        $this->assertEquals(2, $version);
223
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
224
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
225
226
        // Users
227
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
228
229
        $this->assertTrue($iterator->hasNext());
230
        $row = $iterator->moveNext();
231
        $this->assertEquals(
232
            $this->getExpectedUsersVersion1()[0],
233
            $row->toArray()
234
        );
235
236
        $this->assertTrue($iterator->hasNext());
237
        $row = $iterator->moveNext();
238
        $this->assertEquals(
239
            $this->getExpectedUsersVersion1()[1],
240
            $row->toArray()
241
        );
242
243
        $this->assertFalse($iterator->hasNext());
244
245
        // Posts
246
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from posts');
247
248
        $this->assertTrue($iterator->hasNext());
249
        $row = $iterator->moveNext();
250
        $this->assertEquals(
251
            $this->getExpectedPostsVersion2()[0],
252
            $row->toArray()
253
        );
254
    }
255
256
    /**
257
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
258
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
259
     */
260
    public function testGetCurrentVersionIsEmpty()
261
    {
262
        $this->expectException(DatabaseNotVersionedException::class);
263
        $this->migrate->getCurrentVersion();
264
    }
265
266
    /**
267
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
268
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
269
     */
270
    public function testCreateVersion()
271
    {
272
        $this->migrate->createVersion();
273
        $records = $this->migrate->getDbDriver()->getIterator("select * from " . $this->migrationTable)->toArray();
274
        $this->assertEquals([
275
            [
276
                'version' => '0',
277
                'status' => Migration::VERSION_STATUS_UNKNOWN
278
            ]
279
        ], $records);
280
281
        // Check Bug (cannot create twice)
282
        $this->migrate->createVersion();
283
        $records = $this->migrate->getDbDriver()->getIterator("select * from " . $this->migrationTable)->toArray();
284
        $this->assertEquals([
285
            [
286
                'version' => '0',
287
                'status' => Migration::VERSION_STATUS_UNKNOWN
288
            ]
289
        ], $records);
290
    }
291
}
292