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 |
||
| 8 | class PaymentReferenceTest extends TestCase |
||
| 9 | { |
||
| 10 | public function testValidQrReference() |
||
| 11 | { |
||
| 12 | $paymentReference = new PaymentReference(); |
||
| 13 | $paymentReference->setType(PaymentReference::TYPE_QR); |
||
| 14 | $paymentReference->setReference('012345678901234567890123456'); |
||
| 15 | |||
| 16 | $this->assertSame(0, $paymentReference->getViolations()->count()); |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @dataProvider invalidQrReferenceProvider |
||
| 21 | */ |
||
| 22 | public function testInvalidQrReference($value) |
||
| 23 | { |
||
| 24 | $paymentReference = new PaymentReference(); |
||
| 25 | $paymentReference->setType(PaymentReference::TYPE_QR); |
||
| 26 | $paymentReference->setReference($value); |
||
| 27 | |||
| 28 | $this->assertSame(1, $paymentReference->getViolations()->count()); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function invalidQrReferenceProvider() |
||
| 32 | { |
||
| 33 | return [ |
||
| 34 | ['01234567890123456789012345'], // too short |
||
| 35 | ['0123456789012345678901234567'], // too long |
||
| 36 | ['Ä12345678901234567890123456'] // invalid characters |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testValidScorReference() |
||
| 41 | { |
||
| 42 | $paymentReference = new PaymentReference(); |
||
| 43 | $paymentReference->setType(PaymentReference::TYPE_SCOR); |
||
| 44 | $paymentReference->setReference('RF18539007547034'); |
||
| 45 | |||
| 46 | $this->assertSame(0, $paymentReference->getViolations()->count()); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @dataProvider invalidScorReferenceProvider |
||
| 51 | */ |
||
| 52 | public function testInvalidScorReference($value) |
||
| 53 | { |
||
| 54 | $paymentReference = new PaymentReference(); |
||
| 55 | $paymentReference->setType(PaymentReference::TYPE_SCOR); |
||
| 56 | $paymentReference->setReference($value); |
||
| 57 | |||
| 58 | $this->assertSame(1, $paymentReference->getViolations()->count()); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function invalidScorReferenceProvider() |
||
| 62 | { |
||
| 63 | return [ |
||
| 64 | ['RF12'],// too short |
||
| 65 | ['RF181234567890123456789012'], // too long |
||
| 66 | ['RF1853900754703Ä'] // invalid characters |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | } |