Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mock Cache Laravel Package

kodus/mock-cache

Drop-in mock cache for Laravel tests. Swap the real cache for a lightweight fake that records reads, writes, and forgets so you can assert keys, values, and TTLs without touching Redis, Memcached, or the filesystem. Keeps tests fast, deterministic, and free of flaky cache state.

Deep Wiki
Context7

kodus/mock-cache is a lightweight Laravel testing helper that swaps the framework's cache store for an in-memory mock, so your test suite never touches Redis, Memcached, or the filesystem. It intercepts cache calls as they happen and records them, letting you assert exactly what was stored, retrieved, or forgotten—keeping cache-dependent tests fast, deterministic, and free of flaky infrastructure.

  • Drop-in replacement for the Cache facade inside tests
  • Fluent assertions such as assertCached, assertNotCached, and assertForgotten
  • TTL and tag support for verifying expiration behaviour
  • Compatible with PHPUnit and Pest, with no external dependencies
Frequently asked questions about Mock Cache
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo