Skip to content

Commit

Permalink
Merge pull request #14 from laminas/3.3.x-merge-up-into-3.4.x_5fb6d33…
Browse files Browse the repository at this point in the history
…c57f0f5.78323193

Merge release 3.3.1 into 3.4.x
  • Loading branch information
weierophinney authored Nov 19, 2020
2 parents 06fd144 + 3a0ddf2 commit f0b299d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 13 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 3.3.1 - 2020-11-19

### Fixed

- [#13](https://github.com/laminas/laminas-stdlib/pull/13) fixes an issue whereby calling `Laminas\Stdlib\PriorityQueue::extract()` left the instance in an invalid state, causing other functionality like `count()` and `contains()` to reflect the prior state.

-----

### Release Notes for [3.3.1](https://github.com/laminas/laminas-stdlib/milestone/2)

- Total issues resolved: **1**
- Total pull requests resolved: **1**
- Total contributors: **2**

#### Bug

- [13: Ensure PriorityQueue::extract also removes item from internal list](https://github.com/laminas/laminas-stdlib/pull/13) thanks to @weierophinney and @lougv1

## 3.3.0 - 2020-08-25

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"phpbench/phpbench": "^0.17.1",
"phpunit/phpunit": "^9.3.7"
"phpunit/phpunit": "~9.3.7"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 11 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="laminas-stdlib Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
31 changes: 29 additions & 2 deletions src/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
* Inner queue class to use for iteration
* @var string
*/
protected $queueClass = 'Laminas\Stdlib\SplPriorityQueue';
protected $queueClass = SplPriorityQueue::class;

/**
* Actual items aggregated in the priority queue. Each item is an array
Expand Down Expand Up @@ -153,7 +153,34 @@ public function top()
*/
public function extract()
{
return $this->getQueue()->extract();
$value = $this->getQueue()->extract();

$keyToRemove = null;
$highestPriority = null;
foreach ($this->items as $key => $item) {
if ($item['data'] !== $value) {
continue;
}

if (null === $highestPriority) {
$highestPriority = $item['priority'];
$keyToRemove = $key;
continue;
}

if ($highestPriority >= $item['priority']) {
continue;
}

$highestPriority = $item['priority'];
$keyToRemove = $key;
}

if ($keyToRemove !== null) {
unset($this->items[$keyToRemove]);
}

return $value;
}

/**
Expand Down
44 changes: 44 additions & 0 deletions test/PriorityQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,48 @@ public function testQueueRevertsToInitialStateWhenEmpty()

self::assertEquals($queue, $testQueue);
}

/**
* @see https://github.com/laminas/laminas-stdlib/issues/12
*/
public function testUpdatesCountAfterExtractingTopElement(): void
{
$queue = new PriorityQueue();
$queue->insert('first');
$queue->insert('second');

$queue->extract();

$this->assertCount(1, $queue);
}

/**
* @see https://github.com/laminas/laminas-stdlib/issues/12
*/
public function testTopValueNotFoundAfterExtractingTopElement(): void
{
$queue = new PriorityQueue();
$queue->insert('first');
$queue->insert('second');

$queue->extract();

$this->assertFalse($queue->contains('first'));
}

/**
* @see https://github.com/laminas/laminas-stdlib/issues/12
*/
public function testValueStillFoundAfterExtractingTopElementWhenItIsADuplicate(): void
{
$queue = new PriorityQueue();
$queue->insert('first');
$queue->insert('second');
$queue->insert('first');

$queue->extract();

$this->assertCount(2, $queue);
$this->assertTrue($queue->contains('first'));
}
}

0 comments on commit f0b299d

Please sign in to comment.