| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Unit test suite for HTML_Common2 class
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* LICENSE:
|
|
|
8 |
*
|
|
|
9 |
* Copyright (c) 2004-2010, Alexey Borzov <avb@php.net>
|
|
|
10 |
*
|
|
|
11 |
* All rights reserved.
|
|
|
12 |
*
|
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
|
14 |
* modification, are permitted provided that the following conditions
|
|
|
15 |
* are met:
|
|
|
16 |
*
|
|
|
17 |
* * Redistributions of source code must retain the above copyright
|
|
|
18 |
* notice, this list of conditions and the following disclaimer.
|
|
|
19 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
20 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
21 |
* documentation and/or other materials provided with the distribution.
|
|
|
22 |
* * The names of the authors may not be used to endorse or promote products
|
|
|
23 |
* derived from this software without specific prior written permission.
|
|
|
24 |
*
|
|
|
25 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
26 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
27 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
28 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
29 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
30 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
31 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
32 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
33 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
34 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
35 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
36 |
*
|
|
|
37 |
* @category HTML
|
|
|
38 |
* @package HTML_Common2
|
|
|
39 |
* @author Alexey Borzov <avb@php.net>
|
|
|
40 |
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|
|
41 |
* @version CVS: $Id: HTML_Common2_Test.php 304516 2010-10-19 18:43:58Z avb $
|
|
|
42 |
* @link http://pear.php.net/package/HTML_Common2
|
|
|
43 |
*/
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* PHPUnit Test Case
|
|
|
47 |
*/
|
|
|
48 |
require_once 'PHPUnit/Framework/TestCase.php';
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* HTML_Common2 class
|
|
|
52 |
*/
|
|
|
53 |
require_once 'HTML/Common2.php';
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* A non-abstract subclass of HTML_Common2
|
|
|
57 |
*
|
|
|
58 |
* HTML_Common2 cannot be instantiated due to abstract __toString() method,
|
|
|
59 |
* we need to (sort of) implement that.
|
|
|
60 |
*/
|
|
|
61 |
class HTML_Common2_Concrete extends HTML_Common2
|
|
|
62 |
{
|
|
|
63 |
public function __toString()
|
|
|
64 |
{
|
|
|
65 |
return '';
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* A subclass to test the 'watched attributes' functionality of HTML_Common2
|
|
|
71 |
*
|
|
|
72 |
* Two attributes are watched here: 'readonly' and 'uppercase'. The former
|
|
|
73 |
* should not be changed by any of the methods and the value of the latter
|
|
|
74 |
* should always be uppercase. This is achieved by implementing the
|
|
|
75 |
* onAttributeChange() method defined in HTML_Common2
|
|
|
76 |
*/
|
|
|
77 |
class HTML_Common2_WatchedAttributes extends HTML_Common2_Concrete
|
|
|
78 |
{
|
|
|
79 |
protected $watchedAttributes = array('readonly', 'uppercase');
|
|
|
80 |
|
|
|
81 |
protected $attributes = array(
|
|
|
82 |
'readonly' => 'this attribute is readonly',
|
|
|
83 |
'uppercase' => 'VALUE OF THIS IS ALWAYS UPPERCASE'
|
|
|
84 |
);
|
|
|
85 |
|
|
|
86 |
protected function onAttributeChange($name, $value = null)
|
|
|
87 |
{
|
|
|
88 |
if ('readonly' == $name) {
|
|
|
89 |
return;
|
|
|
90 |
}
|
|
|
91 |
if ('uppercase' == $name) {
|
|
|
92 |
if (null === $value) {
|
|
|
93 |
unset($this->attributes[$name]);
|
|
|
94 |
} else {
|
|
|
95 |
$this->attributes[$name] = strtoupper($value);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Unit test for HTML_Common2 class
|
|
|
103 |
*/
|
|
|
104 |
class HTML_Common2_Test extends PHPUnit_Framework_TestCase
|
|
|
105 |
{
|
|
|
106 |
public function testUnknownOptionIsNull()
|
|
|
107 |
{
|
|
|
108 |
$this->assertNull(HTML_Common2::getOption('foobar'));
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public function testAnyOptionAllowed()
|
|
|
112 |
{
|
|
|
113 |
HTML_Common2::setOption('foobar', 'baz');
|
|
|
114 |
$this->assertEquals('baz', HTML_Common2::getOption('foobar'));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public function testArrayOfOptionsAllowed()
|
|
|
118 |
{
|
|
|
119 |
HTML_Common2::setOption(array(
|
|
|
120 |
'quux' => 'xyzzy'
|
|
|
121 |
));
|
|
|
122 |
$this->assertEquals('xyzzy', HTML_Common2::getOption('quux'));
|
|
|
123 |
|
|
|
124 |
$this->assertArrayHasKey('charset', HTML_Common2::getOption());
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public function testConstructorSetsDefaultAttributes()
|
|
|
128 |
{
|
|
|
129 |
$obj = new HTML_Common2_Concrete();
|
|
|
130 |
$this->assertEquals(array(), $obj->getAttributes());
|
|
|
131 |
$obj = new HTML_Common2_Concrete(array('foo' => 'bar'));
|
|
|
132 |
$this->assertEquals(array('foo' => 'bar'), $obj->getAttributes());
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public function testUnknownAttributeIsNull()
|
|
|
136 |
{
|
|
|
137 |
$obj = new HTML_Common2_Concrete();
|
|
|
138 |
$this->assertNull($obj->getAttribute('foobar'));
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public function testAttributeNamesAreLowercased()
|
|
|
142 |
{
|
|
|
143 |
$obj = new HTML_Common2_Concrete();
|
|
|
144 |
$obj->setAttributes(array('BAZ' => 'quux'));
|
|
|
145 |
$obj->setAttribute('Foo', 'bar');
|
|
|
146 |
$obj->mergeAttributes(array('XyZZy' => 'xyzzy value'));
|
|
|
147 |
|
|
|
148 |
$this->assertEquals('bar', $obj->getAttribute('FOO'));
|
|
|
149 |
|
|
|
150 |
$obj->removeAttribute('fOO');
|
|
|
151 |
$this->assertEquals(
|
|
|
152 |
array('baz' => 'quux', 'xyzzy' => 'xyzzy value'),
|
|
|
153 |
$obj->getAttributes()
|
|
|
154 |
);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public function testAttributeValuesAreStrings()
|
|
|
158 |
{
|
|
|
159 |
$obj = new HTML_Common2_Concrete();
|
|
|
160 |
$obj->setAttributes(array('foo' => null, 'bar' => 10));
|
|
|
161 |
$obj->setAttribute('baz', 2.5);
|
|
|
162 |
$obj->mergeAttributes(array('foobar' => 42));
|
|
|
163 |
foreach ($obj->getAttributes() as $attribute) {
|
|
|
164 |
$this->assertType('string', $attribute);
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
public function testDefaultIndentLevelIsZero()
|
|
|
169 |
{
|
|
|
170 |
$obj = new HTML_Common2_Concrete();
|
|
|
171 |
$this->assertEquals(0, $obj->getIndentLevel());
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
public function testIndentLevelIsNonnegative()
|
|
|
175 |
{
|
|
|
176 |
$obj = new HTML_Common2_Concrete();
|
|
|
177 |
$obj->setIndentLevel(-1);
|
|
|
178 |
$this->assertEquals(0, $obj->getIndentLevel());
|
|
|
179 |
$obj->setIndentLevel(1);
|
|
|
180 |
$this->assertEquals(1, $obj->getIndentLevel());
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
public function testDefaultCommentIsNull()
|
|
|
184 |
{
|
|
|
185 |
$obj = new HTML_Common2_Concrete();
|
|
|
186 |
$this->assertNull($obj->getComment());
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
public function testAttributesAsStringAccepted()
|
|
|
190 |
{
|
|
|
191 |
$obj = new HTML_Common2_Concrete('multiple style= "height: 2em;" class=\'foo\' width=100% ');
|
|
|
192 |
$this->assertEquals(
|
|
|
193 |
array('multiple' => 'multiple', 'style' => 'height: 2em;',
|
|
|
194 |
'class' => 'foo', 'width' => '100%'),
|
|
|
195 |
$obj->getAttributes()
|
|
|
196 |
);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
public function testNonXhtmlAttributesTransformed()
|
|
|
200 |
{
|
|
|
201 |
$obj = new HTML_Common2_Concrete(array('multiple'));
|
|
|
202 |
$obj->setAttribute('selected');
|
|
|
203 |
$obj->mergeAttributes('checked nowrap');
|
|
|
204 |
$this->assertEquals(
|
|
|
205 |
array('multiple' => 'multiple', 'selected' => 'selected',
|
|
|
206 |
'checked' => 'checked', 'nowrap' => 'nowrap'),
|
|
|
207 |
$obj->getAttributes()
|
|
|
208 |
);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
public function testWellFormedXhtmlGenerated()
|
|
|
212 |
{
|
|
|
213 |
$obj = new HTML_Common2_Concrete(array('foo' => 'bar&"baz"', 'quux' => 'xyz\'zy'));
|
|
|
214 |
$this->assertEquals(
|
|
|
215 |
' foo="bar&"baz"" quux="xyz'zy"',
|
|
|
216 |
$obj->getAttributes(true)
|
|
|
217 |
);
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
public function testCanWatchAttributes()
|
|
|
221 |
{
|
|
|
222 |
$obj = new HTML_Common2_WatchedAttributes();
|
|
|
223 |
|
|
|
224 |
$obj->setAttributes(array('readonly' => 'something', 'uppercase' => 'new value', 'foo' => 'bar'));
|
|
|
225 |
$this->assertEquals(
|
|
|
226 |
array('readonly' => 'this attribute is readonly', 'uppercase' => 'NEW VALUE', 'foo' => 'bar'),
|
|
|
227 |
$obj->getAttributes()
|
|
|
228 |
);
|
|
|
229 |
|
|
|
230 |
$obj->mergeAttributes(array('readonly' => 'something', 'uppercase' => 'other value', 'foo' => 'baz'));
|
|
|
231 |
$this->assertEquals(
|
|
|
232 |
array('readonly' => 'this attribute is readonly', 'uppercase' => 'OTHER VALUE', 'foo' => 'baz'),
|
|
|
233 |
$obj->getAttributes()
|
|
|
234 |
);
|
|
|
235 |
|
|
|
236 |
$obj->setAttribute('readonly', 'something else');
|
|
|
237 |
$obj->setAttribute('uppercase', 'yet another value');
|
|
|
238 |
$obj->setAttribute('foo', 'quux');
|
|
|
239 |
$this->assertEquals(
|
|
|
240 |
array('readonly' => 'this attribute is readonly', 'uppercase' => 'YET ANOTHER VALUE', 'foo' => 'quux'),
|
|
|
241 |
$obj->getAttributes()
|
|
|
242 |
);
|
|
|
243 |
|
|
|
244 |
$obj->removeAttribute('readonly');
|
|
|
245 |
$obj->removeAttribute('uppercase');
|
|
|
246 |
$obj->removeAttribute('foo');
|
|
|
247 |
$this->assertEquals(
|
|
|
248 |
array('readonly' => 'this attribute is readonly'),
|
|
|
249 |
$obj->getAttributes()
|
|
|
250 |
);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
public function testFluentInterfaces()
|
|
|
254 |
{
|
|
|
255 |
$obj = new HTML_Common2_Concrete();
|
|
|
256 |
|
|
|
257 |
$this->assertSame($obj, $obj->setAttributes(array('foo' => 'foo value')));
|
|
|
258 |
$this->assertSame($obj, $obj->mergeAttributes(array('bar' => 'bar value')));
|
|
|
259 |
$this->assertSame($obj, $obj->setAttribute('baz', 'baz value'));
|
|
|
260 |
$this->assertSame($obj, $obj->removeAttribute('bar'));
|
|
|
261 |
$this->assertSame($obj, $obj->setComment('A comment'));
|
|
|
262 |
$this->assertSame($obj, $obj->setIndentLevel(3));
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
public function testCanAddCssClasses()
|
|
|
266 |
{
|
|
|
267 |
$obj = new HTML_Common2_Concrete();
|
|
|
268 |
|
|
|
269 |
$obj->addClass('foobar');
|
|
|
270 |
$obj->addClass('foobar');
|
|
|
271 |
$this->assertEquals('foobar', $obj->getAttribute('class'));
|
|
|
272 |
|
|
|
273 |
$obj->addClass('quux xyzzy');
|
|
|
274 |
$this->assertFalse($obj->hasClass('bar'));
|
|
|
275 |
$this->assertTrue($obj->hasClass('foobar'));
|
|
|
276 |
$this->assertTrue($obj->hasClass('quux'));
|
|
|
277 |
$this->assertTrue($obj->hasClass('xyzzy'));
|
|
|
278 |
|
|
|
279 |
$obj->addClass(array('newclass'));
|
|
|
280 |
$this->assertTrue($obj->hasClass('newclass'));
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
public function testCanRemoveCssClasses()
|
|
|
284 |
{
|
|
|
285 |
$obj = new HTML_Common2_Concrete(array('class' => 'foobar quux xyzzy'));
|
|
|
286 |
|
|
|
287 |
$obj->removeClass('foobar xyzzy');
|
|
|
288 |
$this->assertFalse($obj->hasClass('xyzzy'));
|
|
|
289 |
$this->assertFalse($obj->hasClass('foobar'));
|
|
|
290 |
$this->assertTrue($obj->hasClass('quux'));
|
|
|
291 |
|
|
|
292 |
$obj->removeClass(array('quux'));
|
|
|
293 |
$this->assertEquals(null, $obj->getAttribute('class'));
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
?>
|