diff --git a/tests/Dashboards/RedisTest.php b/tests/Dashboards/RedisTest.php index 84a8c6d..a1e3e26 100644 --- a/tests/Dashboards/RedisTest.php +++ b/tests/Dashboards/RedisTest.php @@ -39,6 +39,15 @@ protected function setUp(): void { $this->dashboard->redis = $this->redis; } + /** + * @throws Exception + */ + protected function tearDown(): void { + foreach (['string', 'set', 'list', 'zset', 'hash', 'stream'] as $key) { + $this->redis->del('pu-test-type-'.$key); + } + } + /** * @param array|string $keys * @@ -132,25 +141,97 @@ public function testGetInfo(): void { /** * @throws Exception */ - public function testTypes(): void { + public function testStringType(): void { $this->dashboard->store('string', 'pu-test-type-string', 'svalue'); + $this->assertSame('svalue', $this->dashboard->getAllKeyValues('string', 'pu-test-type-string')); + } + + /** + * @throws Exception + */ + public function testSetType(): void { $this->dashboard->store('set', 'pu-test-type-set', 'svalue1'); $this->dashboard->store('set', 'pu-test-type-set', 'svalue2'); $this->dashboard->store('set', 'pu-test-type-set', 'svalue3'); + $this->assertEqualsCanonicalizing( + ['svalue1', 'svalue2', 'svalue3'], + $this->dashboard->getAllKeyValues('set', 'pu-test-type-set') + ); + + $subkey = array_search('svalue2', $this->redis->sMembers('pu-test-type-set'), true); + $this->dashboard->deleteSubKey('set', 'pu-test-type-set', $subkey); + $this->assertEqualsCanonicalizing( + ['svalue1', 'svalue3'], + $this->dashboard->getAllKeyValues('set', 'pu-test-type-set') + ); + } + + /** + * @throws Exception + */ + public function testListType(): void { $this->dashboard->store('list', 'pu-test-type-list', 'lvalue1'); $this->dashboard->store('list', 'pu-test-type-list', 'lvalue2'); $this->dashboard->store('list', 'pu-test-type-list', 'lvalue3'); + $this->assertEqualsCanonicalizing( + ['lvalue1', 'lvalue2', 'lvalue3'], + $this->dashboard->getAllKeyValues('list', 'pu-test-type-list') + ); + + $this->dashboard->deleteSubKey('list', 'pu-test-type-list', 1); + $this->assertEqualsCanonicalizing( + ['lvalue1', 'lvalue3'], + $this->dashboard->getAllKeyValues('list', 'pu-test-type-list') + ); + } + + /** + * @throws Exception + */ + public function testZSetType(): void { $this->dashboard->store('zset', 'pu-test-type-zset', 'zvalue1', '', ['zset_score' => 0]); $this->dashboard->store('zset', 'pu-test-type-zset', 'zvalue2', '', ['zset_score' => 1]); $this->dashboard->store('zset', 'pu-test-type-zset', 'zvalue3', '', ['zset_score' => 77]); + $this->assertEqualsCanonicalizing( + ['zvalue1', 'zvalue2', 'zvalue3'], + $this->dashboard->getAllKeyValues('zset', 'pu-test-type-zset') + ); + + $this->dashboard->deleteSubKey('zset', 'pu-test-type-zset', 1); + $this->assertEqualsCanonicalizing( + ['zvalue1', 'zvalue3'], + $this->dashboard->getAllKeyValues('zset', 'pu-test-type-zset') + ); + } + + /** + * @throws Exception + */ + public function testHashType(): void { $this->dashboard->store('hash', 'pu-test-type-hash', 'hvalue1', '', ['hash_key' => 'hashkey1']); $this->dashboard->store('hash', 'pu-test-type-hash', 'hvalue2', '', ['hash_key' => 'hashkey2']); $this->dashboard->store('hash', 'pu-test-type-hash', 'hvalue3', '', ['hash_key' => 'hashkey3']); + $this->assertEqualsCanonicalizing( + ['hashkey1' => 'hvalue1', 'hashkey2' => 'hvalue2', 'hashkey3' => 'hvalue3'], + $this->dashboard->getAllKeyValues('hash', 'pu-test-type-hash') + ); + + $this->dashboard->deleteSubKey('hash', 'pu-test-type-hash', 'hashkey2'); + $this->assertEqualsCanonicalizing( + ['hashkey1' => 'hvalue1', 'hashkey3' => 'hvalue3'], + $this->dashboard->getAllKeyValues('hash', 'pu-test-type-hash') + ); + } + + /** + * @throws Exception + */ + public function testStreamType(): void { $this->dashboard->store('stream', 'pu-test-type-stream', '', '', [ 'stream_id' => '1670541476219-0', 'stream_fields' => ['field1' => 'stvalue1', 'field2' => 'stvalue2'], @@ -160,52 +241,18 @@ public function testTypes(): void { 'stream_field' => 'field3', ]); - $expected_original = [ - 'string' => 'svalue', - 'set' => ['svalue1', 'svalue2', 'svalue3'], - 'list' => ['lvalue1', 'lvalue2', 'lvalue3'], - 'zset' => [0 => 'zvalue1', 1 => 'zvalue2', 77 => 'zvalue3'], - 'hash' => ['hashkey1' => 'hvalue1', 'hashkey2' => 'hvalue2', 'hashkey3' => 'hvalue3'], - 'stream' => [ + $this->assertEqualsCanonicalizing( + [ '1670541476219-0' => ['field1' => 'stvalue1', 'field2' => 'stvalue2'], '1670541476219-1' => ['field3' => 'stvalue3'], ], - ]; - - foreach ($expected_original as $type_o => $value_o) { - if (is_string($value_o)) { - $this->assertSame($value_o, $this->dashboard->getAllKeyValues($type_o, 'pu-test-type-'.$type_o)); - } else { - $this->assertEqualsCanonicalizing($value_o, $this->dashboard->getAllKeyValues($type_o, 'pu-test-type-'.$type_o)); - } - } - - $delete = [ - 'set' => array_search('svalue2', $this->redis->sMembers('pu-test-type-set'), true), - 'list' => 1, - 'zset' => 1, - 'hash' => 'hashkey2', - 'stream' => '1670541476219-0', - ]; - - foreach ($delete as $type_d => $id) { - $this->dashboard->deleteSubKey($type_d, 'pu-test-type-'.$type_d, $id); - } - - $expected_new = [ - 'set' => ['svalue1', 'svalue3'], - 'list' => ['lvalue1', 'lvalue3'], - 'zset' => [0 => 'zvalue1', 77 => 'zvalue3'], - 'hash' => ['hashkey1' => 'hvalue1', 'hashkey3' => 'hvalue3'], - 'stream' => ['1670541476219-1' => ['field3' => 'stvalue3']], - ]; - - foreach ($expected_new as $type_n => $value_n) { - $this->assertEqualsCanonicalizing($value_n, $this->dashboard->getAllKeyValues($type_n, 'pu-test-type-'.$type_n)); - } + $this->dashboard->getAllKeyValues('stream', 'pu-test-type-stream') + ); - foreach (['string', 'set', 'list', 'zset', 'hash', 'stream'] as $key) { - $this->redis->del('pu-test-type-'.$key); - } + $this->dashboard->deleteSubKey('stream', 'pu-test-type-stream', '1670541476219-0'); + $this->assertEqualsCanonicalizing( + ['1670541476219-1' => ['field3' => 'stvalue3']], + $this->dashboard->getAllKeyValues('stream', 'pu-test-type-stream') + ); } } diff --git a/tests/PaginatorTest.php b/tests/PaginatorTest.php index 900e951..afbe2bd 100644 --- a/tests/PaginatorTest.php +++ b/tests/PaginatorTest.php @@ -17,10 +17,10 @@ final class PaginatorTest extends TestCase { protected function setUp(): void { $items = [ - ['key' => 'value1', 'title' => 'value1'], - ['key' => 'value2', 'title' => 'value2'], - ['key' => 'value3', 'title' => 'value3'], - ['key' => 'value4', 'title' => 'value4'], + 1 => ['key' => 'value1', 'title' => 'value1'], + 2 => ['key' => 'value2', 'title' => 'value2'], + 3 => ['key' => 'value3', 'title' => 'value3'], + 4 => ['key' => 'value4', 'title' => 'value4'], ]; $_GET['p'] = 2; @@ -31,8 +31,8 @@ protected function setUp(): void { public function testGetPaginated(): void { $expected = [ - ['key' => 'value3', 'title' => 'value3'], - ['key' => 'value4', 'title' => 'value4'], + 3 => ['key' => 'value3', 'title' => 'value3'], + 4 => ['key' => 'value4', 'title' => 'value4'], ]; $this->assertEqualsCanonicalizing($expected, $this->paginator->getPaginated());