|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://doc.hyperf.io |
| 9 | + |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | + |
| 13 | +namespace HyperfTest\Redis\Lua; |
| 14 | + |
| 15 | +use Hyperf\Redis\Lua\Hash\HGetAllMultiple; |
| 16 | +use Hyperf\Redis\Lua\Hash\HIncrByFloatIfExists; |
| 17 | +use Hyperf\Utils\Str; |
| 18 | +use HyperfTest\Redis\Stub\ContainerStub; |
| 19 | +use Mockery; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + * @coversNothing |
| 25 | + */ |
| 26 | +class HashTest extends TestCase |
| 27 | +{ |
| 28 | + protected function tearDown() |
| 29 | + { |
| 30 | + $container = ContainerStub::mockContainer(); |
| 31 | + $redis = $container->get(\Redis::class); |
| 32 | + $redis->flushDB(); |
| 33 | + |
| 34 | + Mockery::close(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testEvalHGetAllMultiple() |
| 38 | + { |
| 39 | + $container = ContainerStub::mockContainer(); |
| 40 | + $redis = $container->get(\Redis::class); |
| 41 | + $redis->hMSet('{hash}:1', ['id' => 1, 'name' => $name1 = 'Hyperf']); |
| 42 | + $redis->hMSet('{hash}:2', ['id' => 2, 'name' => $name2 = Str::random(16)]); |
| 43 | + $redis->hMSet('{hash}:3', ['id' => 3, 'name' => $name3 = uniqid()]); |
| 44 | + $script = new HGetAllMultiple($container); |
| 45 | + $result = $script->eval(['{hash}:1', '{hash}:2', '{hash}:3']); |
| 46 | + |
| 47 | + $this->assertEquals(['id' => 1, 'name' => $name1], array_shift($result)); |
| 48 | + $this->assertEquals(['id' => 2, 'name' => $name2], array_shift($result)); |
| 49 | + $this->assertEquals(['id' => 3, 'name' => $name3], array_shift($result)); |
| 50 | + $this->assertSame([], $result); |
| 51 | + } |
| 52 | + |
| 53 | + public function testEvalHIncrByFloatIfExists() |
| 54 | + { |
| 55 | + $container = ContainerStub::mockContainer(); |
| 56 | + $redis = $container->get(\Redis::class); |
| 57 | + $redis->hMSet('{hash}:1', ['id' => 1, 'name' => 'Hyperf', 'incr' => 0]); |
| 58 | + |
| 59 | + $script = new HIncrByFloatIfExists($container); |
| 60 | + $this->assertEquals(2.2, $script->eval(['{hash}:1', 'incr', 2.2])); |
| 61 | + |
| 62 | + $script = new HIncrByFloatIfExists($container); |
| 63 | + $this->assertSame(null, $script->eval(['{hash}:2', 'incr', 1])); |
| 64 | + } |
| 65 | +} |
0 commit comments