Completed
Push — master ( 38815c...5ae367 )
by Manuel
03:51
created

QrPaymentReferenceGeneratorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 16.84 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 16
loc 95
c 0
b 0
f 0
rs 10

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Sprain\SwissQrBill\Tests\Reference;
4
5
use PHPUnit\Framework\TestCase;
6
use Sprain\SwissQrBill\Reference\QrPaymentReferenceGenerator;
7
use Symfony\Component\Validator\Validation;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
class QrPaymentReferenceGeneratorTest extends TestCase
11
{
12
    /** @var  ValidatorInterface */
13
    private $validator;
14
15
    public function setUp()
16
    {
17
        $this->validator = Validation::createValidatorBuilder()
18
            ->addMethodMapping('loadValidatorMetadata')
19
            ->getValidator();
20
    }
21
22
    /**
23
     * @dataProvider qrPaymentReferenceProvider
24
     */
25
    public function testValidQrPaymentReference($customerIdentification, $referenceNumber, $expectedResult)
26
    {
27
        $qrReference = (new QrPaymentReferenceGenerator())
28
            ->setCustomerIdentificationNumber($customerIdentification)
29
            ->setReferenceNumber($referenceNumber)
30
            ->generate();
31
32
        $this->assertSame($expectedResult, $qrReference);
33
    }
34
35
    public function qrPaymentReferenceProvider()
36
    {
37
        return [
38
            // Realistic real-life examples
39
            ['310014', '18310019779911119', '310014000183100197799111196'], // https://www.tkb.ch/download/online/BESR-Handbuch.pdf
40
            ['040329', '340 ', '040329000000000000000003406'], // https://www.lukb.ch/documents/10620/13334/LUKB-BESR-Handbuch.pdf
41
            ['247656', '3073000002311006 ', '247656000030730000023110061'], // https://hilfe.flexbuero.ch/article/1181/
42
            ['123456', '11223344', '123456000000000000112233440'],
43
44
            // Handle it as numerics as well
45
            [310014, 18310019779911119, '310014000183100197799111196'],
46
47
            // Correct handling of whitespace
48
            [' 310 014 ', ' 1831001 9779911119 ', '310014000183100197799111196'],
49
        ];
50
    }
51
52
    /**
53
     * @expectedException Sprain\SwissQrBill\Validator\Exception\InvalidQrPaymentReferenceException
54
     */
55
    public function testInvalidQrPaymentReference()
56
    {
57
        (new QrPaymentReferenceGenerator())
58
            ->generate(); // Generating without data is not valid
59
    }
60
61
    /**
62
     * @dataProvider invalidCustomerIdentificationNumberProvider
63
     */
64
    public function testCustomerIdentificationNumberMustBeValid($invalidCustomerIdentificationNumber)
65
    {
66
        $qrReference = (new QrPaymentReferenceGenerator())
67
            ->setCustomerIdentificationNumber($invalidCustomerIdentificationNumber)
68
            ->setReferenceNumber('18310019779911119');
69
70
        $this->assertSame(1, $this->validator->validate($qrReference)->count());
71
    }
72
73
    public function invalidCustomerIdentificationNumberProvider()
74
    {
75
        return [
76
            ['1234567'], // too long
77
            ['12345A'],  // non-digits
78
            ['1234.5'],   // non-digits
79
            ['']
80
        ];
81
    }
82
83
    /**
84
     * @dataProvider invalidReferenceNumberProvider
85
     */
86
    public function testReferenceNumberMustBeValid($invalidReferenceNumber)
87
    {
88
        $qrReference = (new QrPaymentReferenceGenerator())
89
            ->setCustomerIdentificationNumber('123456')
90
            ->setReferenceNumber($invalidReferenceNumber);
91
92
        $this->assertSame(1, $this->validator->validate($qrReference)->count());
93
    }
94
95
    public function invalidReferenceNumberProvider()
96
    {
97
        return [
98
            ['123456789012345678901'], // too long
99
            ['1234567890123456789A'],  // non-digits
100
            ['123456789012345678.0'],  // non-digits
101
            ['']
102
        ];
103
    }
104
}