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
Add to your composer.json under require-dev to ensure it’s only installed in testing environments.
First Use Case:
Extend the provided abstract test classes to test your custom cache adapters. For example, for a PSR-16 (SimpleCache) adapter:
use Laminas\Cache\Storage\Adapter\Test\AbstractSimpleCacheIntegrationTest;
class MyRedisCacheTest extends AbstractSimpleCacheIntegrationTest
{
protected function createCachePool(): \Psr\SimpleCache\CacheInterface
{
return new MyRedisCacheAdapter();
}
}
Where to Look First:
AbstractCacheItemPoolIntegrationTest (for PSR-6 adapters)AbstractSimpleCacheIntegrationTest (for PSR-16 adapters)Extending Abstract Test Classes:
Override createCachePool() to return an instance of your custom cache adapter. This is the core pattern for leveraging the package.
class MyFileCacheTest extends AbstractCacheItemPoolIntegrationTest
{
protected function createCachePool(): \Psr\Cache\CacheItemPoolInterface
{
return new \Laminas\Cache\Storage\Adapter\Filesystem();
}
}
Testing Edge Cases: Use built-in test methods to verify:
testMaximumKeyLength).testHasItemReturnsFalseWhenDeferredItemIsExpired).DateTime, arrays).public function testCustomEdgeCase()
{
$this->assertTrue($this->cachePool->hasItem('key'));
$this->assertEquals('value', $this->cachePool->getItem('key')->get());
}
Integration with Laravel:
FileCache, RedisCache).Cache facade:
use Illuminate\Support\Facades\Cache;
use Laminas\Cache\Storage\Adapter\Test\AbstractSimpleCacheIntegrationTest;
class LaravelRedisCacheTest extends AbstractSimpleCacheIntegrationTest
{
protected function createCachePool(): \Psr\SimpleCache\CacheInterface
{
return Cache::store('redis');
}
}
Data Providers:
Leverage static data providers (e.g., dataProviderForGetItem) for parameterized tests:
/**
* @dataProvider dataProviderForGetItem
*/
public function testGetItem($key, $expectedValue, $expectedMetadata = null)
{
$this->cachePool->set($key, $expectedValue, $expectedMetadata);
$this->assertEquals($expectedValue, $this->cachePool->get($key));
}
PSR Compliance:
Test both PSR-6 (CacheItemPoolInterface) and PSR-16 (CacheInterface) adapters by extending the appropriate abstract class. The package ensures compatibility with PSR versions 2 and 3.
Test-Driven Development (TDD):
# 1. Add the package to dev dependencies.
composer require --dev laminas/laminas-cache-storage-adapter-test
# 2. Create a test class extending the abstract test.
php artisan make:test MyCacheAdapterTest
# 3. Implement the adapter to pass tests.
Regression Testing:
// Test suite for all cache adapters in a Laravel app.
class AllCacheAdaptersTest extends \Tests\TestCase
{
public function testFileCache()
{
(new MyFileCacheTest())->testHasItemReturnsFalseWhenItemIsMissing();
}
public function testRedisCache()
{
(new MyRedisCacheTest())->testGetItem();
}
}
CI/CD Integration:
.github/workflows/ci.yml:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: composer test # Runs PHPUnit with the test suite.
Laravel-Specific:
Cache facade or use the real store in tests:
use Illuminate\Foundation\Testing\RefreshDatabase;
class LaravelCacheTest extends AbstractSimpleCacheIntegrationTest
{
use RefreshDatabase;
protected function createCachePool(): \Psr\SimpleCache\CacheInterface
{
return Cache::store('database'); // Uses Laravel's database driver.
}
}
Custom Adapters:
class MyTaggedCacheTest extends AbstractCacheItemPoolIntegrationTest
{
public function testTagOperations()
{
$this->cachePool->set('key', 'value', ['tag1', 'tag2']);
$this->assertEquals(['value'], $this->cachePool->getItemsByTag('tag1'));
}
}
Performance Testing:
sleepTimer feature (introduced in v4.1.0) to speed up tests with long expiry times:
protected function setUp(): void
{
parent::setUp();
$this->sleepTimer = 0.1; // Reduce sleep time for expiry tests.
}
Type Safety:
testInvalidKeys).PHPUnit Version Compatibility:
phpunit.xml config matches:
<phpunit bootstrap="vendor/autoload.php">
<extensions>
<extension class="Laminas\Cache\Storage\Adapter\Test\PHPUnit\Extensions\SleepTimerExtension"/>
</extensions>
</phpunit>
DataProvider errors (e.g., non-static methods).Cache Pool Isolation:
flush() or clear() in tearDown() if needed:
protected function tearDown(): void
{
$this->cachePool->clear();
parent::tearDown();
}
Expiry Timing:
set($key, $value, 1)) may fail if the test runner is slow. Use the sleepTimer to adjust:
// In your test class:
protected $sleepTimer = 0.01; // 10ms sleep per second of expiry time.
Key Validation:
// Example of a failing test:
$this->expectException(\Psr\Cache\InvalidArgumentException::class);
$this->cachePool->set(str_repeat('a', 300), 'value');
PSR Version Mismatches:
AbstractSimpleCacheIntegrationTest for a PSR-6 adapter.AbstractCacheItemPoolIntegrationTest for PSR-6.Serialization Issues:
DateTime, Resource) may not serialize/deserialize correctly. Test with:
$date = new \DateTime();
$this->cachePool->set('date', $date);
$this->assertInstanceOf(\DateTime::class, $this->cachePool->get('
How can I help you explore Laravel packages today?