add simple (de)quote and (de)escape capabilities to http\Params
[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 testEmpty() {
54 $p = new http\Params(NULL);
55 $this->assertEquals(array(), $p->params);
56 }
57
58 function testErrorOfToArrayWithArgs() {
59 $this->setExpectedException("PHPUnit_Framework_Error_Warning");
60 $p = new http\Params();
61 $p->toArray("dummy");
62 }
63
64 function testIntegerKeys() {
65 $p = new http\Params("0=nothing;1=yes");
66 $this->assertEquals(array("0" => array("value" => "nothing", "arguments" => array(1=>"yes"))), $p->params);
67 $this->assertEquals("0=nothing;1=yes", $p->toString());
68 }
69
70 function testBoolParamArguments() {
71 $p = new http\Params;
72 $container = array("value" => false, "arguments" => array("wrong" => false, "correct" => true));
73 $p["container"] = $container;
74 $this->assertEquals("container=0;wrong=0;correct", $p->toString());
75 $this->assertEquals(array("container" => $container), $p->toArray());
76 }
77
78 function testNoArgsForParam() {
79 $p = new http\Params;
80 $p["param"] = true;
81 $this->assertEquals("param", $p->toString());
82 $p["param"] = false;
83 $this->assertEquals("param=0", $p->toString());
84 }
85
86 protected function runAssertions($p, $s) {
87 $this->assertCount(3, $p->params);
88 $this->assertArrayHasKey("foo", $p->params);
89 $this->assertArrayHasKey("bar", $p->params);
90 $this->assertArrayHasKEy("gotit", $p->params);
91
92 $this->assertTrue($p["foo"]["value"]);
93 $this->assertTrue($p["bar"]["value"]);
94 $this->assertEmpty($p["gotit"]["value"]);
95
96 $this->assertEmpty($p["foo"]["arguments"]);
97 $this->assertCount(2, $p["bar"]["arguments"]);
98 $this->assertCount(1, $p["gotit"]["arguments"]);
99
100 $this->assertEmpty($p["bar"]["arguments"]["arg"]);
101 $this->assertTrue($p["bar"]["arguments"]["bla"]);
102 $this->assertTrue($p["gotit"]["arguments"]["now"]);
103
104 $this->assertEquals($s, (string) $p);
105
106 $comp = array (
107 'foo' =>
108 array (
109 'value' => true,
110 'arguments' =>
111 array (
112 ),
113 ),
114 'bar' =>
115 array (
116 'value' => true,
117 'arguments' =>
118 array (
119 'arg' => '0',
120 'bla' => true,
121 ),
122 ),
123 'gotit' =>
124 array (
125 'value' => '0',
126 'arguments' =>
127 array (
128 'now' => true,
129 ),
130 ),
131 );
132
133 $this->assertEquals($comp, $p->params);
134 $a = new http\Params($p->params);
135 $this->assertEquals($comp, $a->toArray());
136 }
137 }