Passed
Push — develop ( 415154...b14201 )
by Andreas
03:08
created

SKUTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 137
Duplicated Lines 14.6 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 2
dl 20
loc 137
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
use Wambo\Catalog\Model\SKU;
3
4
/**
5
 * Class SKUTest contains tests for the Wambo\Catalog\Model\SKU class.
6
 */
7
class SKUTest extends PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * If the given SKU is valid validateSKU should not throw an exception
11
     *
12
     * @test
13
     * @dataProvider getValidSKUs
14
     *
15
     * @param string $sku A SKU
16
     */
17
    public function validateSKU_validSKUs_NoExceptionIsThrown($sku)
18
    {
19
        // act
20
        try {
21
22
            new SKU($sku);
23
            $exceptionThrown = false;
24
25
        } catch (Exception $validationException) {
26
            $exceptionThrown = true;
27
        }
28
29
        // assert
30
        $this->assertFalse($exceptionThrown, "validateSKU('$sku') should not have thrown an exception");
31
    }
32
33
    /**
34
     * If the given SKU is invalid validateSKU should throw an exception
35
     *
36
     * @test
37
     * @dataProvider getInvalidSKUs
38
     *
39
     * @param string $sku                      A SKU
40
     * @param string $expectedExceptionMessage The expected exception message
41
     */
42
    public function validateSKU_invalidSKUs_ExceptionIsThrown($sku, $expectedExceptionMessage)
43
    {
44
        // act
45
        try {
46
47
            new SKU($sku);
48
            $exceptionThrown = false;
49
50
        } catch (Exception $validationException) {
51
52
            // assert
53
            $exceptionThrown = true;
54
            $this->assertContains($expectedExceptionMessage, $validationException->getMessage(),
55
                "validateSKU('$sku') should have thrown an exception with the message: $expectedExceptionMessage");
56
57
        }
58
59
        // assert
60
        $this->assertTrue($exceptionThrown, "validateSKU('$sku') should have thrown an exception");
61
    }
62
63
    /**
64
     * Get a list of valid SKUs
65
     *
66
     * @return array
67
     */
68
    public static function getValidSKUs()
69
    {
70
        return [
71
            ["abcdefghijklmnopqrstuvwxyz012345"], // max length <= 32
72
            ["abcdefghijklmnopqrstuvwxyz"], // the whole alphabet
73
            ["123456789"], // only digits
74
            ["00000111111111"], // leading zeros
75
            ["a-product"], // dashes
76
            ["product1"], // characters and digits
77
            ["product112345678"], // characters and digits
78
            ["product-1"],
79
            ["product-112345678"],
80
            ["ab"], // min length >= 2
81
            ["12"], // min length >= 2
82
        ];
83
    }
84
85
    /**
86
     * Get a list of invalid SKUs
87
     *
88
     * @return array
89
     */
90
    public static function getInvalidSKUs()
91
    {
92
        // format: [ "sku", "expected exception messages" ]
93
        return [
94
            // empty
95
            ["", "empty"],
96
97
            // white space
98
            [" product", "white space"],
99
            ["product ", "white space"],
100
            [" product ", "white space"],
101
            ["pro duct", "white space"],
102
103
            // invalid characters
104
            ['7$tshirt', "invalid characters"], // Dollar sign
105
            ['gâteau', "invalid characters"], // French umlaut
106
            ['käse', "invalid characters"], // German umlaut
107
            ['product/1', "invalid characters"],
108
            ['product:1', "invalid characters"],
109
            ['product.1', "invalid characters"],
110
            ['product(1)', "invalid characters"],
111
            ['product§1', "invalid characters"],
112
            ['👃-spray', "invalid characters"], // nose emoji
113
            ['Åre', "invalid characters"], // Swedish umlaut
114
            ['Öresund', "invalid characters"], // German umlaut
115
            ['наушник', "invalid characters"], // Russian
116
            ['이어폰', "invalid characters"], // Korean
117
118
            // invalid prefix
119
            ['-product', "cannot start"],
120
121
122
            // invalid postfix
123
            ['product-', "cannot end"],
124
125
            // uppercase characters
126
            ["Product-123", "uppercase"],
127
            ["pro-Duct-123", "uppercase"],
128
            ["AAA", "uppercase"],
129
            ["aBc", "uppercase"],
130
            ["abC", "uppercase"],
131
            ["abC", "uppercase"],
132
133
            // minimum length
134
            ["a", "too short"],
135
            ["1", "too short"],
136
            ["0", "too short"],
137
138
            // maximum length
139
            ["abcdefghijklmnopqrstuvwxyz0123456789", "too long"],
140
141
        ];
142
    }
143
}
144