Completed
Pull Request — develop (#24)
by Ben
06:57 queued 04:18
created

UrlReplacementTest   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 3
dl 0
loc 160
rs 10
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A testToString() 0 7 1
A testFromString() 0 4 1
A testFromStringInvalidString() 0 9 2
A testToStringAndFromStringWithInsert() 0 4 1
A testToStringAndFromStringWithSwap() 0 4 1
A testToStringAndFromStringWithDash() 0 4 1
A testParseQueryString() 0 4 1
A testParseQueryStringWithInserts() 0 4 1
A testParseQueryStringWithDash() 0 4 1
A testParseQueryStringWithDash2() 0 4 1
A testParseQueryStringWithDashMismatch() 0 9 2
A testSimpleApplyReplace() 0 4 1
A testSimpleApplyReplaceAtStart() 0 4 1
A testSimpleApplyReplaceAtStartBadIndex() 0 4 1
A testSimpleApplyReplaceAtEnd() 0 4 1
A testSimpleApplyReplaceAtEndBadIndex() 0 4 1
A testSimpleApplyInsertAtEnd() 0 4 1
A testSimpleApplyInsertAtStart() 0 4 1
A testSimpleApplyInsertAtStartBadIndex() 0 4 1
A testSimpleApplyInsertAtEndBadIndex() 0 4 1
A assertBadApply() 0 6 1
A assertApply() 0 6 1
A assertParseQuery() 0 20 4
A assertToAndFromString() 0 8 1
1
<?php
2
3
use GroupByInc\API\Url\UrlReplacement;
4
use GroupByInc\API\Url\OperationType;
5
use GroupByInc\API\Url\ParserException;
6
use GroupByInc\API\Util\StringBuilder;
7
8
class UrlReplacementTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    public function testToString()
12
    {
13
        $replacement = new UrlReplacement(2, "a", OperationType::Swap);
14
        $this->assertEquals("2-a", $replacement);
15
        $replacement = new UrlReplacement(20, "%", OperationType::Insert);
16
        $this->assertEquals("i20-%", $replacement);
17
    }
18
19
    public function testFromString()
20
    {
21
        UrlReplacement::fromString("2-a");
22
    }
23
24
    public function testFromStringInvalidString()
25
    {
26
        try {
27
            UrlReplacement::fromString("a2-a");
28
            $this->fail("Exception not thrown");
29
        } catch (ParserException $e) {
30
            // expected
31
        }
32
    }
33
34
    public function testToStringAndFromStringWithInsert()
35
    {
36
        $this->assertToAndFromString("i2-/");
37
    }
38
39
    public function testToStringAndFromStringWithSwap()
40
    {
41
        $this->assertToAndFromString("35-9");
42
    }
43
44
    public function testToStringAndFromStringWithDash()
45
    {
46
        $this->assertToAndFromString("35--");
47
    }
48
49
    public function testParseQueryString()
50
    {
51
        $this->assertParseQuery("2-a", "3-b", "4-c");
52
    }
53
54
    public function testParseQueryStringWithInserts()
55
    {
56
        $this->assertParseQuery("2-a", "i3-b", "4-c");
57
    }
58
59
    public function testParseQueryStringWithDash()
60
    {
61
        $this->assertParseQuery("2--", "i3-b", "4-c");
62
    }
63
64
    public function testParseQueryStringWithDash2()
65
    {
66
        $this->assertParseQuery("2--", "i3-b", "4--");
67
    }
68
69
    public function testParseQueryStringWithDashMismatch()
70
    {
71
        try {
72
            UrlReplacement::parseQueryString("2-a-i3-b--4-c");
73
            $this->fail("Exception not thrown");
74
        } catch (ParserException $e) {
75
            // expected
76
        }
77
    }
78
79
    public function testSimpleApplyReplace()
80
    {
81
        $this->assertApply("avc123", "abc123", new UrlReplacement(1, "b", OperationType::Swap));
82
    }
83
84
    public function testSimpleApplyReplaceAtStart()
85
    {
86
        $this->assertApply("zbc123", "abc123", new UrlReplacement(0, "a", OperationType::Swap));
87
    }
88
89
    public function testSimpleApplyReplaceAtStartBadIndex()
90
    {
91
        $this->assertBadApply("zbc123", new UrlReplacement(-1, "a", OperationType::Swap));
92
    }
93
94
    public function testSimpleApplyReplaceAtEnd()
95
    {
96
        $this->assertApply("abc124", "abc123", new UrlReplacement(5, "3", OperationType::Swap));
97
    }
98
99
    public function testSimpleApplyReplaceAtEndBadIndex()
100
    {
101
        $this->assertBadApply("abc124", new UrlReplacement(6, "3", OperationType::Swap));
102
    }
103
104
    public function testSimpleApplyInsertAtEnd()
105
    {
106
        $this->assertApply("abc12", "abc123", new UrlReplacement(5, "3", OperationType::Insert));
107
    }
108
109
    public function testSimpleApplyInsertAtStart()
110
    {
111
        $this->assertApply("bc123", "abc123", new UrlReplacement(0, "a", OperationType::Insert));
112
    }
113
114
    public function testSimpleApplyInsertAtStartBadIndex()
115
    {
116
        $this->assertBadApply("bc123", new UrlReplacement(-1, "a", OperationType::Insert));
117
    }
118
119
    public function testSimpleApplyInsertAtEndBadIndex()
120
    {
121
        $this->assertBadApply("abc12", new UrlReplacement(6, "3", OperationType::Insert));
122
    }
123
124
    private function assertBadApply($input, UrlReplacement $replacement)
125
    {
126
        $builder = new StringBuilder($input);
127
        $replacement->apply($builder, 0);
128
        $this->assertEquals($input, $builder);
129
    }
130
131
    private function assertApply($input, $expected, UrlReplacement $replacement)
132
    {
133
        $builder = new StringBuilder($input);
134
        $replacement->apply($builder, 0);
135
        $this->assertEquals($expected, $builder);
136
    }
137
138
    private function assertParseQuery()
139
    {
140
        $builder = new StringBuilder();
141
        $args = func_get_args();
142
        /** @var string $replacement */
143
        foreach ($args as $j => $replacement) {
144
            if ($builder->length() != 0) {
145
                $builder->append("-");
146
            }
147
            $builder->append($replacement);
148
        }
149
150
        $replacements = UrlReplacement::parseQueryString($builder->__toString());
151
152
        $this->assertEquals(strlen($replacement), count($replacements));
0 ignored issues
show
Bug introduced by
The variable $replacement seems to be defined by a foreach iteration on line 143. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
153
154
        for ($i = 0; $i < count($replacements); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
155
            $this->assertEquals($args[strlen($replacement) - ($i + 1)], $replacements[$i]->__toString());
156
        }
157
    }
158
159
    private function assertToAndFromString($replacementString)
160
    {
161
        $replacement1 = UrlReplacement::fromString($replacementString);
162
        $urlReplacementString = (string)$replacement1;
163
        $replacement2 = UrlReplacement::fromString($urlReplacementString);
164
        $this->assertEquals($replacement1, $replacement2);
165
        $this->assertEquals((string)$replacement2, $replacementString);
166
    }
167
}
168