Skip to content

Commit

Permalink
Test enhancement (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k authored and BenMorel committed Mar 14, 2018
1 parent 9fd1b89 commit a878173
Show file tree
Hide file tree
Showing 11 changed files with 747 additions and 2 deletions.
22 changes: 22 additions & 0 deletions tests/Field/DayOfMonthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Brick\DateTime\Tests\Field;

use Brick\DateTime\Field\DayOfMonth;
use Brick\DateTime\Tests\AbstractTestCase;

/**
* Unit tests for class DayOfMonth.
*/
class DayOfMonthTest extends AbstractTestCase
{
/**
* @doesNotPerformAssertions
*/
public function testCheckWithNullMonthOfYear()
{
DayOfMonth::check(31);
}
}
32 changes: 32 additions & 0 deletions tests/Field/WeekOfYearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Brick\DateTime\Tests\Field;

use Brick\DateTime\Field\WeekOfYear;
use Brick\DateTime\Tests\AbstractTestCase;

/**
* Unit tests for class WeekOfYear.
*/
class WeekOfYearTest extends AbstractTestCase
{
/**
* @expectedException \Brick\DateTime\DateTimeException
* @expectedExceptionMessage Invalid week-of-year: -1 is not in the range 1 to 53.
*/
public function testCheckShouldThrowDateTimeExceptionWithFieldNotInRange()
{
WeekOfYear::check(-1);
}

/**
* @expectedException \Brick\DateTime\DateTimeException
* @expectedExceptionMessage Year 2000 does not have 53 weeks
*/
public function testCheckShouldThrowDateTimeExceptionWith52WeekYear()
{
WeekOfYear::check(53, 2000);
}
}
80 changes: 80 additions & 0 deletions tests/InstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\Duration;
use Brick\DateTime\Instant;
use Brick\DateTime\TimeZone;

/**
* Unit tests for class Instant.
Expand Down Expand Up @@ -373,6 +374,36 @@ public function testIsBeforeOrEqualTo(int $s1, int $n1, int $s2, int $n2, int $c
$this->assertSame($cmp <= 0, Instant::of($s1, $n1)->isBeforeOrEqualTo(Instant::of($s2, $n2)));
}

/**
* @dataProvider providerIsBetweenInclusive
*
* @param int $seconds The seconds value.
* @param int $nanos The nano seconds value.
* @param bool $isBetween Check the secs and nanos are between.
*/
public function testIsBetweenInclusive(int $seconds, int $nanos, $isBetween)
{
$this->assertSame($isBetween, Instant::of($seconds, $nanos)->isBetweenInclusive(
Instant::of(-1, -1),
Instant::of(1, 1)
));
}

/**
* @dataProvider providerIsBetweenExclusive
*
* @param int $seconds The seconds value.
* @param int $nanos The nano seconds value.
* @param bool $isBetween Check the secs and nanos are between.
*/
public function testIsBetweenExclusive(int $seconds, int $nanos, $isBetween)
{
$this->assertSame($isBetween, Instant::of($seconds, $nanos)->isBetweenExclusive(
Instant::of(-1, -1),
Instant::of(1, 1)
));
}

/**
* @dataProvider providerCompareTo
*
Expand Down Expand Up @@ -403,6 +434,46 @@ public function testIsPast(int $testSecond, int $testNano, int $nowSecond, int $
$this->assertSame($cmp === -1, Instant::of($testSecond, $testNano)->isPast($clock));
}

/**
* @return array
*/
public function providerIsBetweenExclusive() : array
{
return [
[-1, -2, false],
[-1, -1, false],
[-1, 0, true],
[-1, 1, true],
[0, -1, true],
[0, 0, true],
[0, 1, true],
[1, -1, true],
[1, 0, true],
[1, 1, false],
[1, 2, false],
];
}

/**
* @return array
*/
public function providerIsBetweenInclusive() : array
{
return [
[-1, -2, false],
[-1, -1, true],
[-1, 0, true],
[-1, 1, true],
[0, -1, true],
[0, 0, true],
[0, 1, true],
[1, -1, true],
[1, 0, true],
[1, 1, true],
[1, 2, false],
];
}

/**
* @return array
*/
Expand Down Expand Up @@ -553,4 +624,13 @@ public function providerToString() : array
[2000000000, 0, '2033-05-18T03:33:20Z'],
];
}

public function testAtTimeZone()
{
$timeZone = TimeZone::utc();
$instant = Instant::of(1000000000);
$result = $instant->atTimeZone($timeZone);
$this->assertSame(1000000000, $result->getInstant()->getEpochSecond());
$this->assertSame('2001-09-09T01:46:40', (string) $result->getDateTime());
}
}
12 changes: 12 additions & 0 deletions tests/IntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
*/
class IntervalTest extends AbstractTestCase
{
/**
* @expectedException \Brick\DateTime\DateTimeException
* @expectedExceptionMessage The end instant must not be before the start instant.
*/
public function testEndInstantIsNotBeforeStartInstant()
{
$start = Instant::of(2000000000, 987654321);
$end = Instant::of(2000000009, 123456789);

$interval = new Interval($end, $start);
}

public function testGetStartEnd()
{
$start = Instant::of(2000000000, 987654321);
Expand Down
11 changes: 11 additions & 0 deletions tests/MonthDayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ public function providerWithInvalidMonthThrowsException() : array
];
}

public function testWithDayWithSameDay()
{
$month = 1;
$day = 20;
$monthDay = MonthDay::of($month, $day);
$newMonthDay = $monthDay->withDay($day);

$this->assertMonthDayIs($month, $day, $monthDay);
$this->assertMonthDayIs($month, $day, $newMonthDay);
}

/**
* @dataProvider providerWithDay
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Parser/DateTimeParseResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Brick\DateTime\Tests\Parser;

use Brick\DateTime\Parser\DateTimeParseResult;
use Brick\DateTime\Tests\AbstractTestCase;

/**
* Unit tests for class DateTimeParseResult.
*/
class DateTimeParseResultTest extends AbstractTestCase
{
/**
* @expectedException \Brick\DateTime\Parser\DateTimeParseException
* @expectedExceptionMessage Field invalid_field_name is not present in the parsed result.
*/
public function testGetFieldWithInvalidFieldStringName()
{
$dateTimeParseResult = new DateTimeParseResult();
$dateTimeParseResult->getField('invalid_field_name');
}
}
45 changes: 45 additions & 0 deletions tests/Parser/PatternParserBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Brick\DateTime\Tests\Parser;

use Brick\DateTime\Parser\PatternParserBuilder;
use Brick\DateTime\Tests\AbstractTestCase;

/**
* Unit tests for class PatternParserBuilder.
*/
class PatternParserBuilderTest extends AbstractTestCase
{
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot call endOptional() without a call to startOptional() first.
*/
public function testEndOptionalShouldThrowRuntimeException()
{
$patternParserBuilder = new PatternParserBuilder();
$patternParserBuilder->endOptional();
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot call endGroup() without a call to startGroup() first.
*/
public function testEndGroupShouldThrowRuntimeException()
{
$patternParserBuilder = new PatternParserBuilder();
$patternParserBuilder->endGroup();
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Builder misses call to endOptional() or endGroup().
*/
public function testToParserWithNonEmptyStack()
{
$patternParserBuilder = new PatternParserBuilder();
$patternParserBuilder->startGroup();
$patternParserBuilder->toParser();
}
}
19 changes: 19 additions & 0 deletions tests/StopwatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ private static function setClockTime(int $second, int $nano) : void
self::$clock->setTime(Instant::of($second, $nano));
}

public function testConstructorWithNullClock()
{
$stopwatch = new Stopwatch();

$this->assertNull($stopwatch->getStartTime());
$this->assertFalse($stopwatch->isRunning());
$this->assertDurationIs(0, 0, $stopwatch->getElapsedTime());
}

/**
* @return Stopwatch
*/
Expand Down Expand Up @@ -80,6 +89,16 @@ public function testElapsedTimeWhileRunning(Stopwatch $stopwatch) : Stopwatch
return $stopwatch;
}

public function testStopWithNullStartTime()
{
$stopwatch = new Stopwatch();
$stopwatch->stop();

$this->assertNull($stopwatch->getStartTime());
$this->assertFalse($stopwatch->isRunning());
$this->assertDurationIs(0, 0, $stopwatch->getElapsedTime());
}

/**
* @depends testElapsedTimeWhileRunning
*
Expand Down
26 changes: 24 additions & 2 deletions tests/YearMonthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,27 @@ public function testWithYear()
$this->assertYearMonthIs(2001, 5, YearMonth::of(2000, 5)->withYear(2001));
}

public function testWithYearWithSameYear()
{
$year = 2018;
$month = 2;
$yearMonth = YearMonth::of($year, $month);

$this->assertYearMonthIs($year, $month, $yearMonth->withYear($year));
}

public function testWithMonth()
{
$this->assertYearMonthIs(2000, 12, YearMonth::of(2000, 1)->withMonth(12));
}

public function testWithMonthWithSameMonth()
{
$month = 2;

$this->assertYearMonthIs(2000, $month, YearMonth::of(2000, $month)->withMonth($month));
}

public function testGetFirstDay()
{
$this->assertLocalDateIs(2023, 10, 1, YearMonth::of(2023, 10)->getFirstDay());
Expand All @@ -347,7 +363,7 @@ public function testGetFirstDay()
* @param int $month
* @param int $day
*/
public function getGetLastDay(int $year, int $month, int $day)
public function testGetLastDay(int $year, int $month, int $day)
{
$this->assertLocalDateIs($year, $month, $day, YearMonth::of($year, $month)->getLastDay());
}
Expand All @@ -362,7 +378,7 @@ public function providerGetLastDay() : array
[2000, 2, 29],
[2001, 2, 28],
[2002, 3, 31],
[2002, 4, 40],
[2002, 4, 30],
];
}

Expand All @@ -386,6 +402,12 @@ public function testPlusYears(int $year, int $month, int $plusYears, int $expect
$this->assertYearMonthIs($expectedYear, $expectedMonth, $yearMonth->plusYears($plusYears));
}

public function testPlusZeroYears()
{
$yearMonth = YearMonth::of(2005, 1);
$this->assertYearMonthIs(2005, 1, $yearMonth->plusYears(0));
}

/**
* @return array
*/
Expand Down
Loading

0 comments on commit a878173

Please sign in to comment.