queue = new Zend_Stdlib_PriorityQueue(); $this->queue->insert('foo', 3); $this->queue->insert('bar', 4); $this->queue->insert('baz', 2); $this->queue->insert('bat', 1); } public function testSerializationAndDeserializationShouldMaintainState() { $s = serialize($this->queue); $unserialized = unserialize($s); $count = count($this->queue); $this->assertSame($count, count($unserialized), 'Expected count ' . $count . '; received ' . count($unserialized)); $expected = array(); foreach ($this->queue as $item) { $expected[] = $item; } $test = array(); foreach ($unserialized as $item) { $test[] = $item; } $this->assertSame($expected, $test, 'Expected: ' . var_export($expected, 1) . "\nReceived:" . var_export($test, 1)); } public function testRetrievingQueueAsArrayReturnsDataOnlyByDefault() { $expected = array( 'foo', 'bar', 'baz', 'bat', ); $test = $this->queue->toArray(); $this->assertSame($expected, $test, var_export($test, 1)); } public function testCanCastToArrayOfPrioritiesOnly() { $expected = array( 3, 4, 2, 1, ); $test = $this->queue->toArray(Zend_Stdlib_PriorityQueue::EXTR_PRIORITY); $this->assertSame($expected, $test, var_export($test, 1)); } public function testCanCastToArrayOfDataPriorityPairs() { $expected = array( array('data' => 'foo', 'priority' => 3), array('data' => 'bar', 'priority' => 4), array('data' => 'baz', 'priority' => 2), array('data' => 'bat', 'priority' => 1), ); $test = $this->queue->toArray(Zend_Stdlib_PriorityQueue::EXTR_BOTH); $this->assertSame($expected, $test, var_export($test, 1)); } public function testCanIterateMultipleTimesAndReceiveSameResults() { $expected = array('bar', 'foo', 'baz', 'bat'); for ($i = 1; $i < 3; $i++) { $test = array(); foreach ($this->queue as $item) { $test[] = $item; } $this->assertEquals($expected, $test, 'Failed at iteration ' . $i); } } public function testCanRemoveItemFromQueue() { $this->queue->remove('baz'); $expected = array('bar', 'foo', 'bat'); $test = array(); foreach ($this->queue as $item) { $test[] = $item; } $this->assertEquals($expected, $test); } public function testCanTestForExistenceOfItemInQueue() { $this->assertTrue($this->queue->contains('foo')); $this->assertFalse($this->queue->contains('foobar')); } public function testCanTestForExistenceOfPriorityInQueue() { $this->assertTrue($this->queue->hasPriority(3)); $this->assertFalse($this->queue->hasPriority(1000)); } } if (PHPUnit_MAIN_METHOD == 'Zend_Stdlib_PriorityQueueTest::main') { Zend_Stdlib_PriorityQueueTest::main(); }