Skip to content

Commit

Permalink
Added support for DateTimeImmutable by checking for DateTimeInterface…
Browse files Browse the repository at this point in the history
… implementations for the Holiday Class.

Signed-off-by: Sacha Telgenhof <[email protected]>
  • Loading branch information
stelgenhof committed Jan 24, 2018
1 parent 090010b commit fce31b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
26 changes: 14 additions & 12 deletions src/Yasumi/Holiday.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DateTime;
use InvalidArgumentException;
use JsonSerializable;
use Yasumi\Exception\InvalidDateException;
use Yasumi\Exception\UnknownLocaleException;

/**
Expand Down Expand Up @@ -83,23 +84,24 @@ class Holiday extends DateTime implements JsonSerializable
* If a holiday date needs to be defined for a specific timezone, make sure that the date instance (DateTime) has
* the correct timezone set. Otherwise the default system timezone is used.
*
* @param string $shortName The short name (internal name) of this holiday
* @param array $names An array containing the name/description of this holiday in various
* languages. Overrides global translations
* @param DateTime $date A DateTime instance representing the date of the holiday
* @param string $displayLocale Locale (i.e. language) in which the holiday information needs to be
* displayed in. (Default 'en_US')
* @param string $type The type of holiday. Use the following constants: TYPE_OFFICIAL,
* TYPE_OBSERVANCE, TYPE_SEASON, TYPE_BANK or TYPE_OTHER. By default an
* official holiday is considered.
* @param string $shortName The short name (internal name) of this holiday
* @param array $names An array containing the name/description of this holiday in various
* languages. Overrides global translations
* @param \DateTimeInterface $date A DateTime instance representing the date of the holiday
* @param string $displayLocale Locale (i.e. language) in which the holiday information needs to be
* displayed in. (Default 'en_US')
* @param string $type The type of holiday. Use the following constants: TYPE_OFFICIAL,
* TYPE_OBSERVANCE, TYPE_SEASON, TYPE_BANK or TYPE_OTHER. By default an
* official holiday is considered.
*
* @throws \Yasumi\Exception\InvalidDateException
* @throws UnknownLocaleException
* @throws \InvalidArgumentException
*/
public function __construct(
$shortName,
array $names,
$date,
\DateTimeInterface $date,
$displayLocale = self::DEFAULT_LOCALE,
$type = self::TYPE_OFFICIAL
) {
Expand All @@ -109,8 +111,8 @@ public function __construct(
}

// Validate if date parameter is instance of DateTime
if (! ($date instanceof DateTime)) {
throw new InvalidArgumentException(sprintf('Date "%s" is not a valid DateTime instance.', $date));
if (! ($date instanceof \DateTimeInterface)) {
throw new InvalidDateException($date);
}

// Load internal locales variable
Expand Down
29 changes: 18 additions & 11 deletions tests/Base/HolidayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,7 @@ class HolidayTest extends PHPUnit_Framework_TestCase
*/
public function testHolidayBlankNameInvalidArgumentException()
{
new Holiday('', [], '2015-01-01');
}

/**
* Tests that an InvalidArgumentException is thrown in case an invalid type for date is given.
*
* @expectedException InvalidArgumentException
*/
public function testHolidayInvalidDateTypeInvalidArgumentException()
{
new Holiday('testHoliday', [], '2015-01-01');
new Holiday('', [], new \DateTime());
}

/**
Expand All @@ -72,6 +62,23 @@ public function testHolidayIsJsonSerializable()
$this->assertArrayHasKey('shortName', $instance);
}

/**
* Tests that a Yasumi holiday instance can be created using an object that implements the DateTimeInterface (e.g.
* DateTime or DateTimeImmutable)
*/
public function testHolidayWithDateTimeInterface()
{
// Assert with DateTime instance
$holiday = new Holiday('testHoliday', [], new \DateTime(), 'en_US');
$this->assertNotNull($holiday);
$this->assertInstanceOf(Holiday::class, $holiday);

// Assert with DateTimeImmutable instance
$holiday = new Holiday('testHoliday', [], new \DateTimeImmutable(), 'en_US');
$this->assertNotNull($holiday);
$this->assertInstanceOf(Holiday::class, $holiday);
}

/**
* Tests the getName function of the Holiday object with no translations for the name given.
*/
Expand Down

0 comments on commit fce31b3

Please sign in to comment.