Skip to content

Commit

Permalink
Added DateTime:comesBefore, added tests for DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
DRvanR committed Feb 10, 2015
1 parent e1d7f77 commit d8727bc
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Surfnet/Stepup/DateTime/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions src/Surfnet/Stepup/Tests/DateTime/DateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Surfnet\Stepup\Tests\DateTime;

use DateInterval;
use DateTime as CoreDateTime;
use PHPUnit_Framework_TestCase as UnitTest;
use Surfnet\Stepup\DateTime\DateTime;

class DateTimeTest extends UnitTest
{
/**
* Might seem a bit overdone, but we rely on this specific format in quite a bit of places. If the format changes
* this might lead to some unforeseen errors. This ensures that if the format is changed, this test fails and
* that you're hopefully reading this as an instruction to check all the places that handle datetime for
* compatibility with the new format. Think about log(-processors), (de-)serializers, etc.
*
* @test
* @group domain
*/
public function the_configured_format_is_what_is_needed_for_correct_application_behavior()
{
$this->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));
}
}

0 comments on commit d8727bc

Please sign in to comment.