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 |
||
| 19 | final class VersionTest extends PHPUnit_Framework_TestCase |
||
| 20 | { |
||
| 21 | //////////////////////////////////////////////////////////////////////////// |
||
| 22 | // __construct() |
||
| 23 | //////////////////////////////////////////////////////////////////////////// |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @dataProvider provideValidVersion |
||
| 27 | * |
||
| 28 | * @param string $unused |
||
| 29 | * @param int $major |
||
| 30 | * @param int $minor |
||
| 31 | * @param int $patch |
||
| 32 | * @param string $preRelease |
||
| 33 | * @param string $build |
||
| 34 | */ |
||
| 35 | public function testConstructor( |
||
| 36 | string $unused, |
||
| 37 | int $major, |
||
| 38 | int $minor, |
||
| 39 | int $patch, |
||
| 40 | string $preRelease, |
||
| 41 | string $build |
||
| 42 | ) { |
||
| 43 | $version = new Version($major, $minor, $patch, $preRelease, $build); |
||
| 44 | static::assertEquals( |
||
| 45 | $major, |
||
| 46 | $version->getMajor(), |
||
| 47 | '__construct() takes the major version as its first argument' |
||
| 48 | ); |
||
| 49 | static::assertEquals( |
||
| 50 | $minor, |
||
| 51 | $version->getMinor(), |
||
| 52 | '__construct() takes the minor version as its second argument' |
||
| 53 | ); |
||
| 54 | static::assertEquals( |
||
| 55 | $patch, |
||
| 56 | $version->getPatch(), |
||
| 57 | '__construct() takes the patch version as its third argument' |
||
| 58 | ); |
||
| 59 | static::assertEquals( |
||
| 60 | $preRelease, |
||
| 61 | $version->getPreRelease(), |
||
| 62 | '__construct() takes the pre-release version as its forth argument' |
||
| 63 | ); |
||
| 64 | static::assertEquals( |
||
| 65 | $build, |
||
| 66 | $version->getBuild(), |
||
| 67 | '__construct() takes the build version as its fifth argument' |
||
| 68 | ); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @dataProvider provideWrongPreReleaseVersion |
||
| 73 | * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException |
||
| 74 | * @expectedExceptionMessage The pre-release version is not compatible with rule 9 of the specifications. |
||
| 75 | * |
||
| 76 | * @param string $preRelease |
||
| 77 | */ |
||
| 78 | public function testConstructorFailsIfPreReleaseNotValid(string $preRelease) |
||
| 79 | { |
||
| 80 | new Version(10, 12, 14, $preRelease); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @dataProvider provideWrongPreReleaseVersion |
||
| 85 | * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException |
||
| 86 | * @expectedExceptionMessage The build version is not compatible with rule 10 of the specifications. |
||
| 87 | * |
||
| 88 | * @param string $build |
||
| 89 | */ |
||
| 90 | public function testConstructorFailsIfBuildNotValid(string $build) |
||
| 91 | { |
||
| 92 | new Version(10, 12, 14, '', $build); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** @return array */ |
||
| 96 | public function provideWrongPreReleaseVersion() : array |
||
| 97 | { |
||
| 98 | return [ |
||
| 99 | ['é'], |
||
| 100 | ['"'], |
||
| 101 | ['~'], |
||
| 102 | ['+'], |
||
| 103 | ['+R'], |
||
| 104 | ['R+'], |
||
| 105 | ['.RC'], |
||
| 106 | ['.'], |
||
| 107 | ['RC.'], |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | |||
| 111 | //////////////////////////////////////////////////////////////////////////// |
||
| 112 | // fromString() |
||
| 113 | //////////////////////////////////////////////////////////////////////////// |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @dataProvider provideValidVersion |
||
| 117 | * |
||
| 118 | * @param string $string |
||
| 119 | * @param int $expectedMajor |
||
| 120 | * @param int $expectedMinor |
||
| 121 | * @param int $expectedPatch |
||
| 122 | * @param string $expectedPreRelease |
||
| 123 | * @param string $exceptedBuild |
||
| 124 | */ |
||
| 125 | public function testFromString( |
||
| 126 | string $string, |
||
| 127 | int $expectedMajor, |
||
| 128 | int $expectedMinor, |
||
| 129 | int $expectedPatch, |
||
| 130 | string $expectedPreRelease, |
||
| 131 | string $exceptedBuild |
||
| 132 | ) { |
||
| 133 | $version = Version::fromString($string); |
||
| 134 | static::assertEquals( |
||
| 135 | $expectedMajor, |
||
| 136 | $version->getMajor(), |
||
| 137 | '__construct() takes the major version as its first argument' |
||
| 138 | ); |
||
| 139 | static::assertEquals( |
||
| 140 | $expectedMinor, |
||
| 141 | $version->getMinor(), |
||
| 142 | '__construct() takes the minor version as its second argument' |
||
| 143 | ); |
||
| 144 | static::assertEquals( |
||
| 145 | $expectedPatch, |
||
| 146 | $version->getPatch(), |
||
| 147 | '__construct() takes the patch version as its third argument' |
||
| 148 | ); |
||
| 149 | static::assertEquals( |
||
| 150 | $expectedPreRelease, |
||
| 151 | $version->getPreRelease(), |
||
| 152 | '__construct() takes the pre-release version as its forth argument' |
||
| 153 | ); |
||
| 154 | static::assertEquals( |
||
| 155 | $exceptedBuild, |
||
| 156 | $version->getBuild(), |
||
| 157 | '__construct() takes the build version as its fifth argument' |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** @return array */ |
||
| 162 | public function provideValidVersion() : array |
||
| 163 | { |
||
| 164 | return [ |
||
| 165 | ['1.2.3', 1, 2, 3, '', ''], |
||
| 166 | ['1.0.0-alpha', 1, 0, 0, 'alpha', ''], |
||
| 167 | ['1.0.0-alpha.1', 1, 0, 0, 'alpha.1', ''], |
||
| 168 | ['1.0.0-0.3.7', 1, 0, 0, '0.3.7', ''], |
||
| 169 | ['1.0.0-x.7.z.92', 1, 0, 0, 'x.7.z.92', ''], |
||
| 170 | ['1.0.0-alpha+001', 1, 0, 0, 'alpha', '001'], |
||
| 171 | ['1.0.0+20130313144700', 1, 0, 0, '', '20130313144700'], |
||
| 172 | ['1.0.0-beta+exp.sha.5114f85', 1, 0, 0, 'beta', 'exp.sha.5114f85'], |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @dataProvider provideWrongStringVersion |
||
| 178 | * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException |
||
| 179 | * @expectedExceptionMessageRegExp /The string ".+" does not look like a version\./ |
||
| 180 | * |
||
| 181 | * @param string $string |
||
| 182 | */ |
||
| 183 | public function testFromStringFailsWithInvalidString(string $string) |
||
| 184 | { |
||
| 185 | Version::fromString($string); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function provideWrongStringVersion() : array |
||
| 192 | { |
||
| 193 | return [ |
||
| 194 | ['1'], |
||
| 195 | ['a'], |
||
| 196 | ['1.1'], |
||
| 197 | ['1.a'], |
||
| 198 | ['1.1.a'], |
||
| 199 | ]; |
||
| 200 | } |
||
| 201 | |||
| 202 | //////////////////////////////////////////////////////////////////////////// |
||
| 203 | // __toString() |
||
| 204 | //////////////////////////////////////////////////////////////////////////// |
||
| 205 | public function testToString() |
||
| 206 | { |
||
| 207 | $data = [ |
||
| 208 | '1.0.0', |
||
| 209 | '1.0.0-alpha.test', |
||
| 210 | '1.0.0-alpha.test+build', |
||
| 211 | '1.0.0+build', |
||
| 212 | ]; |
||
| 213 | |||
| 214 | foreach ($data as $item) { |
||
| 215 | static::assertEquals($item, (string) Version::fromString($item)); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | //////////////////////////////////////////////////////////////////////////// |
||
| 220 | // equals() |
||
| 221 | //////////////////////////////////////////////////////////////////////////// |
||
| 222 | public function testEquals() |
||
| 223 | { |
||
| 224 | $current = new Version(1, 0, 0); |
||
| 225 | $other = new Version(1, 0, 0); |
||
| 226 | static::assertEquals(true, $other->isEquals($current)); |
||
| 227 | $current = new Version(1, 1, 0); |
||
| 228 | $other = new Version(1, 0, 0); |
||
| 229 | static::assertEquals(false, $other->isEquals($current)); |
||
| 230 | } |
||
| 231 | |||
| 232 | //////////////////////////////////////////////////////////////////////////// |
||
| 233 | // greaterThan() |
||
| 234 | //////////////////////////////////////////////////////////////////////////// |
||
| 235 | public function testGreaterThan() |
||
| 236 | { |
||
| 237 | $current = new Version(1, 0, 0); |
||
| 238 | $other = new Version(1, 1, 0); |
||
| 239 | static::assertEquals(false, $current->isGreaterThan($other)); |
||
| 240 | static::assertEquals(true, $other->isGreaterThan($current)); |
||
| 241 | } |
||
| 242 | |||
| 243 | //////////////////////////////////////////////////////////////////////////// |
||
| 244 | // greaterThanOrEqual() |
||
| 245 | //////////////////////////////////////////////////////////////////////////// |
||
| 246 | public function testGreaterThanOrEqual() |
||
| 247 | { |
||
| 248 | $current = new Version(1, 0, 0); |
||
| 249 | $other = new Version(1, 1, 0); |
||
| 250 | static::assertEquals(false, $current->isGreaterThanOrEqual($other)); |
||
| 251 | static::assertEquals(true, $other->isGreaterThanOrEqual($current)); |
||
| 252 | } |
||
| 253 | |||
| 254 | //////////////////////////////////////////////////////////////////////////// |
||
| 255 | // lessThan() |
||
| 256 | //////////////////////////////////////////////////////////////////////////// |
||
| 257 | public function testLessThan() |
||
| 258 | { |
||
| 259 | $current = new Version(1, 0, 0); |
||
| 260 | $other = new Version(1, 1, 0); |
||
| 261 | static::assertEquals(true, $current->isLessThan($other)); |
||
| 262 | static::assertEquals(false, $other->isLessThan($current)); |
||
| 263 | } |
||
| 264 | |||
| 265 | //////////////////////////////////////////////////////////////////////////// |
||
| 266 | // lessThanOrEqual() |
||
| 267 | //////////////////////////////////////////////////////////////////////////// |
||
| 268 | public function testLessThanOrEqual() |
||
| 269 | { |
||
| 270 | $current = new Version(1, 0, 0); |
||
| 271 | $other = new Version(1, 1, 0); |
||
| 272 | static::assertEquals(true, $current->isLessThanOrEqual($other)); |
||
| 273 | static::assertEquals(false, $other->isLessThanOrEqual($current)); |
||
| 274 | } |
||
| 275 | } |
||
| 276 |