UrlTest::testSetInvalidFragment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the bee4/httpclient package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Test\Transport
10
 */
11
12
namespace Bee4\Test\Transport;
13
14
use Bee4\Transport\Url;
15
16
/**
17
 * Check behaviour of Url helper
18
 * @package Bee4\Test\Transport
19
 */
20
class UrlTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * Provide a list of testable URLs
24
     * @return array
25
     */
26
    public function provideUrls()
27
    {
28
        return [
29
            ['invalid-url-given', false],
30
            ['www.jeux.com', false],
31
            ['www.jeux.com/without-scheme.html', false],
32
            ['http://www.bee4.fr'],
33
            ['ftp://www.bee4.fr/a-page.html'],
34
            ['http://www.bee4.fr:8080/a-page.html?with=query'],
35
            ['http://www.bee4.fr:1349/a-page.html?with=query#and-fragment'],
36
            ['http://user:[email protected]/a-page.html?with=query#and-fragment'],
37
            ['http://[email protected]/a-page.html?with=query#and-fragment'],
38
            ['ssh://user:[email protected]/a-page/'],
39
            ['mailto:[email protected]']
40
        ];
41
    }
42
43
    /**
44
     * @param string $url Url to be tested
45
     * @param boolean $valid True if the given URL is considered has a valid one
46
     * @dataProvider provideUrls
47
     */
48
    public function testUrl($url, $valid = true)
49
    {
50
        $this->assertEquals($valid, Url::isValid($url));
51
52
        if ($valid == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
53
            $this->setExpectedException('\InvalidArgumentException');
54
        }
55
56
        $object = new Url($url);
57
        $this->assertEquals($url, (string)$object);
58
    }
59
60
    /**
61
     * Check that it is possible to update a loaded URL
62
     */
63
    public function testUrlUpdate()
64
    {
65
        $url = new Url('http://www.bee4.fr');
66
67
        $this->assertEquals('http', $url->scheme());
68
        $this->assertEquals('www.bee4.fr', $url->host());
69
        $this->assertEquals(80, $url->port());
70
71
        $url->scheme('https');
72
        $this->assertEquals('https', $url->scheme());
73
        $this->assertEquals(443, $url->port());
74
75
        $url->path('/page.html');
76
        $url->query('nb=20');
77
        $url->fragment('anchor');
78
79
        $this->assertEquals('https://www.bee4.fr/page.html?nb=20#anchor', (string)$url);
80
    }
81
82
    /**
83
     * Check authentication detection
84
     */
85
    public function testHasAuth()
86
    {
87
        $urlWithoutAuth = new Url('http://www.bee4.fr');
88
        $this->assertFalse($urlWithoutAuth->hasAuth());
89
        $urlWithAuth = new Url('http://user:[email protected]');
90
        $this->assertTrue($urlWithAuth->hasAuth());
91
    }
92
93
    /**
94
     * Try to set host + port at the same time
95
     */
96
    public function testHostAndPort()
97
    {
98
        $url = new Url('http://www.bee4.fr:80');
99
        $this->assertEquals('http://www.bee4.fr', (string)$url);
100
        $url->host('localhost:8080');
101
        $this->assertEquals('localhost', $url->host());
102
        $this->assertEquals(8080, $url->port());
103
    }
104
105
    /**
106
     * Url must be called with a valid string has parameter
107
     * @expectedException \InvalidArgumentException
108
     */
109
    public function testBadConstruct()
110
    {
111
        new Url(0);
112
    }
113
114
    /**
115
     * Check exception when call an invalid property
116
     * @expectedException \BadMethodCallException
117
     */
118
    public function testBadProperty()
119
    {
120
        $url = new Url('http://www.bee4.fr');
121
        $url->unknown();
0 ignored issues
show
Documentation Bug introduced by
The method unknown does not exist on object<Bee4\Transport\Url>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
122
    }
123
124
    /**
125
     * Check exception when call a valid property with invalid parameters
126
     * @expectedException \BadMethodCallException
127
     */
128
    public function testBadPropertyParameters()
129
    {
130
        $url = new Url('http://www.bee4.fr');
131
        $url->scheme('param1', 'param2');
0 ignored issues
show
Unused Code introduced by
The call to Url::scheme() has too many arguments starting with 'param2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
132
    }
133
134
    /**
135
     * Check exception when try to build an invalid URL (after a bad set)
136
     * @expectedException \RuntimeException
137
     */
138
    public function testInvalidToString()
139
    {
140
        $url = new Url('http://www.bee4.fr');
141
        $url->host('');
142
        $url->toString();
143
    }
144
145
    public function testInvalidToStringCast()
146
    {
147
        $url = new Url('http://www.bee4.fr');
148
        $url->host('');
149
        $this->assertEquals(get_class($url).'::INVALID', (string)$url);
150
    }
151
152
    /**
153
     * @expectedException \InvalidArgumentException
154
     */
155
    public function testSetInvalidPort()
156
    {
157
        $url = new Url('http://www.bee4.fr');
158
        $url->port('invalid');
159
    }
160
161
    /**
162
     * @expectedException \InvalidArgumentException
163
     */
164
    public function testSetInvalidScheme()
165
    {
166
        $url = new Url('http://www.bee4.fr');
167
        $url->scheme(null);
168
    }
169
170
    /**
171
     * @expectedException \InvalidArgumentException
172
     */
173
    public function testSetInvalidHost()
174
    {
175
        $url = new Url('http://www.bee4.fr');
176
        $url->host(0);
177
    }
178
179
    /**
180
     * @expectedException \InvalidArgumentException
181
     */
182
    public function testSetInvalidUser()
183
    {
184
        $url = new Url('http://www.bee4.fr');
185
        $url->user(new \stdClass);
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
    }
187
188
    /**
189
     * @expectedException \InvalidArgumentException
190
     */
191
    public function testSetInvalidPass()
192
    {
193
        $url = new Url('http://www.bee4.fr');
194
        $url->pass(0);
195
    }
196
197
    /**
198
     * @expectedException \InvalidArgumentException
199
     */
200
    public function testSetInvalidPath()
201
    {
202
        $url = new Url('http://www.bee4.fr');
203
        $url->path(null);
204
    }
205
206
    /**
207
     * @expectedException \InvalidArgumentException
208
     */
209
    public function testSetInvalidQuery()
210
    {
211
        $url = new Url('http://www.bee4.fr');
212
        $url->query(null);
213
    }
214
215
    /**
216
     * @expectedException \InvalidArgumentException
217
     */
218
    public function testSetInvalidFragment()
219
    {
220
        $url = new Url('http://www.bee4.fr');
221
        $url->fragment(null);
222
    }
223
}
224