3 class HTMLPurifier_ArrayTest
extends UnitTestCase
6 * Data provider for the rest of tests
9 public function getData()
13 array(array(1, 2, 3, 4))
18 * Testing of initialization of properties of HTMLPurifier_Array
20 public function testConstruct()
22 $array = $this->getData();
23 $object = new HTMLPurifier_ArrayMock($array);
25 $this->assertEqual(0, $object->getOffset());
26 $this->assertEqual($object->getHead(), $object->getOffsetItem());
27 $this->assertEqual(count($array), $object->getCount());
28 $this->assertEqual($array, $object->getArray());
32 * Testing of offset & offsetItem properties while seeking/removing/inserting
34 public function testFindIndex()
36 $array = array(1, 2, 3, 4, 5);
37 $object = new HTMLPurifier_ArrayMock($array);
38 for ($i = 0; $i < $object->getCount(); $i ++
) {
40 $this->assertEqual($i, $object->getOffset());
41 $this->assertEqual($array[$i], $object->getOffsetItem()->value
);
45 $this->assertEqual(2, $object->getOffset());
46 $this->assertEqual(3, $object->getOffsetItem()->value
);
48 $this->assertEqual(2, $object->getOffset());
49 $this->assertEqual(4, $object->getOffsetItem()->value
);
52 $this->assertEqual(1, $object->getOffset());
53 $this->assertEqual(2, $object->getOffsetItem()->value
);
54 $object->insertBefore(1, 'a');
55 $this->assertEqual(1, $object->getOffset());
56 $this->assertEqual('a', $object->getOffsetItem()->value
);
60 * Testing that behavior of insertBefore the same as array_splice
62 public function testInsertBefore()
64 $array = $this->getData();
65 $object = new HTMLPurifier_ArrayMock($array);
68 array_splice($array, $index, 0, array('a'));
69 $object->insertBefore($index, 'a');
70 $this->assertEqual($array, $object->getArray());
73 array_splice($array, $index, 0, array('a'));
74 $object->insertBefore($index, 'a');
75 $this->assertEqual($array, $object->getArray());
77 $index = count($array) * 2;
78 array_splice($array, $index, 0, array('a'));
79 $object->insertBefore($index, 'a');
80 $this->assertEqual($array, $object->getArray());
84 * Testing that behavior of remove the same as array_splice
86 public function testRemove()
88 $array = $this->getData();
89 $object = new HTMLPurifier_ArrayMock($array);
92 array_splice($array, $index, 1);
93 $object->remove($index);
94 $this->assertEqual($array, $object->getArray());
97 array_splice($array, $index, 1);
98 $object->remove($index);
99 $this->assertEqual($array, $object->getArray());
101 $index = count($array) * 2;
102 array_splice($array, $index, 1);
103 $object->remove($index);
104 $this->assertEqual($array, $object->getArray());
108 * Testing that object returns original array
110 public function testGetArray()
112 $array = $this->getData();
113 $object = new HTMLPurifier_ArrayMock($array);
114 $this->assertEqual($array, $object->getArray());
118 * Testing ArrayAccess interface
120 public function testOffsetExists()
122 $array = $this->getData();
123 $object = new HTMLPurifier_ArrayMock($array);
124 $this->assertEqual(isset($array[0]), isset($object[0]));
128 * Testing ArrayAccess interface
130 public function testOffsetGet()
132 $array = array(1, 2, 3);
133 $object = new HTMLPurifier_ArrayMock($array);
134 foreach ($array as $k => $v) {
135 $this->assertEqual($v, $object[$k]);
140 * Testing ArrayAccess interface
142 public function testOffsetSet()
144 $array = array(1, 2, 3);
145 $object = new HTMLPurifier_ArrayMock($array);
146 foreach ($array as $k => $v) {
149 $this->assertEqual($v, $object[$k]);
154 * Testing ArrayAccess interface
155 * There is one difference: keys are updated as well, they are started from 0
157 public function testOffsetUnset()
159 $object = new HTMLPurifier_ArrayMock(array(1, 2, 3, 4));
161 $this->assertEqual(array(1, 3, 4), $object->getArray());
163 $this->assertEqual(array(3, 4), $object->getArray());
165 $this->assertEqual(array(3), $object->getArray());
167 $this->assertEqual(array(), $object->getArray());
172 * Mock for some protected properties of HTMLPurifier_Array
174 class HTMLPurifier_ArrayMock
extends HTMLPurifier_Array
177 * @return HTMLPurifier_ArrayNode|null
179 public function getHead()
187 public function getOffset()
189 return $this->offset
;
195 public function getCount()
201 * @return HTMLPurifier_ArrayNode|null
203 public function getOffsetItem()
205 return $this->offsetItem
;