From 4169e0531c6af3563db231d0baedbe709e5357e9 Mon Sep 17 00:00:00 2001 From: Randall Wilk Date: Wed, 16 Oct 2024 10:14:13 -0500 Subject: [PATCH] Test HashKeyGenerator --- .../KeyGenerators/HashKeyGeneratorTest.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/Unit/KeyGenerators/HashKeyGeneratorTest.php diff --git a/tests/Unit/KeyGenerators/HashKeyGeneratorTest.php b/tests/Unit/KeyGenerators/HashKeyGeneratorTest.php new file mode 100644 index 0000000..cc68055 --- /dev/null +++ b/tests/Unit/KeyGenerators/HashKeyGeneratorTest.php @@ -0,0 +1,43 @@ +keyGenerator = (new HashKeyGenerator) + ->setContextSerializer(new ContextSerializer); + + config([ + 'settings.hash_algorithm' => 'xxh128', + ]); +}); + +it('generates a hash of a key', function () { + // N; is for a serialized null context object + expect($this->keyGenerator->generate('my-key'))->toBe(hash('xxh128', 'my-keyN;')); +}); + +it('generates a hash of a key and context object', function () { + $context = new Context([ + 'id' => 123, + ]); + + expect($this->keyGenerator->generate('my-key', $context)) + ->toBe(hash('xxh128', 'my-key' . serialize($context))); +}); + +it('works with other context serializers', function () { + $this->keyGenerator->setContextSerializer(new DotNotationContextSerializer); + + $context = new Context([ + 'id' => 123, + 'bool-value' => false, + ]); + + expect($this->keyGenerator->generate('my-key', $context)) + ->toBe(hash('xxh128', 'my-keyid:123::bool-value:0')); +});