-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ebln/testing
Release 1.1.0 → Full coverage & usage of PSR-16 bulk methods
- Loading branch information
Showing
13 changed files
with
469 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brnc\tests\CachePsr16Adapter; | ||
|
||
use Brnc\CachePsr16Adapter\CacheItemPool; | ||
use Brnc\CachePsr16Adapter\Model\CacheItem; | ||
use Brnc\CachePsr16Adapter\NowFactory; | ||
use Brnc\tests\CachePsr16Adapter\helper\BrokenPsr16Mock; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @covers \Brnc\CachePsr16Adapter\CacheItemPool | ||
*/ | ||
final class BrokenPsr16Test extends TestCase | ||
{ | ||
public function testHasItem(): void | ||
{ | ||
$cache = $this->createCache(); | ||
static::assertFalse($cache->hasItem('foo')); | ||
} | ||
|
||
public function testClear(): void | ||
{ | ||
$cache = $this->createCache(); | ||
static::assertFalse($cache->clear()); | ||
} | ||
|
||
public function testCommit(): void | ||
{ | ||
$cache = $this->createCache(); | ||
$cache->saveDeferred($this->createItem()); | ||
static::assertFalse($cache->commit()); | ||
} | ||
|
||
public function testSave(): void | ||
{ | ||
$cache = $this->createCache(); | ||
static::assertFalse($cache->save($this->createItem())); | ||
} | ||
|
||
public function testDeleteItem(): void | ||
{ | ||
$cache = $this->createCache(); | ||
static::assertFalse($cache->deleteItem('foo')); | ||
} | ||
|
||
public function testDeleteItems(): void | ||
{ | ||
$cache = $this->createCache(); | ||
static::assertFalse($cache->deleteItems(['foo'])); | ||
} | ||
|
||
private function createItem(): CacheItem | ||
{ | ||
return new CacheItem('foo_' . bin2hex(random_bytes(2)), bin2hex(random_bytes(4)), true, null, new NowFactory()); | ||
} | ||
|
||
private function createCache(): CacheItemPool | ||
{ | ||
return new CacheItemPool(new BrokenPsr16Mock()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brnc\tests\CachePsr16Adapter; | ||
|
||
use Brnc\CachePsr16Adapter\CacheItemPool; | ||
use Brnc\CachePsr16Adapter\Exception\InvalidArgumentException; | ||
use Brnc\CachePsr16Adapter\Model\CacheItem; | ||
use Brnc\CachePsr16Adapter\NowFactory; | ||
use Brnc\tests\CachePsr16Adapter\helper\BrokenPsr16Mock; | ||
use Brnc\tests\CachePsr16Adapter\helper\Psr16Array; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Cache\CacheItemInterface; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
/** | ||
* @internal | ||
* @covers \Brnc\CachePsr16Adapter\CacheItemPool | ||
*/ | ||
final class CacheItemPoolTest extends TestCase | ||
{ | ||
public function testOnlyOwnItems(): void | ||
{ | ||
$cache = new CacheItemPool(new Psr16Array()); | ||
$item = $this->createStub(CacheItemInterface::class); | ||
$this->expectException(InvalidArgumentException::class); | ||
// ensure that exception is implementing the right interface for the PSR-6 | ||
// phpstan is quite strict: expects class-string<Throwable> yet PSR-6 doesn't implement throwable yet | ||
$implementing = class_implements(InvalidArgumentException::class); | ||
$implementing = $implementing ?: []; | ||
static::assertArrayHasKey(\Psr\Cache\InvalidArgumentException::class, $implementing); | ||
$cache->save($item); | ||
} | ||
|
||
public function testGetItemsWithFatalError(): void | ||
{ | ||
$pool = new CacheItemPool(new BrokenPsr16Mock()); | ||
static::assertSame([], $pool->getItems(['foo', 'bar'])); | ||
} | ||
|
||
public function testFailingCommit(): void | ||
{ | ||
$psr16 = $this->createStub(CacheInterface::class); | ||
$psr16->method('setMultiple')->willReturn(false); | ||
$pool = new CacheItemPool($psr16); | ||
$item = new CacheItem('foo', null, false, null, new NowFactory()); | ||
$pool->saveDeferred($item); | ||
static::assertFalse($pool->commit()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brnc\tests\CachePsr16Adapter; | ||
|
||
use Brnc\CachePsr16Adapter\Exception\InvalidArgumentException; | ||
use Brnc\CachePsr16Adapter\Model\CacheItem; | ||
use Brnc\CachePsr16Adapter\NowFactory; | ||
use DateInterval; | ||
use DateTime; | ||
use DateTimeImmutable; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @covers \Brnc\CachePsr16Adapter\Model\CacheItem | ||
*/ | ||
final class CacheItemTest extends TestCase | ||
{ | ||
public function testExpiresAtWithDateTime(): void | ||
{ | ||
$item = new CacheItem('foo', null, false, null, $this->getClock()); | ||
$dateTime = new DateTime(); | ||
$item->expiresAt($dateTime); | ||
|
||
static::assertEquals($dateTime, $item->getExpiry()); | ||
static::assertNotSame($dateTime, $item->getExpiry()); | ||
} | ||
|
||
public function testExpiresAtWithDateTimeImmutable(): void | ||
{ | ||
$item = new CacheItem('foo', null, false, null, $this->getClock()); | ||
$dateTime = new DateTimeImmutable(); | ||
$item->expiresAt($dateTime); | ||
static::assertSame($dateTime, $item->getExpiry()); | ||
} | ||
|
||
public function testExpiresAtWithBogus(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$implementing = class_implements(InvalidArgumentException::class); | ||
$implementing = $implementing ?: []; | ||
static::assertArrayHasKey(\Psr\Cache\InvalidArgumentException::class, $implementing); | ||
$item = new CacheItem('foo', null, false, null, $this->getClock()); | ||
$item->expiresAt((object)['foo']); | ||
} | ||
|
||
public function testExpiresAtWithNull(): void | ||
{ | ||
$item = new CacheItem('foo', null, false, null, $this->getClock()); | ||
$item->expiresAt(null); | ||
static::assertNull($item->getExpiry()); | ||
} | ||
|
||
public function testExpiresAfterWithNull(): void | ||
{ | ||
$item = new CacheItem('foo', null, false, null, $this->getClock()); | ||
$item->expiresAfter(null); | ||
static::assertNull($item->getExpiry()); | ||
} | ||
|
||
public function testExpiresAfterWithBogus(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$item = new CacheItem('foo', null, false, null, $this->getClock()); | ||
$item->expiresAfter('foobar'); | ||
} | ||
|
||
public function testExpiresAfterWithInt(): void | ||
{ | ||
$item = new CacheItem('foo', null, false, null, $this->getFixedClock()); | ||
$item->expiresAfter(60); | ||
static::assertEquals($this->getFixedClock()->now()->add(new DateInterval('PT60S')), $item->getExpiry()); | ||
} | ||
|
||
public function testExpiresAfterWithDateInterval(): void | ||
{ | ||
$item = new CacheItem('foo', null, false, null, $this->getFixedClock()); | ||
$interval = new DateInterval('PT60S'); | ||
$item->expiresAfter($interval); | ||
static::assertEquals($this->getFixedClock()->now()->add(new DateInterval('PT60S')), $item->getExpiry()); | ||
} | ||
|
||
private function getClock(): NowFactory | ||
{ | ||
return new NowFactory(); | ||
} | ||
|
||
private function getFixedClock(): NowFactory | ||
{ | ||
$stub = $this->createStub(NowFactory::class); | ||
$stub->method('now') | ||
->willReturn(new DateTimeImmutable('2037-12-31')) | ||
; | ||
|
||
return $stub; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.