Download the PHP package jeckel-lab/clock without Composer
On this page you can find all versions of the php package jeckel-lab/clock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package clock
Clock
A clock abstraction library for PHP which allow to mock system clock when testing
Installation
Usage
In your code, always use the JeckelLab\Contract\Infrastructure\System\Clock
interface in your services when you need to access the current time. After that, you just have to define which implementation to use according to your environment (real or fake for tests).
In some framework, it can be easier to use the factory to handle the switch and inject the required configuration.
Different fake clock
There are 2 types of fake clock available:
frozen
: This clock with always return the same value everytime we call itfaked
: This one will return an incremented time (like a real one) but initiated at the beginning at the script with a defined date time. It's useful to continue to keep track of long process.
Fake clock can both be initiate in 2 different ways:
- by passing the value of the initial time (
fake_time_init
value in configuration), This option is useful when you want to always use the same time, or initialize it with an env variable - by passing the path to a file where it will read the value of time (
fake_time_path
). This option is useful when you want to control the time of each of your process when running different tests cases.
Use with Symfony 4 and 5
With SF4 and SF5 we use the internal DI system with the factory. The factory will get different parameters according to the current environment.
Configure DI with a factory in config/services.yaml
:
Configure default parameters in config/packages/parameters.yaml
:
And then configure parameters for tests environment in config/packages/test/parameters.yaml
:
or
Test with Codeception
To be able to change current date in your Codeception tests, you first need to configure your fake clock to use the fake_time_path
file as a time source.
Next, configure codeception with the provided helper:
Enable the helper in your suite:
Now you can set the fake time in your tests:
in BDD:
in other tests: