diff options
-rw-r--r-- | framework/Collections/TPriorityList.php | 18 | ||||
-rw-r--r-- | tests/unit/Collections/TPriorityListTest.php | 48 |
2 files changed, 32 insertions, 34 deletions
diff --git a/framework/Collections/TPriorityList.php b/framework/Collections/TPriorityList.php index 6ce93d49..ebb90b91 100644 --- a/framework/Collections/TPriorityList.php +++ b/framework/Collections/TPriorityList.php @@ -368,18 +368,28 @@ class TPriorityList extends TList * Removes an item from the priority list.
* The list will search for the item. The first matching item found will be removed from the list.
* @param mixed item the item to be removed.
+ * @param numeric priority of item to remove. without this parameter it defaults to false.
+ * A value of false means any priority. null will be filled in with the default priority.
* @return integer index within the flattened list at which the item is being removed
* @throws TInvalidDataValueException If the item does not exist
*/
- public function remove($item)
+ public function remove($item,$priority=false)
{
if($this->getReadOnly())
throw new TInvalidOperationException('list_readonly',get_class($this));
- if(($priority=$this->priorityOf($item, true))!==false)
+ if(($p=$this->priorityOf($item, true))!==false)
{
- $this->removeAtIndexInPriority($priority[1], $priority[0]);
- return $priority[2];
+ if($priority !== false) {
+ if($priority === null)
+ $priority = $this->getDefaultPriority();
+ $priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
+
+ if($p[0] != $priority)
+ throw new TInvalidDataValueException('list_item_inexistent');
+ }
+ $this->removeAtIndexInPriority($p[1], $p[0]);
+ return $p[2];
}
else
throw new TInvalidDataValueException('list_item_inexistent');
diff --git a/tests/unit/Collections/TPriorityListTest.php b/tests/unit/Collections/TPriorityListTest.php index 70f7c159..f329538c 100644 --- a/tests/unit/Collections/TPriorityListTest.php +++ b/tests/unit/Collections/TPriorityListTest.php @@ -583,60 +583,48 @@ class TPriorityListTest extends PHPUnit_Framework_TestCase $this->assertEquals(2, $plist->remove($this->pitem2)); $this->assertEquals(1, $plist->getPriorityCount()); - } - - public function testRemoveAtTPriorityList() - { - $plist = new TPriorityList($this->plist); - $this->assertEquals($this->pitem1, $plist->removeAt(1)); - $this->assertEquals(-1, $plist->indexOf($this->pitem1)); - $this->assertEquals(1, $plist->indexOf($this->pitem2)); - $this->assertEquals(0, $plist->indexOf($this->pfirst)); - try { - $plist->removeAt(3); - $this->fail('exception not raised when removing item with invalid index'); - } - catch (TInvalidDataValueException $e) { - } - } - - public function testRemoveFromPriorityTPriorityList() - { $plist = new TPriorityList($this->plist); try { - $plist->removeFromPriority($this->pitem5); + $plist->remove($this->pitem5); $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list'); } catch (TInvalidDataValueException $v) { } try { - $plist->removeFromPriority($this->pitem3); + $plist->remove($this->pitem3, null); $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list'); } catch (TInvalidDataValueException $v) { } try { - $plist->removeFromPriority($this->pitem3, null); + $plist->remove($this->pitem1, 100); $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list'); } catch (TInvalidDataValueException $v) { } + $plist->insertBefore($this->pitem3, $this->pitem4); + $this->assertEquals(4, $plist->remove($this->pitem3, 100)); + } + + public function testRemoveAtTPriorityList() + { + $plist = new TPriorityList($this->plist); + + $this->assertEquals($this->pitem1, $plist->removeAt(1)); + $this->assertEquals(-1, $plist->indexOf($this->pitem1)); + $this->assertEquals(1, $plist->indexOf($this->pitem2)); + $this->assertEquals(0, $plist->indexOf($this->pfirst)); try { - $plist->removeFromPriority($this->pitem1, 100); - $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list'); + $plist->removeAt(3); + $this->fail('exception not raised when removing item with invalid index'); } - catch (TInvalidDataValueException $v) { + catch (TInvalidDataValueException $e) { } - - $plist->insertBefore($this->pitem3, $this->pitem4); - $this->assertEquals(1, $plist->removeFromPriority($this->pitem3, 100)); - - } public function testItemAtIndexPriorityTPriorityList() |