Skip to content

Instantly share code, notes, and snippets.

@event15
Created March 14, 2017 20:54
Show Gist options
  • Save event15/c020f5c7481530e348876744c3b63d82 to your computer and use it in GitHub Desktop.
Save event15/c020f5c7481530e348876744c3b63d82 to your computer and use it in GitHub Desktop.
<?php
namespace WP;
use DateTime;
interface DateFormatterInterface
{
public function set(DateTime $newDate) : DateTime;
public function transformTo(FormatInterface $newFormat) : string;
}
interface FormatInterface
{
public function convert(DateTime $dateToConvert) : string;
}
class PolishDateFormat extends \IntlDateFormatter implements FormatInterface
{
public function __construct(
$locale = 'pl_PL',
$datetype = \IntlDateFormatter::LONG,
$timetype = \IntlDateFormatter::NONE,
$timezone = 'Europe/Warsaw',
$calendar = \IntlDateFormatter::GREGORIAN,
$pattern = null
) {
parent::__construct($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
}
public function convert(DateTime $dateToConvert) : string
{
return $this->format($dateToConvert);
}
}
class DateFormatter implements DateFormatterInterface
{
private $date;
public function set(DateTime $newDate) : DateTime
{
return $this->date = $newDate;
}
public function transformTo(FormatInterface $newFormat) : string
{
return $newFormat->convert($this->date);
}
}
<?php
namespace spec\WP;
use WP\{
DateFormatter,
PolishDateFormat
};
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use DateTime;
class DateFormatterSpec extends ObjectBehavior
{
private $dateFormatter;
public function __construct()
{
$this->dateFormatter = new DateFormatter();
}
public function it_DateFormatter_should_return_valid_DateTime_type()
{
$this->set(new DateTime('2017-03-14'))->shouldHaveType(DateTime::class);
}
public function it_should_translate_given_datetime_to_formatted_string()
{
$this->set(new DateTime('2017-03-14'));
$this->transformTo(new PolishDateFormat())->shouldBeString('14 marca 2017');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment