Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public static function push(string|array $className, array $data = [], array $op
*/
public static function getUniqueId(string $class, string $method, array $data): string
{
sort($data);
$data = static::sortUniqueValues($data);

$hashInput = implode('', [
$class,
Expand All @@ -314,4 +314,23 @@ public static function getUniqueId(string $class, string $method, array $data):

return hash('md5', $hashInput);
}

/**
* Recursively sort an array by key
*
* @param array<string, mixed> $data The data to sort
* @return array<string, mixed> The sorted array
*/
protected static function sortUniqueValues(array $data): array
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = static::sortUniqueValues($value);
}
}

ksort($data);

return $data;
}
}
36 changes: 36 additions & 0 deletions tests/TestCase/QueueManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ public function tearDown(): void
array_map('unlink', glob($this->fsQueuePath . DS . '*'));
}

public function testGetUniqueId()
{
$first = QueueManager::getUniqueId('Example', 'hello', [1, 2, 3]);
$second = QueueManager::getUniqueId('Example', 'hello', [3, 2, 1]);
$this->assertNotEquals($first, $second, 'values are sorted by key');

$second = QueueManager::getUniqueId('Human', 'hello', [3, 2, 1]);
$this->assertNotEquals($first, $second, 'class changes hash');

$second = QueueManager::getUniqueId('Example', 'bye', [3, 2, 1]);
$this->assertNotEquals($first, $second, 'method changes hash');

$first = QueueManager::getUniqueId('Example', 'hello', ['user_id' => 'admin', 'role' => 'guest']);
$second = QueueManager::getUniqueId('Example', 'hello', ['user_id' => 'admin', 'role' => 'guest']);
$this->assertSame($first, $second, 'same values and keys are the same');

$second = QueueManager::getUniqueId('Example', 'hello', ['role' => 'guest', 'user_id' => 'admin']);
$this->assertSame($first, $second, 'reordered keys are the same');

$first = QueueManager::getUniqueId('Example', 'hello', ['user_id' => 'admin', 'role' => 'guest']);
$second = QueueManager::getUniqueId('Example', 'hello', ['user_id' => 'admin', 'role' => 'admin']);
$this->assertNotEquals($first, $second, 'different values are distinct');

$first = QueueManager::getUniqueId('Example', 'hello', ['foo' => 'admin', 'bar' => 'guest']);
$second = QueueManager::getUniqueId('Example', 'hello', ['user_id' => 'admin', 'role' => 'guest']);
$this->assertNotEquals($first, $second, 'different keys, same values are distinct');

$first = QueueManager::getUniqueId('Example', 'hello', ['foo' => 'foo', 'bar' => 'foo']);
$second = QueueManager::getUniqueId('Example', 'hello', ['user_id' => 'admin', 'role' => 'guest']);
$this->assertNotEquals($first, $second, 'different keys and values, are distinct');

$first = QueueManager::getUniqueId('Example', 'hello', ['arr' => ['a' => 1, 'b' => 2]]);
$second = QueueManager::getUniqueId('Example', 'hello', ['arr' => ['b' => 2, 'a' => 1]]);
$this->assertEquals($first, $second, 'nested arrays are sorted too');
}

public function testSetConfig()
{
QueueManager::setConfig('test', [
Expand Down
Loading