fb2eb4741f253dba81be89fa13e2f7627135d07e
[m6w6/ext-http] / phpunit / QueryStringTest.php
1 <?php
2
3 class QueryStringTest extends PHPUnit_Framework_TestCase {
4 protected $q;
5 protected $s = "a=b&r[]=0&r[]=1&r[]=2&rr[][]=00&rr[][]=01&1=2";
6 protected $e = "a=b&r%5B0%5D=0&r%5B1%5D=1&r%5B2%5D=2&rr%5B0%5D%5B0%5D=00&rr%5B0%5D%5B1%5D=01&1=2";
7
8 function setUp() {
9 $this->q = new http\QueryString($this->s);
10 }
11
12 function testSimple() {
13 $this->assertEquals($this->e, (string) $this->q);
14 $this->assertEquals($this->e, $this->q->get());
15 }
16
17 function testGetDefval() {
18 $this->assertEquals("nonexistant", $this->q->get("unknown", "s", "nonexistant"));
19 $this->assertEquals(null, $this->q->get("unknown"));
20 }
21
22 function testGetA() {
23 $this->assertEquals("b", $this->q->get("a"));
24 $this->assertEquals(0, $this->q->get("a", "i"));
25 $this->assertEquals(array("b"), $this->q->get("a", "a"));
26 $this->assertEquals((object)array("scalar" => "b"), $this->q->get("a", "o"));
27 }
28
29 function testGetR() {
30 $this->assertEquals(array(0,1,2), $this->q->get("r"));
31 }
32
33 function testGetRR() {
34 $this->assertEquals(array(array("00","01")), $this->q->get("rr"));
35 }
36
37 function testGet1() {
38 $this->assertEquals(2, $this->q->get(1));
39 $this->assertEquals("2", $this->q->get(1, "s"));
40 $this->assertEquals(2.0, $this->q->get(1, "f"));
41 $this->assertTrue($this->q->get(1, "b"));
42 }
43
44 function testDelA() {
45 $this->assertEquals("b", $this->q->get("a", http\QueryString::TYPE_STRING, null, true));
46 $this->assertEquals(null, $this->q->get("a"));
47 }
48
49 function testDelAll() {
50 $this->q->set(array("a" => null, "r" => null, "rr" => null, 1 => null));
51 $this->assertEquals("", $this->q->toString());
52 }
53
54 function testQSO() {
55 $this->assertEquals($this->e, (string) new http\QueryString($this->q));
56 $this->assertEquals(http_build_query(array("e"=>$this->q->toArray())), (string) new http\QueryString(array("e" => $this->q)));
57 }
58
59 function testIterator() {
60 $this->assertEquals($this->q->toArray(), iterator_to_array($this->q));
61 }
62
63 function testSerialize() {
64 $this->assertEquals($this->e, (string) unserialize(serialize($this->q)));
65 }
66 }