subject = new CacheItem(self::KEY); } public function testGetKey(): void { $this->assertSame(self::KEY, $this->subject->getKey()); } public function testGetAndSet(): void { $this->subject->set(self::EXAMPLE_VALUE); $this->assertSame(self::EXAMPLE_VALUE, $this->subject->get()); } public function testIsHit(): void { $subject = new CacheItem(self::KEY, true); $this->assertTrue($subject->isHit()); } public function testExpiresAtInvalidArgument(): void { $this->expectException(InvalidArgumentException::class); $this->subject->expiresAt('string'); } public function testExpireWithNull(): void { $this->subject->expiresAt(null); $this->assertNull( $this->getPrivateProperty(CacheItem::class, 'expiry')->getValue($this->subject) ); } public function testExpiresAt(): void { $expiry = new DateTime('tomorrow'); $this->subject->expiresAt($expiry); $this->assertSame( $expiry->getTimestamp(), $this->getPrivateProperty(CacheItem::class, 'expiry')->getValue($this->subject) ); } public function testExpiresAfter3MonthAhead(): void { $dt = new DateTime('now'); $dt->add(new DateInterval('P3M')); $expected = $dt->format('Y:m:d'); $this->subject->expiresAfter(new DateInterval('P3M')); $expiry = $this->getPrivateProperty(CacheItem::class, 'expiry')->getValue($this->subject); $resultDt = new DateTime(); $resultDt->setTimestamp($expiry); $this->assertSame($expected, $resultDt->format('Y:m:d')); } public function testExpiresAfterNumberOfSeconds(): void { $ttl = 300; $dt = new DateTime('now'); $dt->add(new DateInterval('PT300S')); $expected = $dt->getTimestamp(); $this->subject->expiresAfter($ttl); $expiry = $this->getPrivateProperty(CacheItem::class, 'expiry')->getValue($this->subject); $this->assertSame($expected, $expiry); } public function testExpiresAfterWithNull(): void { $this->subject->expiresAfter(null); $this->assertNull( $this->getPrivateProperty(CacheItem::class, 'expiry')->getValue($this->subject) ); } public function testExpiresAfterWithInvalidArgument(): void { $this->expectException(InvalidArgumentException::class); $this->subject->expiresAfter('string'); } public function getPrivateProperty(string $className, string $propertyName): ReflectionProperty { $reflector = new ReflectionClass($className); $property = $reflector->getProperty($propertyName); $property->setAccessible(true); return $property; } }