Skip to content

Commit

Permalink
Test HashKeyGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Oct 16, 2024
1 parent 11dfc86 commit 4169e05
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/Unit/KeyGenerators/HashKeyGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Rawilk\Settings\Support\Context;
use Rawilk\Settings\Support\ContextSerializers\ContextSerializer;
use Rawilk\Settings\Support\ContextSerializers\DotNotationContextSerializer;
use Rawilk\Settings\Support\KeyGenerators\HashKeyGenerator;

beforeEach(function () {
$this->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'));
});

0 comments on commit 4169e05

Please sign in to comment.