|
| 1 | +<?php namespace Tests\Integration; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use Illuminate\Support\Facades\Redis; |
| 16 | +use PHPUnit\Framework\Attributes\Group; |
| 17 | +use services\utils\RedisCacheService; |
| 18 | +use Tests\CreatesApplication; |
| 19 | +use Tests\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Integration tests for RedisCacheService::addSingleValue. |
| 23 | + * |
| 24 | + * These tests require a live Redis instance and verify two properties that |
| 25 | + * mocks cannot exercise: |
| 26 | + * |
| 27 | + * 1. Driver compatibility — the variadic SET NX EX form works with the |
| 28 | + * configured Predis/PhpRedis driver. If the driver is switched to |
| 29 | + * PhpRedis, set() returns false on an NX-miss (not null), which would |
| 30 | + * silently break the `!== null` check; this test catches that regression. |
| 31 | + * |
| 32 | + * 2. Atomicity — key and TTL are written in a single command; there is no |
| 33 | + * window where the key exists without a TTL. Verified by reading TTL |
| 34 | + * immediately after addSingleValue returns. |
| 35 | + * |
| 36 | + */ |
| 37 | +#[Group("integration")] |
| 38 | +final class RedisCacheServiceAddSingleValueTest extends TestCase |
| 39 | +{ |
| 40 | + use CreatesApplication; |
| 41 | + |
| 42 | + private const TEST_KEY = 'test:add_single_value:lock'; |
| 43 | + private const TTL = 30; |
| 44 | + |
| 45 | + private RedisCacheService $service; |
| 46 | + private mixed $redis; |
| 47 | + |
| 48 | + protected function setUp(): void |
| 49 | + { |
| 50 | + parent::setUp(); |
| 51 | + $this->redis = Redis::connection(); |
| 52 | + $this->service = new RedisCacheService(); |
| 53 | + // Start clean regardless of any leftover from a previous failed run. |
| 54 | + $this->redis->del(self::TEST_KEY); |
| 55 | + } |
| 56 | + |
| 57 | + protected function tearDown(): void |
| 58 | + { |
| 59 | + $this->redis->del(self::TEST_KEY); |
| 60 | + parent::tearDown(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * First call must succeed and leave a TTL on the key. |
| 65 | + * Second call on the same key must return false (NX semantics). |
| 66 | + */ |
| 67 | + public function testAddSingleValueSetsKeyWithTtlAndNxSemanticsHold(): void |
| 68 | + { |
| 69 | + $token = bin2hex(random_bytes(16)); |
| 70 | + |
| 71 | + $acquired = $this->service->addSingleValue(self::TEST_KEY, $token, self::TTL); |
| 72 | + $this->assertTrue($acquired, 'first addSingleValue must return true'); |
| 73 | + |
| 74 | + // Atomicity: TTL must already be set — no gap between key write and expire. |
| 75 | + $ttl = (int)$this->redis->ttl(self::TEST_KEY); |
| 76 | + $this->assertGreaterThanOrEqual(1, $ttl, 'key must have a positive TTL immediately after addSingleValue'); |
| 77 | + $this->assertLessThanOrEqual(self::TTL, $ttl, 'TTL must not exceed the requested lifetime'); |
| 78 | + |
| 79 | + // NX semantics: a second call while the key still exists must fail. |
| 80 | + $again = $this->service->addSingleValue(self::TEST_KEY, bin2hex(random_bytes(16)), self::TTL); |
| 81 | + $this->assertFalse($again, 'addSingleValue must return false when key already exists (NX)'); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * After the key is deleted the lock can be re-acquired, confirming the |
| 86 | + * return-value contract holds across both the true and false branches. |
| 87 | + */ |
| 88 | + public function testAddSingleValueReturnsTrueAfterKeyIsDeleted(): void |
| 89 | + { |
| 90 | + $token = bin2hex(random_bytes(16)); |
| 91 | + |
| 92 | + $this->assertTrue($this->service->addSingleValue(self::TEST_KEY, $token, self::TTL)); |
| 93 | + $this->redis->del(self::TEST_KEY); |
| 94 | + $this->assertTrue( |
| 95 | + $this->service->addSingleValue(self::TEST_KEY, bin2hex(random_bytes(16)), self::TTL), |
| 96 | + 'addSingleValue must return true once the key has been removed' |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments