diff --git a/src/QueueManager.php b/src/QueueManager.php index 976fd6c..9a8bd89 100644 --- a/src/QueueManager.php +++ b/src/QueueManager.php @@ -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, @@ -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 $data The data to sort + * @return array 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; + } } diff --git a/tests/TestCase/QueueManagerTest.php b/tests/TestCase/QueueManagerTest.php index 85c8474..c584751 100644 --- a/tests/TestCase/QueueManagerTest.php +++ b/tests/TestCase/QueueManagerTest.php @@ -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', [