php-mock/php-mock-prophecy
Prophecy integration for php-mock lets you mock PHP built-in functions like time, rand, and sleep inside Prophecy test doubles. Define expected behavior for global functions alongside class mocks, making time-dependent code testable without hard dependencies.
time(), date(), mktime() and friends to deterministic values, so we can ship date-sensitive logic with trustworthy automated coverage instead of manual QA passes.rand(), mt_rand(), random_int() and uniqid() — turning "sometimes fails" tests into reproducible cases.Clock/Randomizer interface. This package is the pragmatic alternative when a codebase calls time() directly in hundreds of places: it buys test coverage now and defers the DI refactor, letting us sequence that work on the roadmap rather than blocking a release on it.TestClock utility plus a slow migration to injectable time. The decision hinges on how much legacy code we intend to keep.Adopt when most of the following hold:
php-mock/php-mock-phpunit. If we standardise on PHPUnit, this specific package is the wrong choice even though the underlying php-mock/php-mock capability is right.time(), date(), rand(), file_get_contents(), sys_get_temp_dir(), header()), and refactoring to injected dependencies is not on the near-term roadmap.\time() calls or global-namespace code will not be intercepted. Verify this before committing — it is the most common reason a rollout stalls.ClockInterface is injected in the billing and auth modules").Look elsewhere when:
Clock/Randomizer abstraction from day one; it is cheaper, framework-agnostic and survives a test-framework change.random_int() proves nothing about entropy in production; keep those assertions at the integration level.To executives (one paragraph): A small, free, development-only add-on that lets our automated tests control time and randomness inside PHP code. In practice it removes a class of intermittent CI failures that currently cost engineering hours and occasionally let date-related bugs — trial expiry, renewals, rate limits — reach customers. It ships nothing to production, adds no runtime dependency, and requires no refactor to adopt, so it is a cheap, reversible way to raise release confidence on our time-sensitive revenue features while a longer-term cleanup is scheduled.
To engineering leadership: This is the Prophecy adapter from the php-mock family. It intercepts namespaced calls to built-ins so specs can return a fixed time() or rand(), eliminating sleep-based tests and flaky clock assertions in legacy code paths. Cost: one dev dependency, a bootstrap hook, and per-test overhead; the constraint is that it only intercepts unqualified built-in calls in namespaced code and only fits a Prophecy-based suite. Benefit: immediate, low-effort coverage of TTL, scheduling and expiry logic that is otherwise untestable without invasive changes.
To engineers: Use it as a scalpel, not a foundation. Mock exactly the built-ins a spec needs, keep the mocks local to the spec, and avoid broad global stubbing that hides real behaviour. Every time you reach for it in new code, ask whether a Clock or Randomizer dependency would be the better seam — and log that as tech debt if the answer is yes. Do not use it to "prove" security properties of randomness, and check the calling-namespace requirement first so we do not burn a sprint discovering interception does not apply.
How can I help you explore Laravel packages today?