|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2014-Present Couchbase, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +use Couchbase\Utilities\ExpiryHelper; |
| 22 | +use Couchbase\Exception\InvalidArgumentException; |
| 23 | + |
| 24 | +include_once __DIR__ . "/Helpers/CouchbaseTestCase.php"; |
| 25 | + |
| 26 | +class ExpiryTest extends Helpers\CouchbaseTestCase |
| 27 | +{ |
| 28 | + public function testNullExpiryReturnsZero() |
| 29 | + { |
| 30 | + $this->assertEquals(0, ExpiryHelper::parseExpiry(null)); |
| 31 | + } |
| 32 | + |
| 33 | + public function testZeroExpiryReturnsZero() |
| 34 | + { |
| 35 | + $this->assertEquals(0, ExpiryHelper::parseExpiry(0)); |
| 36 | + $this->assertEquals(0, ExpiryHelper::parseExpiry('0')); |
| 37 | + } |
| 38 | + |
| 39 | + public function testNegativeExpiryThrows() |
| 40 | + { |
| 41 | + $this->expectException(InvalidArgumentException::class); |
| 42 | + ExpiryHelper::parseExpiry(-1); |
| 43 | + } |
| 44 | + |
| 45 | + public function testExpiryGreaterThanMaxThrows() |
| 46 | + { |
| 47 | + $this->expectException(InvalidArgumentException::class); |
| 48 | + ExpiryHelper::parseExpiry(4294967296); // MAX_EXPIRY + 1 |
| 49 | + } |
| 50 | + |
| 51 | + public function testRelativeExpiryUnderThirtyDays() |
| 52 | + { |
| 53 | + $expiry = 60; // 1 minute |
| 54 | + $result = ExpiryHelper::parseExpiry($expiry); |
| 55 | + $this->assertEquals($expiry, $result); |
| 56 | + } |
| 57 | + |
| 58 | + public function testRelativeExpiryOverThirtyDaysIsConverted() |
| 59 | + { |
| 60 | + $expiry = 2592000 + 1; // 30 days + 1 second |
| 61 | + $before = time(); |
| 62 | + $result = ExpiryHelper::parseExpiry($expiry); |
| 63 | + $after = time(); |
| 64 | + $this->assertGreaterThanOrEqual($before + $expiry, $result); |
| 65 | + $this->assertLessThanOrEqual($after + $expiry, $result); |
| 66 | + } |
| 67 | + |
| 68 | + public function testAbsoluteDateWithinRangeReturnsTimestamp() |
| 69 | + { |
| 70 | + $dt = new DateTimeImmutable('2025-01-01T00:00:00Z'); |
| 71 | + $result = ExpiryHelper::parseExpiry($dt); |
| 72 | + $this->assertEquals($dt->getTimestamp(), $result); |
| 73 | + } |
| 74 | + |
| 75 | + public function testAbsoluteDateBelowMinThrows() |
| 76 | + { |
| 77 | + $dt = new DateTimeImmutable('1969-12-31T23:59:59Z'); |
| 78 | + $this->expectException(InvalidArgumentException::class); |
| 79 | + ExpiryHelper::parseExpiry($dt); |
| 80 | + } |
| 81 | + |
| 82 | + public function testAbsoluteDateAboveMaxThrows() |
| 83 | + { |
| 84 | + $dt = new DateTimeImmutable('2200-01-01T00:00:00Z'); |
| 85 | + $this->expectException(InvalidArgumentException::class); |
| 86 | + ExpiryHelper::parseExpiry($dt); |
| 87 | + } |
| 88 | + |
| 89 | + public function testZeroSecondDateReturnsZero() |
| 90 | + { |
| 91 | + $dt = new DateTimeImmutable('1970-01-31T00:00:00Z'); |
| 92 | + $this->assertEquals(0, ExpiryHelper::parseExpiry($dt)); |
| 93 | + } |
| 94 | + |
| 95 | + public function testInvalidTypeThrows() |
| 96 | + { |
| 97 | + $this->expectException(InvalidArgumentException::class); |
| 98 | + ExpiryHelper::parseExpiry('foo'); |
| 99 | + } |
| 100 | +} |
0 commit comments