- What is kodus/mock-cache used for?
- kodus/mock-cache is a lightweight Laravel package that provides a PSR-16 mock cache for integration testing. It lets you swap the real cache backend for a fake store so you can assert reads, writes, and cache hits during tests. The result is faster, more deterministic test suites that never touch Redis, Memcached, or a database cache table.
- How do I install kodus/mock-cache in a Laravel application?
- Install it with Composer using composer require kodus/mock-cache --dev, since it is a testing-only dependency. The service provider is auto-discovered in Laravel, so no manual registration is normally required. Check the package README for any optional publishing or configuration steps.
- How do I swap in the fake cache store during a test?
- The package is designed so you can bind or set its fake PSR-16 store as the cache implementation for the duration of a test. Typical usage is to configure it in setUp or a test helper before exercising the code under test. After the test, Laravel's normal test isolation restores the original cache binding.
- Does kodus/mock-cache implement PSR-16 Simple Cache?
- Yes, the package is described as a PSR-16 mock cache, meaning it implements the Psr\SimpleCache\CacheInterface methods such as get, set, has, and delete. That makes it usable anywhere your code type-hints the PSR-16 interface rather than a concrete Laravel cache driver. It also fits Laravel's cache manager when a PSR-16 compatible store is expected.
- Can I assert cache reads, writes, and cache hits with this package?
- Yes, the main purpose of the fake store is to record interactions so you can assert that values were written, read back, or served as hits. This lets you verify caching behavior in integration tests without inspecting a real backend. Assertions are made against the fake store's recorded state rather than real cache infrastructure.
- Which Laravel and PHP versions does kodus/mock-cache support?
- The package targets Laravel applications and PSR-16 compatible code, but exact version constraints are defined in its composer.json. Check the package page on Packagist or the repository's composer.json for the supported PHP and illuminate/support ranges before adding it to an older or newer project. Pinning to a compatible release tag avoids surprises during upgrades.
- Does kodus/mock-cache hit Redis, Memcached, or the database?
- No, the fake store keeps everything in memory for the lifetime of the test, so no external cache backend is contacted. This removes network latency and backend flakiness from your integration tests. It also means tests can run in CI environments where Redis or Memcached are not available.
- Can I use kodus/mock-cache outside of a full Laravel boot?
- Because it is a PSR-16 implementation, it can be used in any PHP test that depends on the Simple Cache interface, including plain PHPUnit tests. In Laravel projects you get the extra convenience of swapping it through the container or cache manager. For framework-agnostic packages, you can inject the mock directly.
- How does a mock cache make tests faster and more deterministic?
- A real cache introduces shared state between tests, so a value left behind by one test can change the outcome of another. The mock store starts clean and lives only for the test, which keeps assertions predictable. It also avoids the overhead of connecting to and flushing an external cache service on every run.
- What alternatives exist to kodus/mock-cache?
- Common alternatives include Laravel's array cache driver, spying or mocking the Cache facade with Mockery, and writing your own small PSR-16 fake class. The array store is simple but does not record interactions for assertions, while facade mocks can become verbose for integration-style tests. kodus/mock-cache sits in between by offering a real PSR-16 implementation you can inspect.