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 |
||
| 9 | class MysqlChangelogRepositoryTest extends PHPUnit_Framework_TestCase |
||
| 10 | { |
||
| 11 | |||
| 12 | protected static $connection; |
||
| 13 | |||
| 14 | public static function setUpBeforeClass() |
||
| 15 | { |
||
| 16 | $extendedPdo = new ExtendedPdo('sqlite::memory:'); |
||
| 17 | $extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`"); |
||
| 18 | |||
| 19 | $extendedPdo->exec(" |
||
| 20 | CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`changelog` ( |
||
| 21 | `id` integer PRIMARY KEY AUTOINCREMENT, |
||
| 22 | `hash` char(40) NOT NULL, |
||
| 23 | `message` text, |
||
| 24 | `message_short` varchar(100), |
||
| 25 | `datetime` datetime NOT NULL, |
||
| 26 | `author` varchar(50) NOT NULL, |
||
| 27 | `commit_link` varchar(100) NOT NULL, |
||
| 28 | `created_at` datetime, |
||
| 29 | `updated_at` datetime |
||
| 30 | )" |
||
| 31 | ); |
||
| 32 | |||
| 33 | self::$connection = new ConnectionLocator(function () use ($extendedPdo) { |
||
| 34 | return $extendedPdo; |
||
| 35 | }); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function testIsInstanceOfChangelogRepository() |
||
| 39 | { |
||
| 40 | $repository = new MysqlChangelogRepository(self::$connection); |
||
| 41 | |||
| 42 | $this->assertInstanceOf( |
||
| 43 | 'Jacobemerick\Web\Domain\Stream\Changelog\MysqlChangelogRepository', |
||
| 44 | $repository |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function testImplementsChangelogInterface() |
||
| 49 | { |
||
| 50 | $repository = new MysqlChangelogRepository(self::$connection); |
||
| 51 | |||
| 52 | $this->assertInstanceOf( |
||
| 53 | 'Jacobemerick\Web\Domain\Stream\Changelog\ChangelogRepositoryInterface', |
||
| 54 | $repository |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testConstructSetsConnections() |
||
| 59 | { |
||
| 60 | $respository = new MysqlChangelogRepository(self::$connection); |
||
| 61 | |||
| 62 | $this->assertAttributeSame( |
||
| 63 | self::$connection, |
||
| 64 | 'connections', |
||
| 65 | $respository |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testGetChanges() |
||
| 70 | { |
||
| 71 | $testData = [ |
||
| 72 | [ |
||
| 73 | 'id' => rand(1, 100), |
||
| 74 | 'message' => 'test one', |
||
| 75 | ], |
||
| 76 | [ |
||
| 77 | 'id' => rand(101, 200), |
||
| 78 | 'message' => 'test two', |
||
| 79 | ], |
||
| 80 | ]; |
||
| 81 | |||
| 82 | array_walk($testData, [$this, 'insertData']); |
||
| 83 | |||
| 84 | $repository = new MysqlChangelogRepository(self::$connection); |
||
| 85 | $data = $repository->getChanges(); |
||
| 86 | |||
| 87 | $this->assertNotFalse($data); |
||
| 88 | $this->assertInternalType('array', $data); |
||
| 89 | foreach ($testData as $key => $testRow) { |
||
| 90 | $this->assertInternalType('array', $data[$key]); |
||
| 91 | $this->assertArraySubset($testRow, $data[$key]); |
||
| 92 | $this->assertArrayHasKey('id', $data[$key]); |
||
| 93 | $this->assertArrayHasKey('message', $data[$key]); |
||
| 94 | $this->assertArrayHasKey('message_short', $data[$key]); |
||
| 95 | $this->assertArrayHasKey('datetime', $data[$key]); |
||
| 96 | $this->assertArrayHasKey('commit_link', $data[$key]); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testGetChangesFailure() |
||
| 101 | { |
||
| 102 | $repository = new MysqlChangelogRepository(self::$connection); |
||
| 103 | $data = $repository->getChanges(); |
||
| 104 | |||
| 105 | $this->assertEmpty($data); |
||
| 106 | $this->assertInternalType('array', $data); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testGetChangesRange() |
||
| 110 | { |
||
| 111 | $testData = [ |
||
| 112 | [ |
||
| 113 | 'id' => rand(1, 100), |
||
| 114 | 'message' => 'test one', |
||
| 115 | ], |
||
| 116 | [ |
||
| 117 | 'id' => rand(101, 200), |
||
| 118 | 'message' => 'test two', |
||
| 119 | ], |
||
| 120 | [ |
||
| 121 | 'id' => rand(201, 300), |
||
| 122 | 'message' => 'test three', |
||
| 123 | ], |
||
| 124 | ]; |
||
| 125 | |||
| 126 | array_walk($testData, [$this, 'insertData']); |
||
| 127 | |||
| 128 | $repository = new MysqlChangelogRepository(self::$connection); |
||
| 129 | $data = $repository->getChanges(2, 1); |
||
| 130 | |||
| 131 | $this->assertNotFalse($data); |
||
| 132 | $this->assertInternalType('array', $data); |
||
| 133 | $this->assertCount(2, $data); |
||
| 134 | |||
| 135 | $testData = array_slice($testData, 1, 2); |
||
| 136 | |||
| 137 | foreach ($testData as $key => $testRow) { |
||
| 138 | $this->assertInternalType('array', $testRow); |
||
| 139 | $this->assertArraySubset($testRow, $data[$key]); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testGetChangesRangeFailure() |
||
| 144 | { |
||
| 145 | $testData = [ |
||
| 146 | [ |
||
| 147 | 'id' => rand(1, 100), |
||
| 148 | 'message' => 'test one', |
||
| 149 | ], |
||
| 150 | [ |
||
| 151 | 'id' => rand(101, 200), |
||
| 152 | 'message' => 'test two', |
||
| 153 | ], |
||
| 154 | ]; |
||
| 155 | |||
| 156 | array_walk($testData, [$this, 'insertData']); |
||
| 157 | |||
| 158 | $repository = new MysqlChangelogRepository(self::$connection); |
||
| 159 | $data = $repository->getChanges(1, 3); |
||
| 160 | |||
| 161 | $this->assertEmpty($data); |
||
| 162 | $this->assertInternalType('array', $data); |
||
| 163 | } |
||
| 164 | |||
| 165 | protected function insertData(array $data) |
||
| 166 | { |
||
| 167 | $defaultData = [ |
||
| 168 | 'id' => null, |
||
| 169 | 'hash' => '', |
||
| 170 | 'message' => null, |
||
| 171 | 'message_short' => null, |
||
| 172 | 'datetime' => '', |
||
| 173 | 'author' => '', |
||
| 174 | 'commit_link' => '', |
||
| 175 | ]; |
||
| 176 | |||
| 177 | $data = array_merge($defaultData, $data); |
||
| 178 | |||
| 179 | return self::$connection->getDefault()->perform(" |
||
| 180 | INSERT INTO `jpemeric_stream`.`changelog` |
||
| 181 | (id, hash, message, message_short, datetime, author, commit_link) |
||
| 182 | VALUES |
||
| 183 | (:id, :hash, :message, :message_short, :datetime, :author, :commit_link)", |
||
| 184 | $data |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | |||
| 188 | protected function tearDown() |
||
| 189 | { |
||
| 190 | self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`changelog`"); |
||
| 191 | } |
||
| 192 | |||
| 193 | public static function tearDownAfterClass() |
||
| 194 | { |
||
| 195 | self::$connection->getDefault()->disconnect(); |
||
| 196 | unlink('jpemeric_stream.db'); |
||
| 197 | } |
||
| 198 | } |
||
| 199 |