Completed
Push — master ( 492298...f4aaa5 )
by Fwolf
04:31
created

HttpUtilTest::testGetSelfHostUrl()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace FwlibTest\Util\Common;
4
5
use Fwlib\Util\Common\Env;
6
use Fwlib\Util\Common\HttpUtil;
7
use Fwlib\Util\UtilContainer;
8
use FwlibTest\Aide\FunctionMockAwareTrait;
9
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
10
use org\bovigo\vfs\vfsStream;
11
use org\bovigo\vfs\vfsStreamDirectory;
12
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13
14
/**
15
 * @SuppressWarnings(PHPMD.TooManyMethods)
16
 *
17
 * @copyright   Copyright 2004-2018 Fwolf
18
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
19
 */
20
class HttpUtilTest extends PHPUnitTestCase
21
{
22
    use FunctionMockAwareTrait;
23
24
25
    /**
26
     * @var Env
27
     */
28
    protected static $envUtilBackup = null;
29
30
    /**
31
     * @var vfsStreamDirectory
32
     */
33
    protected static $vfsRoot = null;
34
35
36
    /**
37
     * @return HttpUtil
38
     */
39
    protected function buildMock()
40
    {
41
        return UtilContainer::getInstance()->getHttp();
42
    }
43
44
45
    public static function setUpBeforeClass()
46
    {
47
        self::$vfsRoot = vfsStream::setup('HttpUtilTest');
48
49
        $utilContainer = UtilContainer::getInstance();
50
        self::$envUtilBackup = $utilContainer->getEnv();
51
    }
52
53
54
    public static function tearDownAfterClass()
55
    {
56
        $utilContainer = UtilContainer::getInstance();
57
        $utilContainer->register('Env', self::$envUtilBackup);
58
    }
59
60
61
    public function testDownload()
62
    {
63
        $httpUtil = $this->buildMock();
64
65
        $factory = $this->getFunctionMockFactory()
66
            ->setNamespace(HttpUtil::class);
67
        $headerMock = $factory->get(null, 'header', true);
68
69
        $expected = 'Test for download()';
70
        $this->expectOutputString($expected);
71
        $httpUtil->download($expected);
72
73
        $headerMock->disable();
74
    }
75
76
77
    public function testDownloadFileAndCheckHeader()
78
    {
79
        /** @var MockObject|HttpUtil $httpUtil */
80
        $httpUtil = $this->getMockBuilder(HttpUtil::class)
81
            ->setMethods(['getBrowserType'])
82
            ->getMock();
83
        $httpUtil->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Util\Common\HttpUtil.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
            ->method('getBrowserType')
85
            ->will($this->returnValue('trident'));
86
87
        $factory = $this->getFunctionMockFactory()
88
            ->setNamespace(HttpUtil::class);
89
        $headerMock = $factory->get(null, 'header', true);
90
91
        $file = vfsStream::newFile('toDownload.txt')->at(self::$vfsRoot);
92
93
94
        // Not assign download file name
95
        $headerMock->setResult([]);
96
        $httpUtil->downloadFile($file->url());
0 ignored issues
show
Bug introduced by
The method downloadFile does only exist in Fwlib\Util\Common\HttpUtil, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
97
        $headers = $headerMock->getResult();
98
        $headerString = implode(', ', $headers);
99
        $this->assertRegExp("/filename=\"toDownload\\.txt\"/", $headerString);
100
101
        // Assign download file name
102
        // Filename is fixed for IE
103
        $headerMock->setResult([]);
104
        $httpUtil->downloadFile($file->url(), 'foo.bar.txt');
105
        $headers = $headerMock->getResult();
106
        $headerString = implode(', ', $headers);
107
        /** @noinspection SpellCheckingInspection */
108
        $this->assertRegExp("/filename=\"foo%2ebar\\.txt\"/", $headerString);
109
110
111
        $headerMock->disableAll();
112
    }
113
114
115
    public function testDownloadFileWithInvalidPath()
116
    {
117
        $httpUtil = $this->buildMock();
118
119
        $this->assertFalse(
120
            $httpUtil->downloadFile(__DIR__ . '/not-exist-file')
121
        );
122
    }
123
124
125
    public function testGetBrowserType()
126
    {
127
        $httpUtil = $this->buildMock();
128
129
        $envUtil = $this->getMockBuilder(Env::class)
130
            ->setMethods(['getServer'])
131
            ->getMock();
132
        $envUtil->expects($this->any())
133
            ->method('getServer')
134
            ->willReturnOnConsecutiveCalls('', 'foo bar');
135
        UtilContainer::getInstance()->register('Env', $envUtil);
136
137
138
        $this->assertEquals('', $httpUtil->getBrowserType(null));
139
        $this->assertEquals('', $httpUtil->getBrowserType('foo bar'));
140
141
        // Not use consecutive return value anymore
142
        $this->assertEquals('', $httpUtil->getBrowserType(''));
143
144
        // Safari 6.0
145
        $browserType = $httpUtil->getBrowserType(
146
            'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko)' .
147
            ' Version/6.0 Mobile/10A5355d Safari/8536.25'
148
        );
149
        $this->assertEquals('webkit', $browserType);
150
151
        // IE 10.6
152
        $browserType = $httpUtil->getBrowserType(
153
            'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1;' .
154
            ' .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0'
155
        );
156
        $this->assertEquals('trident', $browserType);
157
158
        // Chrome 30.0.1599.17
159
        $browserType = $httpUtil->getBrowserType(
160
            'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)' .
161
            ' Chrome/30.0.1599.17 Safari/537.36'
162
        );
163
        $this->assertEquals('webkit', $browserType);
164
    }
165
166
167
    /**
168
     * @see http://ideone.com/mO4Fbm    Will only return if key exists
169
     */
170
    public function testGetClientIp()
171
    {
172
        $httpUtil = $this->buildMock();
173
174
        $factory = $this->getFunctionMockFactory()
175
            ->setNamespace(HttpUtil::class);
176
        $filterInputArrayMock = $factory->get('', 'filter_input_array', true);
177
178
179
        $filterInputArrayMock->setResult([
180
            'REMOTE_ADDR' => '127.0.0.1',
181
        ]);
182
        $ipStr = $httpUtil->getClientIp();
183
        $this->assertEquals('127.0.0.1', $ipStr);
184
185
        $filterInputArrayMock->setResult([]);
186
        $ipStr = $httpUtil->getClientIp();
187
        $this->assertEquals('', $ipStr);
188
189
190
        $filterInputArrayMock->disableAll();
191
    }
192
193
194
    public function testGetInput()
195
    {
196
        $httpUtil = $this->buildMock();
197
198
        $factory = $this->getFunctionMockFactory()
199
            ->setNamespace(HttpUtil::class);
200
        $filterInputMock = $factory->get(null, 'filter_input', true);
201
202
203
        $filterInputMock->setResult(null);
204
        $this->assertNull($httpUtil->getCookie('dummy', null));
205
        $this->assertNull($httpUtil->getGet('dummy', null));
206
        $this->assertNull($httpUtil->getPost('dummy', null));
207
208
209
        $filterInputMock->disableAll();
210
    }
211
212
213
    public function testGetInputs()
214
    {
215
        $httpUtil = $this->buildMock();
216
217
        $factory = $this->getFunctionMockFactory()
218
            ->setNamespace(HttpUtil::class);
219
        $filterInputArrayMock =
220
            $factory->get(null, 'filter_input_array', true);
221
222
223
        $filterInputArrayMock->setResult([]);
224
        $this->assertEmpty($httpUtil->getCookies());
225
        $this->assertEmpty($httpUtil->getGets());
226
        $this->assertEmpty($httpUtil->getPosts());
227
228
229
        $filterInputArrayMock->disableAll();
230
    }
231
232
233
    public function testGetSelfHostUrl()
234
    {
235
        $envUtil = $this->getMockBuilder(Env::class)
236
            ->setMethods(['getServer'])
237
            ->getMock();
238
        $envUtil->expects($this->any())
239
            ->method('getServer')
240
            ->willReturnOnConsecutiveCalls('', 'domain.tld');
241
        UtilContainer::getInstance()->register('Env', $envUtil);
242
243
        /** @var MockObject|HttpUtil $httpUtil */
244
        $httpUtil = $this->getMockBuilder(HttpUtil::class)
245
            ->setMethods(['isHttps'])
246
            ->getMock();
247
        $httpUtil->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Util\Common\HttpUtil.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
248
            ->method('isHttps')
249
            ->willReturn(true);
250
251
252
        // No host
253
        $this->assertEquals('', $httpUtil->getSelfHostUrl());
0 ignored issues
show
Bug introduced by
The method getSelfHostUrl does only exist in Fwlib\Util\Common\HttpUtil, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
254
255
        $this->assertEquals(
256
            'https://domain.tld',
257
            $httpUtil->getSelfHostUrl()
258
        );
259
    }
260
261
262
    public function testGetSelfUrl()
263
    {
264
        $selfHostUrl = 'http://domain.tld';
265
        $requestUri = '/foo.php?p=42';
266
        $urlWithoutQuery = 'http://domain.tld/bar.php';
267
268
        $envUtil = $this->getMockBuilder(Env::class)
269
            ->setMethods(['getServer'])
270
            ->getMock();
271
        $envUtil->expects($this->any())
272
            ->method('getServer')
273
            ->willReturn($requestUri);
274
        UtilContainer::getInstance()->register('Env', $envUtil);
275
276
        /** @var MockObject|HttpUtil $httpUtil */
277
        $httpUtil = $this->getMockBuilder(HttpUtil::class)
278
            ->setMethods(
279
                ['getSelfHostUrl', 'getSelfUrlWithoutQueryString']
280
            )
281
            ->getMock();
282
        $httpUtil->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Util\Common\HttpUtil.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
283
            ->method('getSelfHostUrl')
284
            ->willReturn($selfHostUrl);
285
        $httpUtil->expects($this->any())
286
            ->method('getSelfUrlWithoutQueryString')
287
            ->willReturn($urlWithoutQuery);
288
289
290
        $this->assertEquals(
291
            $selfHostUrl . $requestUri,
292
            $httpUtil->getSelfUrl(true)
0 ignored issues
show
Bug introduced by
The method getSelfUrl does only exist in Fwlib\Util\Common\HttpUtil, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
293
        );
294
        $this->assertEquals(
295
            $urlWithoutQuery,
296
            $httpUtil->getSelfUrl(false)
297
        );
298
    }
299
300
301
    public function testGetSelfUrlWithoutQueryString()
302
    {
303
        $selfHostUrl = 'http://domain.tld';
304
        $scriptName = '/foo.php';
305
306
        $envUtil = $this->getMockBuilder(Env::class)
307
            ->setMethods(['getServer'])
308
            ->getMock();
309
        $envUtil->expects($this->any())
310
            ->method('getServer')
311
            ->willReturn($scriptName);
312
        UtilContainer::getInstance()->register('Env', $envUtil);
313
314
        /** @var MockObject|HttpUtil $httpUtil */
315
        $httpUtil = $this->getMockBuilder(HttpUtil::class)
316
            ->setMethods(['getSelfHostUrl'])
317
            ->getMock();
318
        $httpUtil->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Util\Common\HttpUtil.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
319
            ->method('getSelfHostUrl')
320
            ->willReturnOnConsecutiveCalls('', $selfHostUrl);
321
322
323
        $this->assertEquals(
324
            '',
325
            $httpUtil->getSelfUrlWithoutQueryString()
0 ignored issues
show
Bug introduced by
The method getSelfUrlWithoutQueryString does only exist in Fwlib\Util\Common\HttpUtil, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
326
        );
327
        $this->assertEquals(
328
            $selfHostUrl . $scriptName,
329
            $httpUtil->getSelfUrlWithoutQueryString()
330
        );
331
    }
332
333
334
    public function testGetUrlParam()
335
    {
336
        $selfUrl = 'http://domain.tld/foo.php';
337
338
        /** @var MockObject|HttpUtil $httpUtil */
339
        $httpUtil = $this->getMockBuilder(HttpUtil::class)
340
            ->setMethods(['getGets', 'getSelfUrlWithoutQueryString'])
341
            ->getMock();
342
        $httpUtil->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Util\Common\HttpUtil.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
343
            ->method('getGets')
344
            ->willReturnOnConsecutiveCalls(
345
                ['a' => 1],
346
                ['a' => 1],
347
                ['a' => 1, 'b' => '', 'c' => 3],
348
                ['a' => 1, 'b' => '', 'c' => 3]
349
            );
350
        $httpUtil->expects($this->once())
351
            ->method('getSelfUrlWithoutQueryString')
352
            ->willReturn($selfUrl);
353
354
355
        /** @noinspection PhpDeprecationInspection */
356
        {
357
            $qStr = $httpUtil->getUrlParam();
0 ignored issues
show
Bug introduced by
The method getUrlParam does only exist in Fwlib\Util\Common\HttpUtil, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
358
            $this->assertEquals('?a=1', $qStr);
359
360
            $qStr = $httpUtil->getUrlParam('b', 2);
361
            $this->assertEquals('?a=1&b=2', $qStr);
362
363
            $qStr = $httpUtil->getUrlParam(['a' => 2, 1 => 'a'], ['b', 'c']);
364
            $this->assertEquals('?a=2&1=a', $qStr);
365
366
            $qStr = $httpUtil->getUrlParam(['a' => 2, 1 => 'a'], 'b', true);
367
            $this->assertEquals($selfUrl . '?a=2&c=3&1=a', $qStr);
368
        }
369
    }
370
371
372
    public function testGetUrlPlan()
373
    {
374
        /** @var MockObject|HttpUtil $httpUtil */
375
        $httpUtil = $this->getMockBuilder(HttpUtil::class)
376
            ->setMethods(['getSelfUrl'])
377
            ->getMock();
378
        $httpUtil->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Util\Common\HttpUtil.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
379
            ->method('getSelfUrl')
380
            ->willReturn('https://domain.tld/foo.php?bar=42');
381
382
383
        $url = 'http://domain.tld/?a=https://something';
384
        $this->assertEquals('http', $httpUtil->getUrlPlan($url));
0 ignored issues
show
Bug introduced by
The method getUrlPlan does only exist in Fwlib\Util\Common\HttpUtil, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
385
386
        $url = 'HTTP://domain.tld/';
387
        $this->assertEquals('http', $httpUtil->getUrlPlan($url));
388
389
        $url = 'ftp://domain.tld/';
390
        $this->assertEquals('ftp', $httpUtil->getUrlPlan($url));
391
392
        $url = 'not url';
393
        $this->assertEquals('', $httpUtil->getUrlPlan($url));
394
395
        $url = '';
396
        $this->assertRegExp('/(https?)?/i', $httpUtil->getUrlPlan($url));
397
    }
398
399
400
    public function testIsHttps()
401
    {
402
        $envUtil = $this->getMockBuilder(Env::class)
403
            ->setMethods(['getServer'])
404
            ->getMock();
405
        $envUtil->expects($this->any())
406
            ->method('getServer')
407
            ->willReturnOnConsecutiveCalls(null, 'off', 'on');
408
409
        $utilContainer = UtilContainer::getInstance();
410
        $utilContainer->register('Env', $envUtil);
411
412
        $httpUtil = $this->buildMock();
413
414
        $this->assertFalse($httpUtil->isHttps());
415
        $this->assertFalse($httpUtil->isHttps());
416
        $this->assertTrue($httpUtil->isHttps());
417
    }
418
419
420
    public function testPickGetsAndPickPosts()
421
    {
422
        $httpUtil = $this->buildMock();
423
424
        $factory = $this->getFunctionMockFactory()
425
            ->setNamespace(HttpUtil::class);
426
        $filterInputArrayMock = $factory->get('', 'filter_input_array', true);
427
428
        $dummy = [
429
            'a' => '0',
430
            'b' => '1',
431
        ];
432
        $filterInputArrayMock->setResult($dummy);
433
434
435
        $params = $httpUtil->pickGets(['a', 'b'], true);
436
        $this->assertEqualArray(['b' => '1'], $params);
437
438
439
        $callback = function ($value) {
440
            return 10 * $value;
441
        };
442
        $params = $httpUtil->pickPosts(['a', 'b'], false, $callback);
443
        $this->assertEqualArray(['a' => 0, 'b' => 10], $params);
444
445
446
        $filterInputArrayMock->disableAll();
447
    }
448
449
450
    /**
451
     * Please notice that normal cookie set will only be available till next
452
     * page load, here in test we are using mocked cookie method.
453
     */
454
    public function testSetUnsetCookie()
455
    {
456
        $httpUtil = $this->buildMock();
457
458
        $factory = $this->getFunctionMockFactory(HttpUtil::class);
459
        $setcookieMock = $factory->get(null, 'setcookie', true);
460
461
462
        $httpUtil->setCookie('foo', 'bar', time() + 10);
463
        $this->assertEquals('bar', $setcookieMock->getResult()['foo']);
464
465
        $httpUtil->setCookie('foo', 'bar', time() - 10, '/path/');
466
        $this->assertArrayNotHasKey('foo', $setcookieMock->getResult());
467
468
469
        // For unset
470
        $httpUtil
471
            ->setCookie('foo', 'bar', time() + 10, '/path', 'domain.tld');
472
        $this->assertEquals('bar', $setcookieMock->getResult()['foo']);
473
474
        $httpUtil->unsetCookie('foo');
475
        $this->assertArrayNotHasKey('foo', $setcookieMock->getResult());
476
477
478
        $setcookieMock->disable();
479
    }
480
}
481