acts/time-mock-bundle
Symfony bundle for mocking and controlling time in tests and development. Freeze, fast-forward, and reset “now” to make time-dependent code deterministic, simplify assertions, and avoid flaky tests across DateTime/Clock usage.
acts/time-mock-bundle is a time-mocking utility tailored for Symfony2 applications, enabling deterministic testing by replacing dynamic time calls (e.g., time(), DateTime, Carbon) with mock values. This is particularly valuable for:
Carbon and now() helpers already support mocking via dependency injection or testing utilities (e.g., travel() in Laravel’s Testing facade). This bundle’s value in Laravel is marginal unless it offers unique features (e.g., global time overrides, Symfony-specific integrations like Swiftmailer timestamps).time(): Replace PHP’s native time() with a static value (e.g., 2023-01-01).DateTime/Carbon: Intercept instantiations to return predefined dates.clock).Mockery or PHPUnit’s getMockBuilder can replicate time mocking without a bundle.Testing facade provides travel() for Carbon mocking:
use Illuminate\Support\Facades\Time;
Time::travel($date);
HttpKernel, DependencyInjection, and possibly EventDispatcher. Laravel’s architecture differs significantly, requiring shims or wrappers for integration.| Risk Area | Assessment |
|---|---|
| Architecture Mismatch | High. Symfony2’s event-driven, service-container-heavy architecture contrasts with Laravel’s lighter, facade-based design. Direct integration would require significant refactoring or a proxy layer. |
| Maintenance Overhead | Medium. If adopted, the bundle would need ongoing adaptation to Laravel’s evolving APIs (e.g., Carbon updates, testing utilities). |
| Testing Coverage | Low. The bundle’s lack of stars/dependents suggests unproven reliability. Testing edge cases (e.g., timezone handling, nested mocks) would be critical. |
| Performance Impact | Low. Time mocking is typically lightweight, but global overrides (e.g., replacing time()) could introduce subtle bugs if not scoped properly. |
| Alternatives | High. Laravel’s native tools (travel(), Mockery, partialMock) already solve 90% of use cases. The bundle offers no clear advantage unless targeting Symfony-specific integrations. |
Why Laravel?
Swiftmailer, Doctrine) that necessitate this bundle’s time-mocking behavior?Feature Gaps
Carbon handles this natively.strtotime, DateTimeImmutable) without conflicts?Integration Strategy
laravel-time-mock) be preferable to avoid Symfony dependencies?php-time-mock) for broader use?Testing Scope
Long-Term Viability
HttpKernel.Illuminate\Container, not Symfony’s DependencyInjection.travel() and Mockery reduce need for external bundles.Testing facade.ClockInterface in shared libraries and mock it via Laravel’s DI.| Scenario | Approach |
|---|---|
| Greenfield Laravel | Avoid. Use Laravel’s native Time::travel() or Mockery for time mocking. |
| Symfony2 → Laravel | Phase Out: Replace Symfony-specific time usages with Laravel equivalents before adopting this bundle. |
| Hybrid App | Isolate: Containerize Symfony2 services and mock time at the API boundary (e.g., using Guzzle to call a Symfony2 mock endpoint with frozen time). |
| Legacy Codebase | Wrapper Layer: Create a Laravel service provider that proxies the bundle’s logic, translating Symfony events/services to Laravel equivalents. High effort; evaluate ROI. |
nesbot/carbon vs. Symfony’s symfony/polyfill-php80).Bundle. In Laravel, this would require:
// Example pseudo-integration (hypothetical)
$app->bind('time_mocker', function ($app) {
return new TimeMockBundle(); // Would need significant adaptation
});
EventDispatcher, HttpKernel) could break with Laravel updates.replace to avoid conflicts.DateTime::createFromFormat).Time facade.now() values in async jobs) may be harder to trace than native Laravel tools.How can I help you explore Laravel packages today?