Skip to content

Commit 5f2bfed

Browse files
committed
Add phpunit tests for Proxy-related methods
1 parent 959d25c commit 5f2bfed

File tree

6 files changed

+254
-2
lines changed

6 files changed

+254
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*~

phpunit.xml.dist

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="tests/bootstrap.php" colors="true">
4+
<testsuites>
5+
<testsuite name="phpbrowscap Test Suite">
6+
<directory>tests/phpbrowscap/</directory>
7+
</testsuite>
8+
</testsuites>
9+
10+
<filter>
11+
<whitelist>
12+
<directory suffix=".php">src/phpbrowscap/</directory>
13+
</whitelist>
14+
</filter>
15+
</phpunit>

src/phpbrowscap/Browscap.php

-2
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ public function clearProxySettings($wrapper = null)
425425
/**
426426
* Returns the array of stream context options.
427427
*
428-
* @note Added for debug purposes. Remove at will.
429-
*
430428
* @return array
431429
*/
432430
public function getStreamContextOptions()

tests/bootstrap.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Browscap.ini parsing class with caching and update capabilities
5+
*
6+
* PHP version 5
7+
*
8+
* Copyright (c) 2006-2012 Jonathan Stoppani
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a
11+
* copy of this software and associated documentation files (the "Software"),
12+
* to deal in the Software without restriction, including without limitation
13+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
* and/or sell copies of the Software, and to permit persons to whom the
15+
* Software is furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included
18+
* in all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*
28+
* @package Browscap
29+
* @author Vítor Brandão <[email protected]>
30+
* @copyright Copyright (c) 2006-2012 Jonathan Stoppani
31+
* @version 1.0
32+
* @license http://www.opensource.org/licenses/MIT MIT License
33+
* @link https://github.com/GaretJax/phpbrowscap/
34+
*/
35+
36+
require_once __DIR__.'/phpbrowscap/TestCase.php';
37+
38+
spl_autoload_register(function($class)
39+
{
40+
$file = __DIR__.'/../src/'.strtr($class, '\\', '/').'.php';
41+
if (file_exists($file)) {
42+
require $file;
43+
return true;
44+
}
45+
});

tests/phpbrowscap/BrowscapTest.php

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace phpbrowscap;
4+
5+
use phpbrowscap\Browscap;
6+
7+
/**
8+
* Browscap.ini parsing class with caching and update capabilities
9+
*
10+
* PHP version 5
11+
*
12+
* Copyright (c) 2006-2012 Jonathan Stoppani
13+
*
14+
* Permission is hereby granted, free of charge, to any person obtaining a
15+
* copy of this software and associated documentation files (the "Software"),
16+
* to deal in the Software without restriction, including without limitation
17+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
18+
* and/or sell copies of the Software, and to permit persons to whom the
19+
* Software is furnished to do so, subject to the following conditions:
20+
*
21+
* The above copyright notice and this permission notice shall be included
22+
* in all copies or substantial portions of the Software.
23+
*
24+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
* THE SOFTWARE.
31+
*
32+
* @package Browscap
33+
* @author Vítor Brandão <[email protected]>
34+
* @copyright Copyright (c) 2006-2012 Jonathan Stoppani
35+
* @version 1.0
36+
* @license http://www.opensource.org/licenses/MIT MIT License
37+
* @link https://github.com/GaretJax/phpbrowscap/
38+
*/
39+
class BrowscapTest extends TestCase
40+
{
41+
public function testProxyAutoDetection()
42+
{
43+
$browscap = $this->createBrowscap();
44+
45+
putenv('http_proxy=http://proxy.example.com:3128');
46+
putenv('https_proxy=http://proxy.example.com:3128');
47+
putenv('ftp_proxy=http://proxy.example.com:3128');
48+
49+
$browscap->autodetectProxySettings();
50+
$options = $browscap->getStreamContextOptions();
51+
52+
$this->assertEquals($options['http']['proxy'], 'tcp://proxy.example.com:3128');
53+
$this->assertTrue($options['http']['request_fulluri']);
54+
55+
$this->assertEquals($options['https']['proxy'], 'tcp://proxy.example.com:3128');
56+
$this->assertTrue($options['https']['request_fulluri']);
57+
58+
$this->assertEquals($options['ftp']['proxy'], 'tcp://proxy.example.com:3128');
59+
$this->assertTrue($options['ftp']['request_fulluri']);
60+
}
61+
62+
public function testAddProxySettings()
63+
{
64+
$browscap = $this->createBrowscap();
65+
66+
$browscap->addProxySettings('proxy.example.com', 3128, 'http');
67+
$options = $browscap->getStreamContextOptions();
68+
69+
$this->assertEquals($options['http']['proxy'], 'tcp://proxy.example.com:3128');
70+
$this->assertTrue($options['http']['request_fulluri']);
71+
}
72+
73+
public function testClearProxySettings()
74+
{
75+
$browscap = $this->createBrowscap();
76+
77+
$browscap->addProxySettings('proxy.example.com', 3128, 'http');
78+
$options = $browscap->getStreamContextOptions();
79+
80+
$this->assertEquals($options['http']['proxy'], 'tcp://proxy.example.com:3128');
81+
$this->assertTrue($options['http']['request_fulluri']);
82+
83+
$clearedWrappers = $browscap->clearProxySettings();
84+
$options = $browscap->getStreamContextOptions();
85+
86+
$this->assertEmpty($options);
87+
$this->assertEquals($clearedWrappers, array('http'));
88+
}
89+
90+
public function testGetStreamContext()
91+
{
92+
$cacheDir = $this->createCacheDir();
93+
$browscap = new BrowscapForTest($cacheDir);
94+
95+
$browscap->addProxySettings('proxy.example.com', 3128, 'http');
96+
97+
$resource = $browscap->getStreamContext();
98+
99+
$this->assertTrue(is_resource($resource));
100+
}
101+
}
102+
103+
class BrowscapForTest extends Browscap
104+
{
105+
public function getStreamContext($recreate = false)
106+
{
107+
return $this->_getStreamContext($recreate);
108+
}
109+
}

tests/phpbrowscap/TestCase.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace phpbrowscap;
4+
5+
use phpbrowscap\Browscap;
6+
7+
/**
8+
* Browscap.ini parsing class with caching and update capabilities
9+
*
10+
* PHP version 5
11+
*
12+
* Copyright (c) 2006-2012 Jonathan Stoppani
13+
*
14+
* Permission is hereby granted, free of charge, to any person obtaining a
15+
* copy of this software and associated documentation files (the "Software"),
16+
* to deal in the Software without restriction, including without limitation
17+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
18+
* and/or sell copies of the Software, and to permit persons to whom the
19+
* Software is furnished to do so, subject to the following conditions:
20+
*
21+
* The above copyright notice and this permission notice shall be included
22+
* in all copies or substantial portions of the Software.
23+
*
24+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
* THE SOFTWARE.
31+
*
32+
* @package Browscap
33+
* @author Vítor Brandão <[email protected]>
34+
* @copyright Copyright (c) 2006-2012 Jonathan Stoppani
35+
* @version 1.0
36+
* @license http://www.opensource.org/licenses/MIT MIT License
37+
* @link https://github.com/GaretJax/phpbrowscap/
38+
*/
39+
class TestCase extends \PHPUnit_Framework_TestCase
40+
{
41+
protected $cacheDir;
42+
43+
public function setUp()
44+
{
45+
}
46+
47+
protected function createCacheDir($cache_dir = null)
48+
{
49+
$cacheDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'browscap_testing';
50+
51+
if (!is_dir($cacheDir)) {
52+
if (false === @mkdir($cacheDir, 0777, true)) {
53+
throw new \RuntimeException(sprintf('Unable to create the "%s" directory', $cacheDir));
54+
}
55+
}
56+
57+
$this->cacheDir = $cacheDir;
58+
59+
return $this->cacheDir;
60+
}
61+
62+
protected function createBrowscap()
63+
{
64+
$cacheDir = $this->createCacheDir();
65+
66+
return new Browscap($cacheDir);
67+
}
68+
69+
protected function removeCacheDir()
70+
{
71+
if (isset($this->cacheDir) && is_dir($this->cacheDir)) {
72+
if (false === @rmdir($this->cacheDir)) {
73+
throw new \RuntimeException(sprintf('Unable to remove the "%s" directory', $this->cacheDir));
74+
}
75+
76+
$this->cacheDir = null;
77+
}
78+
}
79+
80+
public function tearDown()
81+
{
82+
$this->removeCacheDir();
83+
}
84+
}

0 commit comments

Comments
 (0)