Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created September 24, 2018 09:32
Show Gist options
  • Save hatelove/36d119d9f777ec2d8b7d5d0f10afafc2 to your computer and use it in GitHub Desktop.
Save hatelove/36d119d9f777ec2d8b7d5d0f10afafc2 to your computer and use it in GitHub Desktop.
today is xmas
<?php
/**
* Created by PhpStorm.
* User: joeychen
* Date: 2018/9/24
* Time: 下午 04:58
*/
namespace App;
use DateTime;
class Holiday
{
public function SayXmas()
{
$today = $this->getToday();
$todayDate = $today->format('md');
if ($todayDate == '1224' || $todayDate == '1225') {
return 'Merry Xmas';
}
else {
return 'Today is not Xmas';
}
}
/**
* @return DateTime
*/
protected function getToday()
{
$today = new DateTime();
return $today;
}
}
<?php
/**
* Created by PhpStorm.
* User: joeychen
* Date: 2018/9/24
* Time: 下午 05:00
*/
namespace Tests {
use App\FakeHoliday;
use DateTime;
use PHPUnit\Framework\TestCase;
class HolidayTest extends TestCase
{
private $holiday;
protected function setUp()
{
$this->holiday = new FakeHoliday();
}
public function test_today_is_xmas()
{
$this->givenToday(12, 25);
$this->sayXmasShouldResponse('Merry Xmas');
}
public function test_today_is_not_xmas()
{
$this->givenToday(11, 25);
$this->sayXmasShouldResponse('Today is not Xmas');
}
private function givenToday($month, $day)
{
$today = new DateTime();
$today->setDate(2010, $month, $day);
$this->holiday->setToday($today);
}
private function sayXmasShouldResponse($expected)
{
$response = $this->holiday->SayXmas();
$this->assertEquals($expected, $response);
}
}
}
namespace App {
class FakeHoliday extends Holiday
{
private $today;
public function setToday($date)
{
$this->today = $date;
}
protected function getToday()
{
return $this->today;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment