Skip to content
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

Test enhancement #2

Merged
merged 16 commits into from
Mar 14, 2018
Merged

Test enhancement #2

merged 16 commits into from
Mar 14, 2018

Conversation

peter279k
Copy link
Contributor

Changed log

  • add more tests for other uncovered classes.

*/
public function testIsBetweenExclusive(int $s1, int $n1, int $s2, int $n2, int $cmp)
{
$this->assertSame($cmp <= 0, Instant::of($s1, $n1)->isBetweenExclusive(Instant::of($s2, $n2), Instant::of($s2, $n2)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not testing much in these two methods, you're only testing that the value is within an interval of 0 seconds.

What I would do here is hardcode the interval start and end, and test with a range of possible values in between:

public function testIsBetweenExclusive(int $seconds, int $nanos, bool $isBetween)
{
    $this->assertSame($isBetween, Instant::of($seconds, $nanos)->isBetweenExclusive(
        Instant::of(-1, -1),
        Instant::of(1, 1)
    );
}

And for the provider:

[
    [-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],
]

Same for isBetweenInclusive(), with the data provider altered accordingly.

* @expectedException Brick\DateTime\DateTimeException
* @expectedExceptionMessage Year 2000 does not have 53 weeks
*/
public function testCehckTimeShouldThrowDateYimeException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still 2 typos: 'Cehck' instead of 'Check' and 'Yime' instead of 'Time'!

[ 1, 0, 0, 1, 1],
];
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These providers are obsolete!

Copy link
Contributor Author

@peter279k peter279k Mar 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove this data provider.

* @expectedException Brick\DateTime\DateTimeException
* @expectedExceptionMessage Invalid week-of-year: -1 is not in the range 1 to 53.
*/
public function testCheckShouldReturnDateTimeExceptionWithFieldNotInRange()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return => Throw

* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot call endOptional() without a call to startOptional() first.
*/
public function testEndOptionalShouldReturnRuntimeException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return => Throw

* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot call endGroup() without a call to startGroup() first.
*/
public function testEndGroupShouldReturnRunTimeException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return => Throw

public function testWithYearIsTheSameYear()
{
$year = (int)date('Y');
$month = (int)date('m');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use fixed year/month here

$month = (int)date('m');

$this->assertInstanceOf(YearMonth::class, YearMonth::of($year, $month)->withYear($year));
$this->assertYearMonthIs($year, $month, YearMonth::of($year, $month)->withYear($year));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should instantiate YearMonth::of(...) only once and use it in 2 lines.

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

public function testWithMonthWithSameMonth()
{
$month = (int)date('m');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a fixed month here

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

public function testPlusYearsWithParameterIsZero()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testPlusZeroYears()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that and it seems that the test method name can be short.

public function testNow()
{
$timeZone = TimeZone::parse('Asia/Taipei');
$yearWeek = YearWeek::now($timeZone);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should provide a Clock here, and check that the resulting YearWeek is in line with the clock!

Copy link
Contributor Author

@peter279k peter279k Mar 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give some examples about this?

Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set set timestamp that now() will use, this way:

$clock = new FixedClock(Instant::of(2000000000));
$yearWeek = YearWeek::now($timeZone, $clock);

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame('Thursday', (string)$zonedDateTime->plusPeriod(Period::ofWeeks(11))->getDayOfWeek());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assert the full string here: assertSame('2000-...', (string) $zonedDateTime->plusPeriod(Period::ofWeeks(11)));

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(56, $zonedDateTime->plusDuration(Duration::zero())->getSecond());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(2002, $zonedDateTime->plusYears(2)->getYear());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(3, $zonedDateTime->plusMonths(2)->getMonth());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(2, $zonedDateTime->plusWeeks(2)->getMonth());
$this->assertSame(3, $zonedDateTime->plusWeeks(2)->getDay());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(22, $zonedDateTime->plusDays(2)->getDay());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(14, $zonedDateTime->plusHours(2)->getHour());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(36, $zonedDateTime->plusMinutes(2)->getMinute());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(58, $zonedDateTime->plusSeconds(2)->getSecond());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame('Thursday', (string)$zonedDateTime->minusPeriod(Period::ofWeeks(11))->getDayOfWeek());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(56, $zonedDateTime->minusDuration(Duration::zero())->getSecond());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(1999, $zonedDateTime->minusYears(1)->getYear());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(12, $zonedDateTime->minusMonths(1)->getMonth());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame(1, $zonedDateTime->minusWeeks(2)->getMonth());
$this->assertSame(6, $zonedDateTime->minusWeeks(2)->getDay());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

{
$dayOfMonth = new DayOfMonth();

$this->assertNull($dayOfMonth->check(31));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable with assertNull() for a void method. To remove the assertion and avoid a "risky" test, maybe you can give a try to @doesNotPerformAssertions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds good because it's the void method and not return the null currently.

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame('2000-01-10T12:34:56.123456789-08:00', (string) $zonedDateTime->withFixedOffsetTimeZone(56));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withFixedOffsetTimeZone() does not take any parameters!


public function testPlusPeriod()
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime->plusDuration(Duration::zero()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use another Duration than zero?

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime->minusDuration(Duration::zero()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, a non-zero Duration would be more useful!


declare(strict_types=1);

namespace Brick\DateTime\Tests;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in namespace Brick\DateTime\Tests\Parser, and located in a tests/Parser directory


declare(strict_types=1);

namespace Brick\DateTime\Tests;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in namespace Brick\DateTime\Tests\Parser, and located in a tests/Parser directory

use Brick\DateTime\Parser\PatternParserBuilder;

/**
* Unit tests for class IsoParsers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for class PatternParserBuilder.

class DateTimeParseResultTest extends AbstractTestCase
{
/**
* @expectedException Brick\DateTime\Parser\DateTimeParseException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add leading \ before exception class

class WeekOfYearTest extends AbstractTestCase
{
/**
* @expectedException Brick\DateTime\DateTimeException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add leading \ before exception class

}

/**
* @expectedException Brick\DateTime\DateTimeException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add leading \ before exception class

@@ -12,6 +12,18 @@
*/
class IntervalTest extends AbstractTestCase
{
/**
* @expectedException Brick\DateTime\DateTimeException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add leading \ before exception class

* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot call endGroup() without a call to startGroup() first.
*/
public function testEndGroupShouldThrowRunTimeException()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RunTime => Runtime

@@ -330,16 +330,38 @@ public function testWithYear()
$this->assertYearMonthIs(2001, 5, YearMonth::of(2000, 5)->withYear(2001));
}

public function testWithYearIsTheSameYear()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to testWithYearWithSameYear

$month = 2;
$yearMonth = YearMonth::of($year, $month);

$this->assertInstanceOf(YearMonth::class, $yearMonth->withYear($year));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not needed, assertYearMonthIs() will throw a TypeError if it's not an instance of YearMonth.

public function testGetFirstDay()
{
$this->assertLocalDateIs(2023, 10, 1, YearMonth::of(2023, 10)->getFirstDay());
}

public function testGetLastDay()
Copy link
Member

@BenMorel BenMorel Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there was already a test for getLastDay(), that was not executed because it was wrongly named!
You can remove this method, and rename my test getGetLastDay to testGetLastDay!

$yearWeek = YearWeek::now($timeZone, $now);

$this->assertSame('2033-W20', (string) $yearWeek);
$this->assertInstanceOf(YearWeek::class, $yearWeek);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use assertYearWeekIs() instead of these 2 lines?

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertDayOfWeekIs(4, $zonedDateTime->getDayOfWeek());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the DayOfWeek::THURSDAY constant instead of 4 for readability

$localDateTime = LocalDateTime::parse($localDateTime);
$zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);

$this->assertSame('2000-03-25T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime->withSecond(56));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use another second value in withSecond()? It's the same as in the original date-time!

/**
* Unit tests for class DayOfMonth.
*
* @doesNotPerformAssertions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work with the annotation on the method rather than on the class?
It would be better if we add other methods in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I set the wrong place for the annotation.

{
$dayOfMonth = new DayOfMonth();

$dayOfMonth->check(31);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check() is a static method, it should be DayOfMonth::check(31); and the first line can be removed.

@BenMorel
Copy link
Member

I think you forgot to reintroduce DateTimeParseResultTest and PatternParserBuilderTest, they're missing!

@BenMorel BenMorel merged commit a878173 into brick:master Mar 14, 2018
@BenMorel
Copy link
Member

Looks good now 👍 Thanks!

@peter279k peter279k deleted the test_enhancement branch August 25, 2020 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants