From 5e62f870779b50d1b8624874d8ae54c6b0fd58c8 Mon Sep 17 00:00:00 2001 From: Patrik Karisch Date: Wed, 10 Jul 2013 23:52:37 +0200 Subject: [PATCH 1/2] fixed behavior on setFlag() --- src/Fetch/Server.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Fetch/Server.php b/src/Fetch/Server.php index e009168..9b08fad 100644 --- a/src/Fetch/Server.php +++ b/src/Fetch/Server.php @@ -179,21 +179,27 @@ public function setFlag($flag, $value = null) return; if (isset(self::$exclusiveFlags[$flag])) { - $kill = $flag; + $kill = self::$exclusiveFlags[$flag]; } elseif ($index = array_search($flag, self::$exclusiveFlags)) { $kill = $index; } - if (isset($kill) && isset($this->flags[$kill])) - unset($this->flags[$kill]); + if (isset($kill) && false !== $index = array_search($kill, $this->flags)) + unset($this->flags[$index]); + $index = array_search($flag, $this->flags); if (isset($value) && $value !== true) { - if ($value == false) { - unset($this->flags[$flag]); - } else { - $this->flags[] = $flag . '=' . $value; + if ($value == false && $index !== false) { + unset($this->flags[$index]); + } elseif ($value != false) { + $match = preg_grep('/' . $flag . '/', $this->flags); + if (reset($match)) { + $this->flags[key($match)] = $flag . '=' . $value; + } else { + $this->flags[] = $flag . '=' . $value; + } } - } else { + } elseif ($index === false) { $this->flags[] = $flag; } } From 7972b0c5d9ed70e202ead31745c28b32f4ac88a0 Mon Sep 17 00:00:00 2001 From: Patrik Karisch Date: Wed, 10 Jul 2013 23:52:54 +0200 Subject: [PATCH 2/2] add unit tests for Server::setFlag() add proper phpunit configuration --- phpunit.xml.dist | 20 ++++++++++++++++++ tests/Fetch/Test/ServerTest.php | 37 ++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 phpunit.xml.dist diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..9e844c9 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,20 @@ + + + + + + + ./tests + + + diff --git a/tests/Fetch/Test/ServerTest.php b/tests/Fetch/Test/ServerTest.php index 24140fe..6514cca 100644 --- a/tests/Fetch/Test/ServerTest.php +++ b/tests/Fetch/Test/ServerTest.php @@ -11,6 +11,7 @@ namespace Fetch\Test; +use Fetch\Server; /** * @package Fetch @@ -18,5 +19,39 @@ */ class ServerTest extends \PHPUnit_Framework_TestCase { - + /** + * @dataProvider flagsDataProvider + * @param string $expected server string with %host% placeholder + * @param integer $port to use (needed to test behavior on port 143 and 993 from constructor) + * @param array $flags to set/unset ($flag => $value) + */ + public function testFlags($expected, $port, $flags) + { + $host = 'example.com'; + $server = new Server($host, $port); + + foreach ($flags as $flag => $value) { + $server->setFlag($flag, $value); + } + + $this->assertEquals(str_replace('%host%', $host, $expected), $server->getServerString()); + } + + public function flagsDataProvider() { + return array( + array('{%host%:143/novalidate-cert}', 143, array()), + array('{%host%:143/validate-cert}', 143, array('validate-cert' => true)), + array('{%host%:143}', 143, array('novalidate-cert' => false)), + array('{%host%:993/ssl}', 993, array()), + array('{%host%:993}', 993, array('ssl' => false)), + array('{%host%:100/tls}', 100, array('tls' => true)), + array('{%host%:100/tls}', 100, array('tls' => true, 'tls' => true)), + array('{%host%:100/notls}', 100, array('tls' => true, 'notls' => true)), + array('{%host%:100}', 100, array('ssl' => true, 'ssl' => false)), + array('{%host%:100/user=foo}', 100, array('user' => 'foo')), + array('{%host%:100/user=foo}', 100, array('user' => 'foo', 'user' => 'foo')), + array('{%host%:100/user=bar}', 100, array('user' => 'foo', 'user' => 'bar')), + array('{%host%:100}', 100, array('user' => 'foo', 'user' => false)), + ); + } }