Skip to content

sync clock #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions exercises/practice/clock/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@

class Clock
{
/**
* This class implements PHP's magic method __toString().
*
* By implementing this method, the class adheres to the `Stringable` interface.
* When an object of this class is used in string context (e.g., echo or string cast),
* this method is automatically called.
*
* More on `Stringable`: https://www.php.net/manual/en/class.stringable.php
*
* @return string The string representation of the Clock object
*/
public function __toString(): string
{
throw new \BadMethodCallException("Implement the __toString function");
Expand Down
82 changes: 41 additions & 41 deletions exercises/practice/clock/ClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public static function setUpBeforeClass(): void

/**
* uuid a577bacc-106b-496e-9792-b3083ea8705e
* @testdox on the hour
* @testdox Create a new clock with an initial time - on the hour
*/
public function testOnTheHour(): void
{
$clock = new Clock(8);

$this->assertEquals('08:00', $clock);
$this->assertEquals('08:00', (string)$clock);
}

/**
Expand All @@ -28,7 +28,7 @@ public function testPastTheHour(): void
{
$clock = new Clock(11, 9);

$this->assertEquals('11:09', $clock);
$this->assertEquals('11:09', (string)$clock);
}

/**
Expand All @@ -38,7 +38,7 @@ public function testPastTheHour(): void
public function testMidnightIsZeroHours(): void
{
$clock = new Clock(24, 0);
$this->assertEquals('00:00', $clock);
$this->assertEquals('00:00', (string)$clock);
}

/**
Expand All @@ -48,7 +48,7 @@ public function testMidnightIsZeroHours(): void
public function testHourRollsOver(): void
{
$clock = new Clock(25, 0);
$this->assertEquals('01:00', $clock);
$this->assertEquals('01:00', (string)$clock);
}

/**
Expand All @@ -58,7 +58,7 @@ public function testHourRollsOver(): void
public function testHourRollsOverContinuously(): void
{
$clock = new Clock(100, 0);
$this->assertEquals('04:00', $clock);
$this->assertEquals('04:00', (string)$clock);
}

/**
Expand All @@ -68,7 +68,7 @@ public function testHourRollsOverContinuously(): void
public function testSixtyMinutesIsNextHour(): void
{
$clock = new Clock(1, 60);
$this->assertEquals('02:00', $clock);
$this->assertEquals('02:00', (string)$clock);
}

/**
Expand All @@ -78,7 +78,7 @@ public function testSixtyMinutesIsNextHour(): void
public function testMinutesRollOver(): void
{
$clock = new Clock(0, 160);
$this->assertEquals('02:40', $clock);
$this->assertEquals('02:40', (string)$clock);
}

/**
Expand All @@ -88,7 +88,7 @@ public function testMinutesRollOver(): void
public function testMinutesRollOverContinuously(): void
{
$clock = new Clock(0, 1723);
$this->assertEquals('04:43', $clock);
$this->assertEquals('04:43', (string)$clock);
}

/**
Expand All @@ -98,7 +98,7 @@ public function testMinutesRollOverContinuously(): void
public function testHourAndMinutesRollOver(): void
{
$clock = new Clock(25, 160);
$this->assertEquals('03:40', $clock);
$this->assertEquals('03:40', (string)$clock);
}

/**
Expand All @@ -108,7 +108,7 @@ public function testHourAndMinutesRollOver(): void
public function testHourAndMinutesRollOverContinuously(): void
{
$clock = new Clock(201, 3001);
$this->assertEquals('11:01', $clock);
$this->assertEquals('11:01', (string)$clock);
}


Expand All @@ -119,7 +119,7 @@ public function testHourAndMinutesRollOverContinuously(): void
public function testHourAndMinutesRollOverToExactlyMidnight(): void
{
$clock = new Clock(72, 8640);
$this->assertEquals('00:00', $clock);
$this->assertEquals('00:00', (string)$clock);
}

/**
Expand All @@ -129,7 +129,7 @@ public function testHourAndMinutesRollOverToExactlyMidnight(): void
public function testNegativeHour(): void
{
$clock = new Clock(-1, 15);
$this->assertEquals('23:15', $clock);
$this->assertEquals('23:15', (string)$clock);
}

/**
Expand All @@ -139,7 +139,7 @@ public function testNegativeHour(): void
public function testNegativeHourRollsOver(): void
{
$clock = new Clock(-25, 0);
$this->assertEquals('23:00', $clock);
$this->assertEquals('23:00', (string)$clock);
}

/**
Expand All @@ -149,7 +149,7 @@ public function testNegativeHourRollsOver(): void
public function testNegativeHourRollsOverContinuously(): void
{
$clock = new Clock(-91, 0);
$this->assertEquals('05:00', $clock);
$this->assertEquals('05:00', (string)$clock);
}

/**
Expand All @@ -159,7 +159,7 @@ public function testNegativeHourRollsOverContinuously(): void
public function testNegativeMinutes(): void
{
$clock = new Clock(1, -40);
$this->assertEquals('00:20', $clock);
$this->assertEquals('00:20', (string)$clock);
}

/**
Expand All @@ -169,17 +169,17 @@ public function testNegativeMinutes(): void
public function testNegativeMinutesRollOver(): void
{
$clock = new Clock(1, -160);
$this->assertEquals('22:20', $clock);
$this->assertEquals('22:20', (string)$clock);
}

/**
* uuid 5d6bb225-130f-4084-84fd-9e0df8996f2a
* uuid d483ceef-b520-4f0c-b94a-8d2d58cf0484
* @testdox negative minutes roll over continuously
*/
public function testNegativeMinutesRollOverContinuously(): void
{
$clock = new Clock(1, -4820);
$this->assertEquals('16:40', $clock);
$this->assertEquals('16:40', (string)$clock);
}

/**
Expand All @@ -189,7 +189,7 @@ public function testNegativeMinutesRollOverContinuously(): void
public function testNegativeSixtyMinutesIsPreviousHour(): void
{
$clock = new Clock(2, -60);
$this->assertEquals('01:00', $clock);
$this->assertEquals('01:00', (string)$clock);
}

/**
Expand All @@ -199,7 +199,7 @@ public function testNegativeSixtyMinutesIsPreviousHour(): void
public function testNegativeHourAndMinutesBothRollOver(): void
{
$clock = new Clock(-25, -160);
$this->assertEquals('20:20', $clock);
$this->assertEquals('20:20', (string)$clock);
}

/**
Expand All @@ -209,19 +209,19 @@ public function testNegativeHourAndMinutesBothRollOver(): void
public function testNegativeHourAndMinutesBothRollOverContinuously(): void
{
$clock = new Clock(-121, -5810);
$this->assertEquals('22:10', $clock);
$this->assertEquals('22:10', (string)$clock);
}


/**
* uuid d098e723-ad29-4ef9-997a-2693c4c9d89a
* @testdox Add minutes
* @testdox Add minutes - add minutes
*/
public function testAddMinutes()
{
$clock = new Clock(10, 0);
$clock = $clock->add(3);
$this->assertEquals("10:03", $clock);
$this->assertEquals("10:03", (string)$clock);
}

/**
Expand All @@ -232,7 +232,7 @@ public function testAddNoMinutes()
{
$clock = new Clock(6, 41);
$clock = $clock->add(0);
$this->assertEquals("06:41", $clock);
$this->assertEquals("06:41", (string)$clock);
}

/**
Expand All @@ -243,7 +243,7 @@ public function testAddToNextHour()
{
$clock = new Clock(0, 45);
$clock = $clock->add(40);
$this->assertEquals("01:25", $clock);
$this->assertEquals("01:25", (string)$clock);
}

/**
Expand All @@ -254,7 +254,7 @@ public function testAddMoreThanOneHour()
{
$clock = new Clock(10, 0);
$clock = $clock->add(61);
$this->assertEquals("11:01", $clock);
$this->assertEquals("11:01", (string)$clock);
}

/**
Expand All @@ -265,7 +265,7 @@ public function testAddMoreThanTwoHoursWithCarry()
{
$clock = new Clock(0, 45);
$clock = $clock->add(160);
$this->assertEquals("03:25", $clock);
$this->assertEquals("03:25", (string)$clock);
}

/**
Expand All @@ -276,7 +276,7 @@ public function testAddAcrossMidnight()
{
$clock = new Clock(23, 59);
$clock = $clock->add(2);
$this->assertEquals("00:01", $clock);
$this->assertEquals("00:01", (string)$clock);
}

/**
Expand All @@ -287,7 +287,7 @@ public function testAddMoreThanOneDay()
{
$clock = new Clock(5, 32);
$clock = $clock->add(1500);
$this->assertEquals("06:32", $clock);
$this->assertEquals("06:32", (string)$clock);
}

/**
Expand All @@ -298,18 +298,18 @@ public function testAddMoreThanTwoDays()
{
$clock = new Clock(1, 1);
$clock = $clock->add(3500);
$this->assertEquals("11:21", $clock);
$this->assertEquals("11:21", (string)$clock);
}

/**
* uuid 37336cac-5ede-43a5-9026-d426cbe40354
* @testdox subtract minutes
* @testdox Subtract minutes - subtract minutes
*/
public function testSubtractMinutes(): void
{
$clock = new Clock(10, 3);
$clock = $clock->sub(3);
$this->assertEquals("10:00", $clock);
$this->assertEquals("10:00", (string)$clock);
}

/**
Expand All @@ -320,7 +320,7 @@ public function testSubtractToPreviousHour(): void
{
$clock = new Clock(10, 3);
$clock = $clock->sub(30);
$this->assertEquals("09:33", $clock);
$this->assertEquals("09:33", (string)$clock);
}

/**
Expand All @@ -331,7 +331,7 @@ public function testSubtractMoreThanAnHour(): void
{
$clock = new Clock(10, 3);
$clock = $clock->sub(70);
$this->assertEquals("08:53", $clock);
$this->assertEquals("08:53", (string)$clock);
}

/**
Expand All @@ -342,7 +342,7 @@ public function testSubtractAcrossMidnight(): void
{
$clock = new Clock(0, 3);
$clock = $clock->sub(4);
$this->assertEquals("23:59", $clock);
$this->assertEquals("23:59", (string)$clock);
}

/**
Expand All @@ -353,7 +353,7 @@ public function testSubtractMoreThanTwoHours(): void
{
$clock = new Clock(0, 0);
$clock = $clock->sub(160);
$this->assertEquals("21:20", $clock);
$this->assertEquals("21:20", (string)$clock);
}

/**
Expand All @@ -364,7 +364,7 @@ public function testSubtractMoreThanTwoHoursWithBorrow(): void
{
$clock = new Clock(6, 15);
$clock = $clock->sub(160);
$this->assertEquals("03:35", $clock);
$this->assertEquals("03:35", (string)$clock);
}

/**
Expand All @@ -375,7 +375,7 @@ public function testSubtractMoreThanOneDay(): void
{
$clock = new Clock(5, 32);
$clock = $clock->sub(1500);
$this->assertEquals("04:32", $clock);
$this->assertEquals("04:32", (string)$clock);
}

/**
Expand All @@ -386,12 +386,12 @@ public function testSubtractMoreThanTwoDays(): void
{
$clock = new Clock(2, 20);
$clock = $clock->sub(3000);
$this->assertEquals("00:20", $clock);
$this->assertEquals("00:20", (string)$clock);
}

/**
* uuid f2fdad51-499f-4c9b-a791-b28c9282e311
* @testdox clocks with same time
* @testdox Compare two clocks for equality - clocks with same time
*/
public function testClocksWithSameTime(): void
{
Expand Down