Download the PHP package lendable/clock without Composer
On this page you can find all versions of the php package lendable/clock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package clock
Lendable Clock
The Lendable Clock library provides an object-oriented interface for accessing the system time in PHP. While PHP offers direct instantiation of \DateTime
, and \DateTimeImmutable
to obtain the current system time, this library introduces the concept of a Clock to offer greater control and flexibility over time-related operations.
Why Use a Clock?
You might wonder why you need a clock when you can simply instantiate \DateTime
objects whenever you need them. Here's why a Clock abstraction is beneficial:
-
Control Over Time: By depending on a Clock rather than instantiating time objects directly, you gain the ability to reason about and control time within your application.
-
Testing Flexibility: Using a Clock allows you to swap underlying implementations, making it easier to test time-dependent code. You can stub time with fixed values, simulate time passing, and observe interactions with the Clock for more robust testing.
-
Dependency Management: Clear dependencies on the Clock class help in managing components that rely on accessing the current system time.
- PSR-20 Compatibility: The library aligns with PSR-20, offering interoperability with other libraries and frameworks.
Installation
You can install the Lendable Clock library via Composer.
Clock Types
The library provides several types of Clocks to suit different use cases:
SystemClock
- Target: Runtime
- Description: Delegates to PHP for the current system time, using a fixed timezone at construction.
FixedClock
- Target: Unit/Functional Tests
- Description: Always provides a specific timestamp provided at construction, facilitating deterministic testing.
TickingMockClock
- Target: Unit/Functional Tests
- Description: Mocks time starting from a given timestamp and simulates time progressing from that point. Useful for testing time-dependent functionality.
PersistedFixedClock
- Target: Functional Tests (e.g., Behat vs. Symfony Kernel)
- Description: Similar to
FixedClock
, but can persist and load the given timestamp from disk. Ideal for scenarios where you need to reload your context during testing.
Use PersistedFixedClock::initializeWith(...)
to set up the timestamp and PersistedFixedClock::fromPersisted(...)
to load from the persisted value on disk.
By leveraging these Clock types, you can enhance the reliability, testability, and maintainability of your time-dependent PHP applications.