Skip to content

Commit

Permalink
Close #1
Browse files Browse the repository at this point in the history
The methods set, expiresAt and expiresAfter of NullCacheItem must return the item itself.
  • Loading branch information
David Weichert committed Mar 12, 2024
1 parent 2181db8 commit afd879f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/NullCacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@ public function isHit()
*/
public function set($value)
{
return $this;
}

/**
* @inheritDoc
*/
public function expiresAt($expiration)
{
return $this;
}

/**
* @inheritDoc
*/
public function expiresAfter($time)
{
return $this;
}

}
71 changes: 71 additions & 0 deletions tests/NullCacheItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Metasyntactical\Psr\Cache;

use DateInterval;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Traversable;

class NullCacheItemTest extends TestCase
{
public function testGetKey(): void
{
$object = new NullCacheItem('key');
self::assertSame('key', $object->getKey());
}

public function testGet(): void
{
$object = new NullCacheItem('key');
self::assertEquals(null, $object->get());
}

public function testIsHit(): void
{
$object = new NullCacheItem('key');
self::assertFalse($object->isHit());
}

public function testSet(): void
{
$object = new NullCacheItem('key');
self::assertSame($object, $object->set('value'));
}

#[DataProvider('provideExpiresAt')]
public function testExpiresAt(
DateTimeInterface|null $value,
): void {
$object = new NullCacheItem('key');
self::assertSame($object, $object->expiresAt($value));
}

public static function provideExpiresAt(): Traversable
{
yield 'null' => ['value' => null];
yield 'DateTime' => ['value' => DateTime::createFromFormat('Y-m-d', '2024-03-12', new DateTimeZone('UTC'))];
yield 'DateTimeImmutable' => ['value' => DateTimeImmutable::createFromFormat('Y-m-d', '2024-03-12', new DateTimeZone('UTC'))];
}

#[DataProvider('provideExpiresAfter')]
public function testExpiresAfter(
DateInterval|int|null $value,
): void {
$object = new NullCacheItem('key');
self::assertSame($object, $object->expiresAfter($value));
}

public static function provideExpiresAfter(): Traversable
{
yield 'null' => ['value' => null];
yield 'int' => ['value' => 42];
yield 'DateIterval' => ['value' => DateInterval::createFromDateString('1 day')];
}
}

0 comments on commit afd879f

Please sign in to comment.