Skip to content

Commit 12f147b

Browse files
committed
Merge pull request #17 from abimus/fix/flags
Fix behavior on Server::setFlag()
2 parents 5403184 + 7972b0c commit 12f147b

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="tests/bootstrap.php"
13+
>
14+
15+
<testsuites>
16+
<testsuite name="Fetch Test Suite">
17+
<directory>./tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
</phpunit>

src/Fetch/Server.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,27 @@ public function setFlag($flag, $value = null)
179179
return;
180180

181181
if (isset(self::$exclusiveFlags[$flag])) {
182-
$kill = $flag;
182+
$kill = self::$exclusiveFlags[$flag];
183183
} elseif ($index = array_search($flag, self::$exclusiveFlags)) {
184184
$kill = $index;
185185
}
186186

187-
if (isset($kill) && isset($this->flags[$kill]))
188-
unset($this->flags[$kill]);
187+
if (isset($kill) && false !== $index = array_search($kill, $this->flags))
188+
unset($this->flags[$index]);
189189

190+
$index = array_search($flag, $this->flags);
190191
if (isset($value) && $value !== true) {
191-
if ($value == false) {
192-
unset($this->flags[$flag]);
193-
} else {
194-
$this->flags[] = $flag . '=' . $value;
192+
if ($value == false && $index !== false) {
193+
unset($this->flags[$index]);
194+
} elseif ($value != false) {
195+
$match = preg_grep('/' . $flag . '/', $this->flags);
196+
if (reset($match)) {
197+
$this->flags[key($match)] = $flag . '=' . $value;
198+
} else {
199+
$this->flags[] = $flag . '=' . $value;
200+
}
195201
}
196-
} else {
202+
} elseif ($index === false) {
197203
$this->flags[] = $flag;
198204
}
199205
}

tests/Fetch/Test/ServerTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,47 @@
1111

1212
namespace Fetch\Test;
1313

14+
use Fetch\Server;
1415

1516
/**
1617
* @package Fetch
1718
* @author Robert Hafner <tedivm@tedivm.com>
1819
*/
1920
class ServerTest extends \PHPUnit_Framework_TestCase
2021
{
21-
22+
/**
23+
* @dataProvider flagsDataProvider
24+
* @param string $expected server string with %host% placeholder
25+
* @param integer $port to use (needed to test behavior on port 143 and 993 from constructor)
26+
* @param array $flags to set/unset ($flag => $value)
27+
*/
28+
public function testFlags($expected, $port, $flags)
29+
{
30+
$host = 'example.com';
31+
$server = new Server($host, $port);
32+
33+
foreach ($flags as $flag => $value) {
34+
$server->setFlag($flag, $value);
35+
}
36+
37+
$this->assertEquals(str_replace('%host%', $host, $expected), $server->getServerString());
38+
}
39+
40+
public function flagsDataProvider() {
41+
return array(
42+
array('{%host%:143/novalidate-cert}', 143, array()),
43+
array('{%host%:143/validate-cert}', 143, array('validate-cert' => true)),
44+
array('{%host%:143}', 143, array('novalidate-cert' => false)),
45+
array('{%host%:993/ssl}', 993, array()),
46+
array('{%host%:993}', 993, array('ssl' => false)),
47+
array('{%host%:100/tls}', 100, array('tls' => true)),
48+
array('{%host%:100/tls}', 100, array('tls' => true, 'tls' => true)),
49+
array('{%host%:100/notls}', 100, array('tls' => true, 'notls' => true)),
50+
array('{%host%:100}', 100, array('ssl' => true, 'ssl' => false)),
51+
array('{%host%:100/user=foo}', 100, array('user' => 'foo')),
52+
array('{%host%:100/user=foo}', 100, array('user' => 'foo', 'user' => 'foo')),
53+
array('{%host%:100/user=bar}', 100, array('user' => 'foo', 'user' => 'bar')),
54+
array('{%host%:100}', 100, array('user' => 'foo', 'user' => false)),
55+
);
56+
}
2257
}

0 commit comments

Comments
 (0)