Gusev's proposed patch
[htmlpurifier.git] / tests / HTMLPurifier / ArrayTest.php
blob7e51d47409629441f1587d73133d6bf6c2a55e99
1 <?php
3 class HTMLPurifier_ArrayTest extends UnitTestCase
5 /**
6 * Data provider for the rest of tests
7 * @return array
8 */
9 public function getData()
11 return array(
12 array(array()),
13 array(array(1, 2, 3, 4))
17 /**
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());
31 /**
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 ++) {
39 $object[$i];
40 $this->assertEqual($i, $object->getOffset());
41 $this->assertEqual($array[$i], $object->getOffsetItem()->value);
44 $object[2];
45 $this->assertEqual(2, $object->getOffset());
46 $this->assertEqual(3, $object->getOffsetItem()->value);
47 $object->remove(2);
48 $this->assertEqual(2, $object->getOffset());
49 $this->assertEqual(4, $object->getOffsetItem()->value);
51 $object[1];
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);
59 /**
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);
67 $index = 0;
68 array_splice($array, $index, 0, array('a'));
69 $object->insertBefore($index, 'a');
70 $this->assertEqual($array, $object->getArray());
72 $index = 2;
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());
83 /**
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);
91 $index = 0;
92 array_splice($array, $index, 1);
93 $object->remove($index);
94 $this->assertEqual($array, $object->getArray());
96 $index = 2;
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) {
147 $v = $v * 2;
148 $object[$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));
160 unset($object[1]);
161 $this->assertEqual(array(1, 3, 4), $object->getArray());
162 unset($object[0]);
163 $this->assertEqual(array(3, 4), $object->getArray());
164 unset($object[1]);
165 $this->assertEqual(array(3), $object->getArray());
166 unset($object[0]);
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()
181 return $this->head;
185 * @return int
187 public function getOffset()
189 return $this->offset;
193 * @return int
195 public function getCount()
197 return $this->count;
201 * @return HTMLPurifier_ArrayNode|null
203 public function getOffsetItem()
205 return $this->offsetItem;