Skip to content

Commit

Permalink
Remove unnecessary method and extend test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Jun 9, 2024
1 parent dfde9bc commit f533051
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function datesUntil(

public function weekday(): Weekday
{
return Weekday::fromDate($this);
return Weekday::fromDateTime($this->toDateTimeImmutable());
}

// Mutations
Expand Down
12 changes: 2 additions & 10 deletions src/Weekday.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@ enum Weekday: string

public static function fromDateTime(\DateTimeImmutable $dateTime): self
{
return self::fromDayOfWeek((int) $dateTime->format('N'));
}
$dayOfWeek = (int) $dateTime->format('N');

public static function fromDate(Date $date): self
{
return self::fromDayOfWeek((int) $date->format('N'));
}

public static function fromDayOfWeek(int $dayOfIsoWeek): self
{
return match ($dayOfIsoWeek) {
return match ($dayOfWeek) {
1 => self::MONDAY,
2 => self::TUESDAY,
3 => self::WEDNESDAY,
Expand Down
61 changes: 61 additions & 0 deletions tests/Weekday/CompareToTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Weekday;

use DigitalCraftsman\DateTimePrecision\Weekday;
use PHPUnit\Framework\TestCase;

/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Weekday */
final class CompareToTest extends TestCase
{
/**
* @test
*
* @dataProvider dataProvider
*
* @covers ::compareTo
*/
public function compare_to_works(
int $expectedResult,
Weekday $weekday,
Weekday $comparator,
): void {
// -- Act & Assert
self::assertSame($expectedResult, $weekday->compareTo($comparator));
}

/**
* @return array<string, array{
* 0: int,
* 1: Weekday,
* 2: Weekday,
* }>
*/
public static function dataProvider(): array
{
return [
'1 weekday later' => [
1,
Weekday::THURSDAY,
Weekday::WEDNESDAY,
],
'same weekday' => [
0,
Weekday::SATURDAY,
Weekday::SATURDAY,
],
'1 weekday before' => [
-1,
Weekday::TUESDAY,
Weekday::WEDNESDAY,
],
'2 weekdays before' => [
-1,
Weekday::MONDAY,
Weekday::WEDNESDAY,
],
];
}
}
68 changes: 68 additions & 0 deletions tests/Weekday/ConstructionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Weekday;

use DigitalCraftsman\DateTimePrecision\Moment;
use DigitalCraftsman\DateTimePrecision\Weekday;
use PHPUnit\Framework\TestCase;

/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Weekday */
final class ConstructionTest extends TestCase
{
/**
* @test
*
* @dataProvider dataProvider
*
* @covers ::fromDateTime
*/
public function from_date_time_works(
Weekday $expectedResult,
Moment $moment,
): void {
// -- Act & Assert
self::assertTrue($expectedResult->isEqualTo(Weekday::fromDateTime($moment->dateTime)));
}

/**
* @return array<string, array{
* 0: Weekday,
* 1: Moment,
* }>
*/
public static function dataProvider(): array
{
return [
'monday' => [
Weekday::MONDAY,
Moment::fromString('2022-10-10 22:15:00'),
],
'tuesday' => [
Weekday::TUESDAY,
Moment::fromString('2022-10-11 22:15:00'),
],
'wednesday' => [
Weekday::WEDNESDAY,
Moment::fromString('2022-10-12 22:15:00'),
],
'thursday' => [
Weekday::THURSDAY,
Moment::fromString('2022-10-13 22:15:00'),
],
'friday' => [
Weekday::FRIDAY,
Moment::fromString('2022-10-14 22:15:00'),
],
'saturday' => [
Weekday::SATURDAY,
Moment::fromString('2022-10-15 22:15:00'),
],
'sunday' => [
Weekday::SUNDAY,
Moment::fromString('2022-10-16 22:15:00'),
],
];
}
}

0 comments on commit f533051

Please sign in to comment.