laminas/laminas-cache-storage-adapter-test
Test adapter for Laminas Cache storage. Provides a lightweight in-memory storage implementation useful for unit tests and CI, enabling predictable cache behavior without external services or persistent backends.
Installation:
composer require --dev laminas/laminas-cache-storage-adapter-test
Ensure this is added to your dev dependencies only.
First Use Case: Extend the provided abstract test classes in your custom cache adapter tests. For example:
use Laminas\Cache\Storage\Adapter\Test\AbstractCacheItemPoolIntegrationTest;
class MyRedisAdapterTest extends AbstractCacheItemPoolIntegrationTest
{
protected function createCacheItemPool(): CacheItemPoolInterface
{
return new MyRedisAdapter(); // Your custom adapter
}
}
Where to Look First:
AbstractCacheItemPoolIntegrationTestAbstractSimpleCacheIntegrationTestAbstractCacheItemIntegrationTestExtending Abstract Test Classes:
AbstractCacheItemPoolIntegrationTest for testing CacheItemPoolInterface implementations (e.g., Redis, Memcached).AbstractSimpleCacheIntegrationTest for testing Psr\SimpleCache\CacheInterface implementations.AbstractCacheItemIntegrationTest for testing individual CacheItemInterface behavior.Example:
class MyMemcachedAdapterTest extends AbstractCacheItemPoolIntegrationTest
{
protected function createCacheItemPool(): CacheItemPoolInterface
{
return new MemcachedAdapter(); // Your custom adapter
}
}
Integration Testing:
testHasItemReturnsFalseWhenDeferredItemIsExpired to customize behavior.PSR Compliance:
PSR-6 (CacheItemPoolInterface) and PSR-16 (SimpleCacheInterface) are included.AbstractSimpleCacheIntegrationTest for PSR-16 compliance:
class MySimpleCacheTest extends AbstractSimpleCacheIntegrationTest
{
protected function createCache(): CacheInterface
{
return new MySimpleCacheAdapter(); // Your PSR-16 adapter
}
}
Type Safety:
Test-Driven Development (TDD):
AbstractCacheItemPoolIntegrationTest.createCacheItemPool() to return your adapter.Regression Testing:
class MyAdapterRegressionTest extends AbstractCacheItemIntegrationTest
{
protected function createCacheItem(): CacheItemInterface
{
return $this->createCacheItemPool()->getItem('test-key');
}
}
Custom Test Cases:
public function testCustomAdapterBehavior()
{
$this->assertTrue($this->createCacheItemPool()->getItem('test')->isHit());
// Custom assertion for your adapter
}
Leverage Sleep Timer:
sleepTimer feature (introduced in v4.1.0) to speed up tests with expiry times:
$this->sleepTimer = 0.1; // Reduce sleep time for faster tests
PSR-6/PSR-16 Dual Support:
Mocking Dependencies:
use PHPUnit\Framework\TestCase;
class MyAdapterTest extends TestCase
{
public function testAdapterWithMockedClient()
{
$mockClient = $this->createMock(Redis::class);
$adapter = new MyRedisAdapter($mockClient);
// Test assertions
}
}
CI/CD Integration:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/phpunit --testdox-html coverage.html
Non-Static Data Providers:
public static function dataProviderForTest(): array
{
return [
['key1', 'value1'],
['key2', 'value2'],
];
}
Cache Pool Reference Issues:
protected function createCacheItemPool(): CacheItemPoolInterface
{
return new MyAdapter(); // New instance per test
}
Key Validation:
public function getItem($key)
{
if (strlen($key) > 255) {
throw new InvalidArgumentException('Key too long');
}
// ...
}
Expiry Timeouts:
sleepTimer property to optimize:
protected $sleepTimer = 0.01; // 10ms sleep for expiry tests
PHPUnit Version Mismatch:
composer require --dev phpunit/phpunit:^9.5
Failed Assertions:
public function testGetItem()
{
$item = $this->createCacheItemPool()->getItem('test');
$this->assertInstanceOf(CacheItemInterface::class, $item);
$this->assertFalse($item->isHit()); // Debug: Is this expected?
}
Slow Tests:
sleepTimer property to reduce wait times:
protected $sleepTimer = 0.1; // 100ms sleep for expiry checks
Type Errors:
CacheItemInterface, bool, null). Use PHP 8's strict typing or runtime checks:
public function getItem($key): CacheItemInterface
{
return new MyCacheItem($key); // Must return CacheItemInterface
}
Adapter-Specific Config:
class MyRedisAdapter implements CacheItemPoolInterface
{
public function __construct(private string $host, private int $port) {}
// ...
}
Test Isolation:
protected function tearDown(): void
{
$this->createCacheItemPool()->clear();
parent::tearDown();
}
class MyAdapterTest extends AbstractCache
How can I help you explore Laravel packages today?