Created
March 14, 2017 20:54
-
-
Save event15/c020f5c7481530e348876744c3b63d82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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