2017 © Pedro Peláez
 

library mockable-datetime

A DateTime library that allows mock dates and times in UnitTests.

image

mcustiel/mockable-datetime

A DateTime library that allows mock dates and times in UnitTests.

  • Tuesday, June 6, 2017
  • by mcustiel
  • Repository
  • 1 Watchers
  • 2 Stars
  • 27,600 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 86 % Grown

The README.md

Mockable DateTime

What is it

Mockable DateTime is a library written in PHP that allows developers to mock the dates for unit tests. There are sometimes in which you need to verify that an action was executed with certain parameters, and one of them is a date or a time (generally obtained with date() or time() built-in functions) and is very difficult to ensure it will have a certain value at the time of the verification. Mockable DateTime solves this problem by giving the developer a way to obtain PHP's built-in DateTime class in a way, that the value it returns can be mocked from unit tests without the need of injecting DateTime as a dependency., (*1)

Build Status Scrutinizer Code Quality Code Coverage, (*2)

Installation

Composer:

{
    "require": {
        "mcustiel/mockable-datetime": "^2.0"
    }
}

How to use it?

In your code

Everytime you need to get system's date just use Mockable DateTime:, (*3)


use Mcustiel\Mockable\DateTime; // ... function savePersonInfoInDatabase(Person $person) { /** @var PersonDbEntity $person */ $person = $this->converter->convert($person, PersonDbEntity::class); $person->setCreatedAt(DateTime::newPhpDateTime()); // DateTime::newImmutablePhpDateTime() can also be used $this->dbClient->insert($person); } // ...

Also arguments can be passed to create the DateTime object:, (*4)


use Mcustiel\Mockable\DateTime; // ... function savePersonInfoInDatabase(Person $person) { /** @var PersonDbEntity $person */ $person = $this->converter->convert($person, PersonDbEntity::class); $person->setCreatedAt(DateTime::newPhpDateTime( '-4 months', new \DateTimeZone('America/New_York') )); // DateTime::newImmutablePhpDateTime() can also be used $this->dbClient->insert($person); } // ...

As you can see in the example, I'm not using PHP's \DateTime directly. Instead I use Mockable DateTime to create instances of PHP's \DateTime., (*5)

Then you have to test this, and assert that insert method was called with some specific date as an argument. For the example I'll use PHPUnit., (*6)

use Mcustiel\Mockable\DateTime;

// ...

/**
 * @test
 */
function shouldCallInsertPersonWithCorrectData()
{
    DateTime::setFixed(new \DateTime('2000-01-01 00:00:01'));
    // Now every call to MockableDateTime::newPhpDateTime() will always return "2000-01-01 00:00:01"
    /** @var Person $person */
    $person = new Person('John', 'Doe');
    /** @var PersonDbEntity $expected */
    $expected = new PersonDbEntity('John', 'Doe');
    $expected->setCreatedAt(new \DateTime('2000-01-01 00:00:01'));    

    $this->dbClientMock->expects($this->once())
        ->method('insert')
        ->with($this->equalTo($expected));
    // ...and other needed mocks
    $this->unitUnderTest->savePersonInfoInDatabase($person);
}
// ...

That's it. For it to work the code and tests should be executed in the same environment (it won't work if you execute tests again a running instance of your webservice), but it should be enough for unit and some low level functional tests., (*7)

DateTime methods:

void setFixed(\DateTime $dateTime)

This method makes all instances of Mockable DateTime to always return the date and time specified by the $dateTime parameter., (*8)

void setOffset(\DateTime $dateTime)

This method makes all instances of Mockable DateTime to always return a date and time with offset equal to the specified by the $dateTime parameter. The time starts to run from the moment of the call to this method. So if you set an offset equal to '2005-05-05 01:00:00' and sleep for 5 seconds, a new call to create a \DateTime will return one with '2005-05-05 01:00:05' set., (*9)

void setSystem()

This method makes all instances of Mockable DateTime to always return current system time (default behaviour)., (*10)

\DateTime newPhpDateTime($time = 'now', \DateTimeZone $timeZone = null)

Creates a new instance of PHP's \DateTime class based in the config of Mockable DateTime., (*11)

\DateTimeImmutable newImmutablePhpDateTime($time = 'now', \DateTimeZone $timeZone = null)

Creates a new instance of PHP's \DateTimeImmutable class based in the config of Mockable DateTime., (*12)

The Versions

06/06 2017

dev-master

9999999-dev

A DateTime library that allows mock dates and times in UnitTests.

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

library date time tests mocks

25/05 2015

1.0.0

1.0.0.0

A DateTime library that allows mock dates and times in UnitTests.

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

library date time tests mocks