Passed
Push — master ( a506e2...902f0a )
by Joao
05:33 queued 11s
created

BaseDatabase::testIsDatabaseVersioned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
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
    protected function getExpectedPostsVersion2()
136
    {
137
        return [
138
            ["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 -->"],
139
        ];
140
    }
141
142
    /**
143
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
144
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
145
     */
146
    protected function assertVersion0()
147
    {
148
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
149
        $this->assertEquals(0, $version);
150
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
151
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
152
153
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
154
155
        $this->assertTrue($iterator->hasNext());
156
        $row = $iterator->moveNext();
157
        $this->assertEquals(
158
            $this->getExpectedUsersVersion0()[0],
159
            $row->toArray()
160
        );
161
162
        $this->assertTrue($iterator->hasNext());
163
        $row = $iterator->moveNext();
164
        $this->assertEquals(
165
            $this->getExpectedUsersVersion0()[1],
166
            $row->toArray()
167
        );
168
169
        $this->assertFalse($iterator->hasNext());
170
171
        try {
172
            $this->migrate->getDbDriver()->getIterator('select * from roles');
173
        } catch (PDOException $ex) {
174
            $this->assertTrue(true);
175
        }
176
    }
177
178
    /**
179
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
180
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
181
     */
182
    protected function assertVersion1()
183
    {
184
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
185
        $this->assertEquals(1, $version);
186
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
187
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
188
189
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
190
191
        $this->assertTrue($iterator->hasNext());
192
        $row = $iterator->moveNext();
193
        $this->assertEquals(
194
            $this->getExpectedUsersVersion1()[0],
195
            $row->toArray()
196
        );
197
198
        $this->assertTrue($iterator->hasNext());
199
        $row = $iterator->moveNext();
200
        $this->assertEquals(
201
            $this->getExpectedUsersVersion1()[1],
202
            $row->toArray()
203
        );
204
205
        $this->assertFalse($iterator->hasNext());
206
207
        try {
208
            $this->migrate->getDbDriver()->getIterator('select * from roles');
209
        } catch (PDOException $ex) {
210
            $this->assertTrue(true);
211
        }
212
    }
213
214
    /**
215
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
216
     * @throws \ByJG\Serializer\Exception\InvalidArgumentException
217
     */
218
    protected function assertVersion2()
219
    {
220
        $version = $this->migrate->getDbDriver()->getScalar('select version from '. $this->migrationTable);
221
        $this->assertEquals(2, $version);
222
        $status = $this->migrate->getDbDriver()->getScalar('select status from '. $this->migrationTable);
223
        $this->assertEquals(Migration::VERSION_STATUS_COMPLETE, $status);
224
225
        // Users
226
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from users');
227
228
        $this->assertTrue($iterator->hasNext());
229
        $row = $iterator->moveNext();
230
        $this->assertEquals(
231
            $this->getExpectedUsersVersion1()[0],
232
            $row->toArray()
233
        );
234
235
        $this->assertTrue($iterator->hasNext());
236
        $row = $iterator->moveNext();
237
        $this->assertEquals(
238
            $this->getExpectedUsersVersion1()[1],
239
            $row->toArray()
240
        );
241
242
        $this->assertFalse($iterator->hasNext());
243
244
        // Posts
245
        $iterator = $this->migrate->getDbDriver()->getIterator('select * from posts');
246
247
        $this->assertTrue($iterator->hasNext());
248
        $row = $iterator->moveNext();
249
        $this->assertEquals(
250
            $this->getExpectedPostsVersion2()[0],
251
            $row->toArray()
252
        );
253
    }
254
255
    /**
256
     * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
257
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
258
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
259
     * @expectedException \ByJG\DbMigration\Exception\DatabaseNotVersionedException
260
     */
261
    public function testGetCurrentVersionIsEmpty()
262
    {
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