From d8727bc94a158d3fb3dbccf5304fe914af4f4031 Mon Sep 17 00:00:00 2001 From: DRvanR Date: Tue, 10 Feb 2015 11:28:51 +0100 Subject: [PATCH] Added DateTime:comesBefore, added tests for DateTime --- src/Surfnet/Stepup/DateTime/DateTime.php | 15 ++- .../Stepup/Tests/DateTime/DateTimeTest.php | 103 ++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src/Surfnet/Stepup/Tests/DateTime/DateTimeTest.php diff --git a/src/Surfnet/Stepup/DateTime/DateTime.php b/src/Surfnet/Stepup/DateTime/DateTime.php index 4c1e4f85b..3bfdcac58 100644 --- a/src/Surfnet/Stepup/DateTime/DateTime.php +++ b/src/Surfnet/Stepup/DateTime/DateTime.php @@ -78,17 +78,26 @@ public function __construct(CoreDateTime $dateTime = null) } /** - * @param DateInterval $intervalSpec + * @param DateInterval $interval * @return DateTime */ - public function add(DateInterval $intervalSpec) + public function add(DateInterval $interval) { $dateTime = clone $this->dateTime; - $dateTime->add($intervalSpec); + $dateTime->add($interval); return new self($dateTime); } + /** + * @param DateTime $dateTime + * @return boolean + */ + public function comesBefore(DateTime $dateTime) + { + return $this->dateTime < $dateTime->dateTime; + } + /** * @param DateTime $dateTime * @return boolean diff --git a/src/Surfnet/Stepup/Tests/DateTime/DateTimeTest.php b/src/Surfnet/Stepup/Tests/DateTime/DateTimeTest.php new file mode 100644 index 000000000..838094a13 --- /dev/null +++ b/src/Surfnet/Stepup/Tests/DateTime/DateTimeTest.php @@ -0,0 +1,103 @@ +assertEquals('Y-m-d\\TH:i:sP', DateTime::FORMAT); + } + + /** + * Ensure that the __toString of our DateTime object actually uses the correct format. For the reason why, read the + * docblock above the {@see the_configured_format_is_what_is_needed_for_correct_application_behavior()} test + * + * @test + * @group domain + */ + public function to_string_returns_the_time_in_the_correct_format() + { + $coreDateTimeObject = new CoreDateTime('@1000'); + $ourDateTimeObject = new DateTime(new CoreDateTime('@1000')); + + $this->assertEquals($coreDateTimeObject->format(DateTime::FORMAT), (string) $ourDateTimeObject); + } + + /** + * @test + * @group domain + */ + public function add_returns_a_different_object_that_has_the_interval_added() + { + $base = new DateTime(new CoreDateTime('@1000')); + $interval = new DateInterval('PT1S'); + + $result = $base->add($interval); + + $this->assertFalse($result === $base, 'DateTime::add must return a different object'); + $this->assertTrue($result > $base, 'DateTime::add adds the interval to the new object'); + } + + /** + * @test + * @group domain + */ + public function comes_before_works_with_exclusive_comparison() + { + $base = new DateTime(new CoreDateTime('@1000')); + $before = new DateTime(new CoreDateTime('@999')); + $same = new DateTime(new CoreDateTime('@1000')); + $after = new DateTime(new CoreDateTime('@1001')); + + $this->assertTrue($before->comesBefore($base)); + $this->assertFalse($same->comesBefore($base)); + $this->assertFalse($after->comesBefore($base)); + } + + /** + * @test + * @group domain + */ + public function comes_after_works_with_exclusive_comparison() + { + $base = new DateTime(new CoreDateTime('@1000')); + $before = new DateTime(new CoreDateTime('@999')); + $same = new DateTime(new CoreDateTime('@1000')); + $after = new DateTime(new CoreDateTime('@1001')); + + $this->assertFalse($before->comesAfter($base)); + $this->assertFalse($same->comesAfter($base)); + $this->assertTrue($after->comesAfter($base)); + } + + /** + * @test + * @group domain + */ + public function comes_after_or_is_equal_works_with_inclusive_comparison() + { + $base = new DateTime(new CoreDateTime('@1000')); + $before = new DateTime(new CoreDateTime('@999')); + $same = new DateTime(new CoreDateTime('@1000')); + $after = new DateTime(new CoreDateTime('@1001')); + + $this->assertFalse($before->comesAfterOrIsEqual($base)); + $this->assertTrue($same->comesAfterOrIsEqual($base)); + $this->assertTrue($after->comesAfterOrIsEqual($base)); + } +}