Created
July 5, 2014 18:53
-
-
Save jrdnull/edbf81888307aa89458f 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 | |
class Clock { | |
private $minutes; | |
/** | |
* @param int $hour | |
* @param int $minutes | |
*/ | |
public function __construct($hour, $minutes = 0) | |
{ | |
$this->minutes = $hour * 60 + $minutes; | |
} | |
/** | |
* Returns a new Clock incremented by $minutes | |
* | |
* @param int $minutes | |
* @return Clock | |
*/ | |
public function add($minutes) | |
{ | |
return $this->build($this->minutes + $minutes); | |
} | |
/** | |
* Returns a new Clock deincremented by $minutes | |
* | |
* @param int $minutes | |
* @return Clock | |
*/ | |
public function sub($minutes) | |
{ | |
return $this->build($this->minutes - $minutes); | |
} | |
/** | |
* Returns the string representation of the receiver | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return sprintf('%02d:%02d', $this->minutes / 60, $this->minutes % 60); | |
} | |
/** | |
* @param $minutes | |
* @return Clock | |
*/ | |
private function build($minutes) | |
{ | |
if ($minutes < 60) { | |
$minutes += 24 * 60; | |
} | |
$hour = floor($minutes / 60) ?: 0; | |
return new Clock($hour < 24 ? $hour : $hour - 24, $minutes % 60); | |
} | |
} |
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 | |
require_once __DIR__ . '/Clock.php'; | |
class ClockTest extends PHPUnit_Framework_TestCase { | |
public function testOnTheHour() { | |
$clock = new Clock(8); | |
$this->assertEquals('08:00', $clock->__toString()); | |
} | |
public function testPastTheHour() { | |
$clock = new Clock(11, 9); | |
$this->assertEquals('11:09', $clock->__toString()); | |
} | |
public function testAddingAFewMinutes() { | |
$clock = new Clock(10); | |
$clock = $clock->add(3); | |
$this->assertEquals('10:03', $clock->__toString()); | |
} | |
public function testAddingOverAnHour() { | |
$clock = new Clock(10); | |
$clock = $clock->add(61); | |
$this->assertEquals('11:01', $clock->__toString()); | |
} | |
public function testWrapAroundAtMidnight() { | |
$clock = new Clock(23, 30); | |
$clock = $clock->add(60); | |
$this->assertEquals('00:30', $clock->__toString()); | |
} | |
public function testSubtractMinutes() { | |
$clock = new Clock(10); | |
$clock = $clock->sub(90); | |
$this->assertEquals('08:30', $clock->__toString()); | |
} | |
public function testWrapAroundBackwards() { | |
$clock = new Clock(0, 30); | |
$clock = $clock->sub(60); | |
$this->assertEquals('23:30', $clock->__toString()); | |
} | |
public function testWrapAroundDay() { | |
$clock = new Clock(5, 32); | |
$clock = $clock->add(25 * 60); | |
$this->assertEquals('06:32', $clock->__toString()); | |
} | |
public function testWrapAroundDayBackwards() { | |
$clock = new Clock(5, 32); | |
$clock = $clock->sub(25 * 60); | |
$this->assertEquals('04:32', $clock->__toString()); | |
} | |
public function testEquivalentClocks() { | |
$this->assertEquals(new Clock(15, 37), new Clock(15, 37)); | |
} | |
public function testInequivalentClocks() { | |
$this->assertNotEquals(new Clock(01, 01), new Clock(18, 32)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment