Completed
Push — develop ( 50dbcf...bff7e0 )
by Ben
13s
created

UriBuilderTest::testSetParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
use GroupByInc\API\Util\UriBuilder;
4
5
class UriBuilderTest 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...
6
{
7
    /** @var UriBuilder */
8
    private $builder;
9
10
    public function setUp()
11
    {
12
        $this->builder = new UriBuilder();
13
        $this->builder->setScheme("http")
14
            ->setUser("guest")
15
            ->setPassword("password")
16
            ->setHost("example.com")
17
            ->setPort(8080)
18
            ->setPath("some/wild/path")
19
            ->setParameter("sale", "true")
20
            ->setParameter("columns", "4")
21
            ->setFragment("bookmark");
22
    }
23
24
    public function testSetPath()
25
    {
26
        $newPath = "a/completely/different/path";
27
        $this->builder->setPath($newPath);
28
        $this->assertEquals($newPath, $this->builder->getPath());
29
    }
30
31
    public function testAppendToPath()
32
    {
33
        $this->builder->appendToPath("/to")
34
            ->appendToPath("/our")
35
            ->appendToPath("/page");
36
        $this->assertEquals("some/wild/path/to/our/page", $this->builder->getPath());
37
    }
38
39
    public function testSetParameter()
40
    {
41
        $this->builder->setParameter("key", "value");
42
        $this->assertEquals("sale=true&columns=4&key=value", $this->builder->getQuery());
43
    }
44
45
    public function testSetParameters()
46
    {
47
        $this->builder->setParameters(array(
48
            "key" => "value",
49
            "height" => "20in",
50
            "colour" => "blue"
51
        ));
52
        $this->assertEquals("key=value&height=20in&colour=blue", $this->builder->getQuery());
53
    }
54
55
    public function testSetUser()
56
    {
57
        $this->builder->setPassword(null);
58
        $this->assertEquals("guest", $this->builder->getUserInfo());
59
    }
60
61
    public function testSetUserAndPassword()
62
    {
63
        $this->assertEquals("guest:password", $this->builder->getUserInfo());
64
    }
65
66
    public function testPasswordNoUser()
67
    {
68
        $this->builder->setUser(null);
69
        $this->assertEquals("", $this->builder->getUserInfo());
70
    }
71
72
    public function testHostAndPort()
73
    {
74
        $this->builder->setScheme(null)
75
            ->setUser(null)
76
            ->setPassword(null)
77
            ->setPath(null)
78
            ->setParameters(array())
79
            ->setFragment(null);
80
        $this->assertEquals("example.com:8080", $this->builder->build());
81
    }
82
83 View Code Duplication
    public function testSchemeHostAndPort()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $this->builder->setUser(null)
86
            ->setPassword(null)
87
            ->setPath(null)
88
            ->setParameters(array())
89
            ->setFragment(null);
90
        $this->assertEquals("http://example.com:8080", $this->builder->build());
91
    }
92
93 View Code Duplication
    public function testSchemeHostPortAndPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $this->builder->setUser(null)
96
            ->setPassword(null)
97
            ->setParameters(array())
98
            ->setFragment(null);
99
        $this->assertEquals("http://example.com:8080/some/wild/path", $this->builder->build());
100
    }
101
102 View Code Duplication
    public function testSchemeHostPortPathAndQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $this->builder->setUser(null)
105
            ->setPassword(null)
106
            ->setFragment(null);
107
        $this->assertEquals("http://example.com:8080/some/wild/path?sale=true&columns=4", $this->builder->build());
108
    }
109
110
    public function testSchemeHostPortPathQueryAndFragment()
111
    {
112
        $this->builder->setUser(null)
113
            ->setPassword(null);
114
        $this->assertEquals("http://example.com:8080/some/wild/path?sale=true&columns=4#bookmark",
115
            $this->builder->build());
116
    }
117
118
    public function testSchemeUserHostPortPathQueryAndFragment()
119
    {
120
        $this->builder->setPassword(null);
121
        $this->assertEquals("http://[email protected]:8080/some/wild/path?sale=true&columns=4#bookmark",
122
            $this->builder->build());
123
    }
124
125
    public function testFullUri()
126
    {
127
        $this->assertEquals("http://guest:[email protected]:8080/some/wild/path?sale=true&columns=4#bookmark",
128
            $this->builder->build());
129
    }
130
131
    public function testSchemeFromString()
132
    {
133
        $builder = new UriBuilder();
134
        $builder->setFromString("http://therest");
135
        $this->assertEquals("http", $builder->getScheme());
136
    }
137
138
    public function testUserFromString()
139
    {
140
        $builder = new UriBuilder();
141
        $builder->setFromString("guest@therest");
142
        $this->assertEquals("guest", $builder->getUser());
143
    }
144
145
    public function testUserAndPasswordFromString()
146
    {
147
        $builder = new UriBuilder();
148
        $builder->setFromString("guest:pass@therest");
149
        $this->assertEquals("guest", $builder->getUser());
150
        $this->assertEquals("pass", $builder->getPassword());
151
    }
152
153
    public function testHostFromString()
154
    {
155
        $builder = new UriBuilder();
156
        $builder->setFromString("localhost");
157
        $this->assertEquals("localhost", $builder->getHost());
158
    }
159
160
    public function testHostAndPortFromString()
161
    {
162
        $builder = new UriBuilder();
163
        $builder->setFromString("hans:gr00ber@localhost:4040");
164
        $this->assertEquals("localhost", $builder->getHost());
165
        $this->assertEquals(4040, $builder->getPort());
166
    }
167
168
    public function testPathFromString()
169
    {
170
        $builder = new UriBuilder();
171
        $builder->setFromString("localhost/path/to/somewhere");
172
        $this->assertEquals("/path/to/somewhere", $builder->getPath());
173
    }
174
175
    public function testQueryParamsFromString()
176
    {
177
        $builder = new UriBuilder();
178
        $builder->setFromString("localhost?key=val&up=down&right=left");
179
        $this->assertEquals(array(
180
            "key" => "val",
181
            "up" => "down",
182
            "right" => "left"
183
        ), $builder->getParameters());
184
    }
185
186
    public function testFragmentFromString()
187
    {
188
        $builder = new UriBuilder();
189
        $builder->setFromString("localhost/path#bookmark");
190
        $this->assertEquals("bookmark", $builder->getFragment());
191
    }
192
193
    public function testFullUriFromString()
194
    {
195
        $builder = new UriBuilder();
196
        $builder->setFromString("http://someone:somehow@somewhere:999/with/a/path?that=leads&no=where#tagged");
197
        $this->assertEquals("http", $builder->getScheme());
198
        $this->assertEquals("someone", $builder->getUser());
199
        $this->assertEquals("somehow", $builder->getPassword());
200
        $this->assertEquals("somewhere", $builder->getHost());
201
        $this->assertEquals(999, $builder->getPort());
202
        $this->assertEquals("/with/a/path", $builder->getPath());
203
        $this->assertEquals(array(
204
            "that" => "leads",
205
            "no" => "where"
206
        ), $builder->getParameters());
207
        $this->assertEquals("tagged", $builder->getFragment());
208
    }
209
}
210