gsokol/date-time-provider-bundle

Symfony bundle for `gsokol/date-time-provider` library

Installs: 170

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.0.0 2017-09-08 15:54 UTC

This package is not auto-updated.

Last update: 2024-04-23 15:53:09 UTC


README

Build Status

Symfony bundle for gsokol/date-time-provider lib.

Install

Install via composer:

composer require gsokol/date-time-provider-bundle

And in app/AppKernel.php:

// app/AppKernel.php

// ...
class AppKernel Extends Kernel
{
    // ...
    public function registerBundles()
    {
        $bundles = [
            // ...
            new GSokol\DateTimeProviderBundle\DateTimePorviderBundle(),
            // ...
        ];
        // ...

Usage

Greedy request DateTime:

doSomeStaff();
$timeOfRequestProcessionStart = $container->get('date-time-provider.greedy')->getRequestTime();

Lazy request DateTime:

someLognOperation();
$currentTime = $container->get('date-time-provider.lazy')->getRequestTime();

Intervaling:

$tomorrow = $container->get('date-time-provider.lazy')->getNewRequestTime()
    ->add(new \DateInterval('P1d'));

Benefits

Lat's think, you have a class method, that should return the next hour DateTime. Something like this:

class NextHour
{
    public function get()
    {
        return (new \DateTime())
            ->add(new \DateInterval('PT1h'));
    }
}

And now we need to cover it with unit tests. Oops, :hankey:!

But if you inject providers:

// class

use GSokol\DateTimeProvider\DateTimeProviderInterface;

class NextHour
{
    /**
     * @var DateTimeProviderInterface
     */
    private $dtProvider;

    public function __construct(DateTimeProviderInterface $dtProvider)
    {
        $this->dtProvider = $dtProvider;
    }

    public function get()
    {
        return $this->dtProvider->getCurrentTime()
            ->add(new \DateInterval('PT1h'));
    }
}
// test

use GSokol\DateTimeProvider\DateTimeProviderInterface;
use PHPUnit\Framework\TestCase;

class NextHourTest extends TestCase
{
    public function testGet()
    {
        $dtStub = (new \DateTime())->setTimestamp(0);
        $dtExp = (new \DateTime())->setTimestamp(3600);
        $dtProvider = $this->getMockBuilder(DateTimeProviderInterface)
            ->disableOriginalConstructor()
            ->setMethods(['getCurrentTime'])
            ->getMock();
        $dtProvider->expects($this->once())
            ->method('getCurrentTime')
            ->will($this->returnValue($dtStub));

        $target = new NextHour($dtProvider);

        $this->assertEquals($dtExp, $target->get());
    }
}

:+1: