tests & bugfixes
[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 testEmpty() {
21 $p = new http\Params(NULL);
22 $this->assertEquals(array(), $p->params);
23 }
24
25 function testErrorOfToArrayWithArgs() {
26 $this->setExpectedException("PHPUnit_Framework_Error_Warning");
27 $p = new http\Params();
28 $p->toArray("dummy");
29 }
30
31 function testIntegerKeys() {
32 $p = new http\Params("0=nothing;1=yes");
33 $this->assertEquals(array("0" => array("value" => "nothing", "arguments" => array(1=>"yes"))), $p->params);
34 $this->assertEquals("0=nothing;1=yes", $p->toString());
35 }
36
37 function testBoolParamArguments() {
38 $p = new http\Params;
39 $container = array("value" => false, "arguments" => array("wrong" => false, "correct" => true));
40 $p["container"] = $container;
41 $this->assertEquals("container=0;wrong=0;correct", $p->toString());
42 $this->assertEquals(array("container" => $container), $p->toArray());
43 }
44
45 function testNoArgsForParam() {
46 $p = new http\Params;
47 $p["param"] = true;
48 $this->assertEquals("param", $p->toString());
49 $p["param"] = false;
50 $this->assertEquals("param=0", $p->toString());
51 }
52
53 protected function runAssertions($p, $s) {
54 $this->assertCount(3, $p->params);
55 $this->assertArrayHasKey("foo", $p->params);
56 $this->assertArrayHasKey("bar", $p->params);
57 $this->assertArrayHasKEy("gotit", $p->params);
58
59 $this->assertTrue($p["foo"]["value"]);
60 $this->assertTrue($p["bar"]["value"]);
61 $this->assertEmpty($p["gotit"]["value"]);
62
63 $this->assertEmpty($p["foo"]["arguments"]);
64 $this->assertCount(2, $p["bar"]["arguments"]);
65 $this->assertCount(1, $p["gotit"]["arguments"]);
66
67 $this->assertEmpty($p["bar"]["arguments"]["arg"]);
68 $this->assertTrue($p["bar"]["arguments"]["bla"]);
69 $this->assertTrue($p["gotit"]["arguments"]["now"]);
70
71 $this->assertEquals($s, (string) $p);
72
73 $comp = array (
74 'foo' =>
75 array (
76 'value' => true,
77 'arguments' =>
78 array (
79 ),
80 ),
81 'bar' =>
82 array (
83 'value' => true,
84 'arguments' =>
85 array (
86 'arg' => '0',
87 'bla' => true,
88 ),
89 ),
90 'gotit' =>
91 array (
92 'value' => '0',
93 'arguments' =>
94 array (
95 'now' => true,
96 ),
97 ),
98 );
99
100 $this->assertEquals($comp, $p->params);
101 $a = new http\Params($p->params);
102 $this->assertEquals($comp, $a->toArray());
103 }
104 }