Skip to content

Commit 3c3b8b9

Browse files
author
Christian Kolb
committed
Add tests
1 parent bd77f2f commit 3c3b8b9

File tree

10 files changed

+535
-0
lines changed

10 files changed

+535
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ coverage.xml
1515
# .env files
1616
/.env*
1717
!.env.example
18+
19+
# Claude
20+
/.claude

tests/Date/DayTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DigitalCraftsman\DateTimePrecision\Date;
6+
7+
use DigitalCraftsman\DateTimePrecision\Date;
8+
use DigitalCraftsman\DateTimePrecision\Day;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Date */
12+
final class DayTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*
17+
* @dataProvider dataProviderForDay
18+
*
19+
* @covers ::day
20+
*/
21+
public function day_works(
22+
Day $expectedResult,
23+
Date $date,
24+
): void {
25+
// -- Act & Assert
26+
self::assertEquals($expectedResult, $date->day());
27+
}
28+
29+
/**
30+
* @return array<string, array{
31+
* 0: Day,
32+
* 1: Date,
33+
* }>
34+
*/
35+
public static function dataProviderForDay(): array
36+
{
37+
return [
38+
'first day' => [
39+
new Day(1),
40+
Date::fromString('2022-10-01'),
41+
],
42+
'middle of month' => [
43+
new Day(15),
44+
Date::fromString('2022-10-15'),
45+
],
46+
'last day' => [
47+
new Day(31),
48+
Date::fromString('2022-10-31'),
49+
],
50+
];
51+
}
52+
}

tests/Day/ConstructionTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DigitalCraftsman\DateTimePrecision\Day;
6+
7+
use DigitalCraftsman\DateTimePrecision\Day;
8+
use DigitalCraftsman\DateTimePrecision\Exception\InvalidDay;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Day
13+
*/
14+
final class ConstructionTest extends TestCase
15+
{
16+
/**
17+
* @test
18+
*
19+
* @dataProvider dataProvider
20+
*
21+
* @covers ::fromDateTime
22+
*/
23+
public function from_date_time_works(
24+
Day $expectedResult,
25+
\DateTimeImmutable $dateTime,
26+
): void {
27+
// -- Act & Assert
28+
self::assertEquals($expectedResult, Day::fromDateTime($dateTime));
29+
}
30+
31+
/**
32+
* @return array<string, array{
33+
* 0: Day,
34+
* 1: \DateTimeImmutable,
35+
* }>
36+
*/
37+
public static function dataProvider(): array
38+
{
39+
return [
40+
'first day of month' => [
41+
new Day(1),
42+
new \DateTimeImmutable('2022-10-01 22:15:00'),
43+
],
44+
'middle of month' => [
45+
new Day(15),
46+
new \DateTimeImmutable('2022-10-15 10:00:00'),
47+
],
48+
'last day of month' => [
49+
new Day(31),
50+
new \DateTimeImmutable('2022-10-31 23:59:59'),
51+
],
52+
];
53+
}
54+
55+
/**
56+
* @test
57+
*
58+
* @covers ::__construct
59+
*
60+
* @doesNotPerformAssertions
61+
*/
62+
public function construction_works(): void
63+
{
64+
// -- Act & Assert
65+
new Day(1);
66+
new Day(15);
67+
new Day(31);
68+
}
69+
70+
/**
71+
* @test
72+
*
73+
* @covers ::__construct
74+
*/
75+
public function construction_fails_with_day_too_low(): void
76+
{
77+
// -- Assert
78+
$this->expectException(InvalidDay::class);
79+
80+
// -- Act
81+
new Day(0);
82+
}
83+
84+
/**
85+
* @test
86+
*
87+
* @covers ::__construct
88+
*/
89+
public function construction_fails_with_day_too_high(): void
90+
{
91+
// -- Assert
92+
$this->expectException(InvalidDay::class);
93+
94+
// -- Act
95+
new Day(32);
96+
}
97+
98+
/**
99+
* @test
100+
*
101+
* @covers ::__construct
102+
*/
103+
public function construction_fails_with_negative_day(): void
104+
{
105+
// -- Assert
106+
$this->expectException(InvalidDay::class);
107+
108+
// -- Act
109+
new Day(-1);
110+
}
111+
}

tests/Day/NormalizationTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DigitalCraftsman\DateTimePrecision\Day;
6+
7+
use DigitalCraftsman\DateTimePrecision\Day;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Day */
11+
final class NormalizationTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*
16+
* @covers ::normalize
17+
* @covers ::denormalize
18+
*/
19+
public function normalize_works(): void
20+
{
21+
// -- Arrange
22+
$day = new Day(15);
23+
24+
// -- Act
25+
$normalizedDay = $day->normalize();
26+
$denormalizedDay = Day::denormalize($normalizedDay);
27+
28+
// -- Assert
29+
self::assertEquals($day, $denormalizedDay);
30+
}
31+
}

tests/Days/ConstructionTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DigitalCraftsman\DateTimePrecision\Days;
6+
7+
use DigitalCraftsman\DateTimePrecision\Day;
8+
use DigitalCraftsman\DateTimePrecision\Days;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Days */
12+
final class ConstructionTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*
17+
* @covers ::__construct
18+
*
19+
* @doesNotPerformAssertions
20+
*/
21+
public function construction_works(): void
22+
{
23+
// -- Act & Assert
24+
new Days([
25+
new Day(1),
26+
new Day(15),
27+
]);
28+
}
29+
30+
/**
31+
* @test
32+
*
33+
* @covers ::__construct
34+
*/
35+
public function construction_fails(): void
36+
{
37+
// -- Assert
38+
$this->expectException(\InvalidArgumentException::class);
39+
40+
// -- Act
41+
new Days([
42+
new Day(1),
43+
new Day(1),
44+
new Day(15),
45+
]);
46+
}
47+
}

tests/Days/ContainsTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DigitalCraftsman\DateTimePrecision\Days;
6+
7+
use DigitalCraftsman\DateTimePrecision\Day;
8+
use DigitalCraftsman\DateTimePrecision\Days;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Days */
12+
final class ContainsTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*
17+
* @dataProvider dataProvider
18+
*
19+
* @covers ::contains
20+
*/
21+
public function contain_works(
22+
bool $expectedResult,
23+
Days $days,
24+
Day $comparator,
25+
): void {
26+
// -- Act & Assert
27+
self::assertSame($expectedResult, $days->contains($comparator));
28+
}
29+
30+
/**
31+
* @return array<string, array{
32+
* 0: bool,
33+
* 1: Days,
34+
* 2: Day,
35+
* }>
36+
*/
37+
public static function dataProvider(): array
38+
{
39+
return [
40+
'day is contained' => [
41+
true,
42+
new Days([
43+
new Day(1),
44+
new Day(15),
45+
]),
46+
new Day(1),
47+
],
48+
'day is not contained' => [
49+
false,
50+
new Days([
51+
new Day(1),
52+
new Day(15),
53+
]),
54+
new Day(20),
55+
],
56+
];
57+
}
58+
}

tests/Days/NormalizationTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DigitalCraftsman\DateTimePrecision\Days;
6+
7+
use DigitalCraftsman\DateTimePrecision\Day;
8+
use DigitalCraftsman\DateTimePrecision\Days;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Days */
12+
final class NormalizationTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*
17+
* @covers ::normalize
18+
* @covers ::denormalize
19+
*/
20+
public function normalize_works(): void
21+
{
22+
// -- Arrange
23+
$days = new Days([
24+
new Day(1),
25+
new Day(15),
26+
]);
27+
28+
// -- Act
29+
$normalizedDays = $days->normalize();
30+
$denormalizedDays = Days::denormalize($normalizedDays);
31+
32+
// -- Assert
33+
self::assertEquals($days, $denormalizedDays);
34+
}
35+
}

0 commit comments

Comments
 (0)