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 |
||
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() |
||
132 | ]; |
||
133 | } |
||
134 | |||
135 | protected function getExpectedPostsVersion2() |
||
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() |
||
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() |
||
290 | } |
||
291 | } |
||
292 |