From 179b9e4d1c888871a3393a6045dfb675a14b6675 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 25 Jan 2012 14:41:48 +0000 Subject: [PATCH] querystring test & fix --- php_http_querystring.c | 15 ++++----- phpunit/QueryStringTest.php | 66 +++++++++++++++++++++++++++++++++++++ tests/phpunit.phpt | 2 +- 3 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 phpunit/QueryStringTest.php diff --git a/php_http_querystring.c b/php_http_querystring.c index ead598b..7c569de 100644 --- a/php_http_querystring.c +++ b/php_http_querystring.c @@ -145,23 +145,22 @@ PHP_HTTP_API STATUS php_http_querystring_update(zval *qarray, zval *params, zval } } } else { + zval *entry; /* * add */ if (Z_TYPE_PP(params_entry) == IS_OBJECT) { - zval *new_array; - - MAKE_STD_ZVAL(new_array); - array_init(new_array); - php_http_querystring_update(new_array, *params_entry, NULL TSRMLS_CC); - *params_entry = new_array; + MAKE_STD_ZVAL(entry); + array_init(entry); + php_http_querystring_update(entry, *params_entry, NULL TSRMLS_CC); } else { Z_ADDREF_PP(params_entry); + entry = *params_entry; } if (key.type == HASH_KEY_IS_STRING) { - add_assoc_zval_ex(qarray, key.str, key.len, *params_entry); + add_assoc_zval_ex(qarray, key.str, key.len, entry); } else { - add_index_zval(qarray, key.num, *params_entry); + add_index_zval(qarray, key.num, entry); } } } diff --git a/phpunit/QueryStringTest.php b/phpunit/QueryStringTest.php new file mode 100644 index 0000000..2fde23a --- /dev/null +++ b/phpunit/QueryStringTest.php @@ -0,0 +1,66 @@ +q = new http\QueryString($this->s); + } + + function testSimple() { + $this->assertEquals($this->e, (string) $this->q); + $this->assertEquals($this->e, $this->q->get()); + } + + function testGetDefval() { + $this->assertEquals("nonexistant", $this->q->get("unknown", "s", "nonexistant")); + $this->assertEquals(null, $this->q->get("unknown")); + } + + function testGetA() { + $this->assertEquals("b", $this->q->get("a")); + $this->assertEquals(0, $this->q->get("a", "i")); + $this->assertEquals(array("b"), $this->q->get("a", "a")); + $this->assertEquals((object)array("scalar" => "b"), $this->q->get("a", "o")); + } + + function testGetR() { + $this->assertEquals(array(0,1,2), $this->q->get("r")); + } + + function testGetRR() { + $this->assertEquals(array(array("00"),array("10")), $this->q->get("rr")); + } + + function testGet1() { + $this->assertEquals(2, $this->q->get(1)); + $this->assertEquals("2", $this->q->get(1, "s")); + $this->assertEquals(2.0, $this->q->get(1, "f")); + $this->assertTrue($this->q->get(1, "b")); + } + + function testDelA() { + $this->assertEquals("b", $this->q->get("a", http\QueryString::TYPE_STRING, null, true)); + $this->assertEquals(null, $this->q->get("a")); + } + + function testDelAll() { + $this->q->set(array("a" => null, "r" => null, "rr" => null, 1 => null)); + $this->assertEquals("", $this->q->toString()); + } + + function testQSO() { + $this->assertEquals($this->e, (string) new http\QueryString($this->q)); + $this->assertEquals(http_build_query(array("e"=>$this->q->toArray())), (string) new http\QueryString(array("e" => $this->q))); + } + + function testIterator() { + $this->assertEquals($this->q->toArray(), iterator_to_array($this->q)); + } + + function testSerialize() { + $this->assertEquals($this->e, (string) unserialize(serialize($this->q))); + } +} diff --git a/tests/phpunit.phpt b/tests/phpunit.phpt index 4289ffd..6320f61 100644 --- a/tests/phpunit.phpt +++ b/tests/phpunit.phpt @@ -12,7 +12,7 @@ require_once "PHPUnit/Autoload.php"; --EXPECTF-- PHPUnit %s by Sebastian Bergmann. -%s +%a Time: %s, Memory: %s -- 2.30.2