Download the PHP package icanhazstring/symfony-time-machine without Composer
On this page you can find all versions of the php package icanhazstring/symfony-time-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download icanhazstring/symfony-time-machine
More information about icanhazstring/symfony-time-machine
Files in icanhazstring/symfony-time-machine
Package symfony-time-machine
Short Description Symfony bundle that adds the capability to change the datetime of an application
License MIT
Homepage https://github.com/icanhazstring/symfony-time-machine
Informations about the package symfony-time-machine
symfony-time-machine
Symfony bundle that adds the capability to change the datetime of an application
Motivation
Writing tests for your application is somewhat nice and easy. But what about
testing your application that heavily is based upon DateTime
objects?
There is symfony/clock
which already provides a ClockInterface
which you can
use to inject a NativeClock
or MockClock
into your services.
But that only works for tests. What if you want to test end-to-end?
You could change the system time of you server, true, but there is an easier way using
this symfony-time-machine
.
Installation
If the autoregister doesn't work, add the bundle into your config/bundles.php
.
How it works
This bundle relies on the presence of ClockInterface
as a service in your application.
Therefor you need to have something like this in your services.yaml
:
This bundle will set this service to public: true
as this is needed to change the
service on request.
Now everytime you boot the TimeKernel
, it will replace the ClockInterface
with a MockClock
with a parsed datetime string from either a request query/cookie time-machine
or environment
variable TIME_MACHINE
.
Usage
Web usage
For web usage you have to alter your public/index.php
a little.
Console usage
For console usage alter the bin/console
.
Register additional TimeMachineHandler
Sometimes you need to alter some other services in your application along with the time-machine.
For this you can implement services implementing the TimeMachineHandler
interface.
These will be called right after the ClockInterface
was changed in the container.
The TimeKernel
will use the HandlerRegistry
to call TimeMachineHandler::handle()
on all
available services.
For example, you could add the time-machine
query parameter to all openapi paths
in your application, as shown here using api-platform:
All versions of symfony-time-machine with dependencies
symfony/clock Version ^6.2.x-dev
symfony/dependency-injection Version ^5.4 || ^6.1
symfony/http-kernel Version ^5.4 || ^6.1