.gitignore
[m6w6/ext-http] / phpunit / ParamsTest.php
1 <?php
2
3 class ParamsTest extends PHPUnit_Framework_TestCase {
4 function testDefault() {
5 $s = "foo, bar;arg=0;bla, gotit=0;now";
6 $this->runAssertions(
7 new http\Params($s),
8 str_replace(" ", "", $s)
9 );
10 }
11
12 function testCustom() {
13 $s = "foo bar.arg:0.bla gotit:0.now";
14 $this->runAssertions(
15 new http\Params($s, " ", ".", ":"),
16 $s
17 );
18 }
19
20 function testQuoted() {
21 $p = new http\Params("multipart/form-data; boundary=\"--123\"");
22 $this->assertEquals(
23 array(
24 "multipart/form-data" => array(
25 "value" => true,
26 "arguments" => array(
27 "boundary" => "--123"
28 )
29 )
30 ),
31 $p->params
32 );
33 $this->assertEquals("multipart/form-data;boundary=--123", (string) $p);
34 }
35
36 function testEscaped() {
37 $p = new http\Params("form-data; name=\"upload\"; filename=\"trick\\\"\0\\\"ed\"");
38 $this->assertEquals(
39 array(
40 "form-data" => array(
41 "value" => true,
42 "arguments" => array(
43 "name" => "upload",
44 "filename" => "trick\"\0\"ed"
45 )
46 )
47 ),
48 $p->params
49 );
50 $this->assertEquals("form-data;name=upload;filename=\"trick\\\"\\0\\\"ed\"", (string) $p);
51 }
52
53 function testUrlencoded() {
54 $s = "foo=b%22r&bar=b%22z&a%5B%5D%5B%5D=1";
55 $p = new http\Params($s, "&", "", "=", http\Params::PARSE_URLENCODED);
56 $this->assertEquals(
57 array(
58 "foo" => array(
59 "value" => "b\"r",
60 "arguments" => array(),
61 ),
62 "bar" => array(
63 "value" => "b\"z",
64 "arguments" => array(),
65 ),
66 "a[][]" => array(
67 "value" => "1",
68 "arguments" => array(),
69 ),
70 ),
71 $p->params
72 );
73 $this->assertEquals("foo=b%22r&bar=b%22z&a%5B%5D%5B%5D=1", (string) $p);
74 }
75
76 function testQuery() {
77 $s = "foo=b%22r&bar=b%22z&a%5B%5D%5B%5D=1";
78 $p = new http\Params($s, "&", "", "=", http\Params::PARSE_QUERY);
79 $this->assertEquals(
80 array(
81 "foo" => array(
82 "value" => "b\"r",
83 "arguments" => array(),
84 ),
85 "bar" => array(
86 "value" => "b\"z",
87 "arguments" => array(),
88 ),
89 "a" => array(
90 "value" => array(
91 array("1")
92 ),
93 "arguments" => array(),
94 ),
95 ),
96 $p->params
97 );
98 $this->assertEquals("foo=b%22r&bar=b%22z&a%5B0%5D%5B0%5D=1", (string) $p);
99 }
100
101
102 function testEmpty() {
103 $p = new http\Params(NULL);
104 $this->assertEquals(array(), $p->params);
105 }
106
107 function testErrorOfToArrayWithArgs() {
108 $this->setExpectedException("PHPUnit_Framework_Error_Warning");
109 $p = new http\Params();
110 $p->toArray("dummy");
111 }
112
113 function testIntegerKeys() {
114 $p = new http\Params("0=nothing;1=yes");
115 $this->assertEquals(array("0" => array("value" => "nothing", "arguments" => array(1=>"yes"))), $p->params);
116 $this->assertEquals("0=nothing;1=yes", $p->toString());
117 }
118
119 function testBoolParamArguments() {
120 $p = new http\Params;
121 $container = array("value" => false, "arguments" => array("wrong" => false, "correct" => true));
122 $p["container"] = $container;
123 $this->assertEquals("container=0;wrong=0;correct", $p->toString());
124 $this->assertEquals(array("container" => $container), $p->toArray());
125 }
126
127 function testNoArgsForParam() {
128 $p = new http\Params;
129 $p["param"] = true;
130 $this->assertEquals("param", $p->toString());
131 $p["param"] = false;
132 $this->assertEquals("param=0", $p->toString());
133 }
134
135 protected function runAssertions($p, $s) {
136 $this->assertCount(3, $p->params);
137 $this->assertArrayHasKey("foo", $p->params);
138 $this->assertArrayHasKey("bar", $p->params);
139 $this->assertArrayHasKEy("gotit", $p->params);
140
141 $this->assertTrue($p["foo"]["value"]);
142 $this->assertTrue($p["bar"]["value"]);
143 $this->assertEmpty($p["gotit"]["value"]);
144
145 $this->assertEmpty($p["foo"]["arguments"]);
146 $this->assertCount(2, $p["bar"]["arguments"]);
147 $this->assertCount(1, $p["gotit"]["arguments"]);
148
149 $this->assertEmpty($p["bar"]["arguments"]["arg"]);
150 $this->assertTrue($p["bar"]["arguments"]["bla"]);
151 $this->assertTrue($p["gotit"]["arguments"]["now"]);
152
153 $this->assertEquals($s, (string) $p);
154
155 $comp = array (
156 'foo' =>
157 array (
158 'value' => true,
159 'arguments' =>
160 array (
161 ),
162 ),
163 'bar' =>
164 array (
165 'value' => true,
166 'arguments' =>
167 array (
168 'arg' => '0',
169 'bla' => true,
170 ),
171 ),
172 'gotit' =>
173 array (
174 'value' => '0',
175 'arguments' =>
176 array (
177 'now' => true,
178 ),
179 ),
180 );
181
182 $this->assertEquals($comp, $p->params);
183 $a = new http\Params($p->params);
184 $this->assertEquals($comp, $a->toArray());
185 }
186 }