Skip to content

Commit 4169e05

Browse files
committed
Test HashKeyGenerator
1 parent 11dfc86 commit 4169e05

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rawilk\Settings\Support\Context;
6+
use Rawilk\Settings\Support\ContextSerializers\ContextSerializer;
7+
use Rawilk\Settings\Support\ContextSerializers\DotNotationContextSerializer;
8+
use Rawilk\Settings\Support\KeyGenerators\HashKeyGenerator;
9+
10+
beforeEach(function () {
11+
$this->keyGenerator = (new HashKeyGenerator)
12+
->setContextSerializer(new ContextSerializer);
13+
14+
config([
15+
'settings.hash_algorithm' => 'xxh128',
16+
]);
17+
});
18+
19+
it('generates a hash of a key', function () {
20+
// N; is for a serialized null context object
21+
expect($this->keyGenerator->generate('my-key'))->toBe(hash('xxh128', 'my-keyN;'));
22+
});
23+
24+
it('generates a hash of a key and context object', function () {
25+
$context = new Context([
26+
'id' => 123,
27+
]);
28+
29+
expect($this->keyGenerator->generate('my-key', $context))
30+
->toBe(hash('xxh128', 'my-key' . serialize($context)));
31+
});
32+
33+
it('works with other context serializers', function () {
34+
$this->keyGenerator->setContextSerializer(new DotNotationContextSerializer);
35+
36+
$context = new Context([
37+
'id' => 123,
38+
'bool-value' => false,
39+
]);
40+
41+
expect($this->keyGenerator->generate('my-key', $context))
42+
->toBe(hash('xxh128', 'my-keyid:123::bool-value:0'));
43+
});

0 commit comments

Comments
 (0)