refactor(DatesHelper): make constructor private and update comment
Updated the constructor of DatesHelper to be private and modified the comment to clarify its purpose as a static class.
This commit is contained in:
parent
d15f30bcbf
commit
289140c630
2 changed files with 46 additions and 41 deletions
|
|
@ -17,9 +17,9 @@ final class DatesHelper
|
|||
|
||||
private const string RFC3339_REGEX = '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+\-]\d{2}:\d{2})$/';
|
||||
|
||||
public function __construct()
|
||||
private function __construct()
|
||||
{
|
||||
throw new \LogicException('Static class');
|
||||
// Static class
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -4,66 +4,71 @@ declare(strict_types=1);
|
|||
|
||||
namespace Tests\Integration\Shared\Helpers;
|
||||
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Shared\Helpers\DatesHelper;
|
||||
|
||||
covers(DatesHelper::class);
|
||||
|
||||
afterEach(function () {
|
||||
\Mockery::close();
|
||||
Date::clearResolvedInstances();
|
||||
afterEach(function (): void {
|
||||
CarbonImmutable::setTestNow();
|
||||
});
|
||||
|
||||
describe('Date Helper', function (): void {
|
||||
it('can parse a date time', function (): void {
|
||||
// GIVEN
|
||||
$date = '2025-12-25 12:10:23';
|
||||
describe('Date Helper Integration Tests', function (): void {
|
||||
|
||||
// WHEN
|
||||
$result = DatesHelper::parseDateTime($date);
|
||||
it('can parse various date formats', function (string $input, string $expectedFormat): void {
|
||||
// GIVEN & WHEN
|
||||
$result = DatesHelper::parseDateTime($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(\DateTimeInterface::class)
|
||||
->and($result->format('Y-m-d'))->toBe('2025-12-25')
|
||||
->and($result->format('H:i:s'))->toBe('12:10:23');
|
||||
->and($result->format('Y-m-d'))->toBe($expectedFormat);
|
||||
})->with([
|
||||
['2025-12-25 12:10:23', '2025-12-25'],
|
||||
['2025-12-25', '2025-12-25'],
|
||||
['25-12-2025', '2025-12-25'],
|
||||
['2025-12-25T12:10:23Z', '2025-12-25'],
|
||||
['2025-12-25T12:10:23+02:00', '2025-12-25'],
|
||||
['2025-12-25T12:10:23.123456Z', '2025-12-25'],
|
||||
]);
|
||||
|
||||
it('throws InvalidArgumentException for unparseable date strings', function (): void {
|
||||
// GIVEN
|
||||
$invalidDate = 'not-a-real-date-string';
|
||||
|
||||
// WHEN & THEN
|
||||
expect(fn () => DatesHelper::parseDateTime($invalidDate))
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('can verify a date is in between', function (): void {
|
||||
it('handles isIn date boundary checks correctly', function (): void {
|
||||
// GIVEN
|
||||
$now = Date::create(2025, 12, 25, 12, 0, 0);
|
||||
$from = Date::createFromDate(2025, 12, 24);
|
||||
$to = Date::createFromDate(2025, 12, 26);
|
||||
$now = CarbonImmutable::create(2025, 6, 15, 12, 0, 0);
|
||||
CarbonImmutable::setTestNow($now);
|
||||
|
||||
// WHEN
|
||||
Date::setTestNow($now);
|
||||
$result = DatesHelper::isIn($from, $to);
|
||||
$past = CarbonImmutable::create(2025, 1, 1);
|
||||
$future = CarbonImmutable::create(2025, 12, 31);
|
||||
$farFuture = CarbonImmutable::create(2025, 12, 30);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeTruthy();
|
||||
// WHEN & THEN
|
||||
expect(DatesHelper::isIn($past, $future))->toBeTrue()
|
||||
->and(DatesHelper::isIn($farFuture, $future))->toBeFalse()
|
||||
->and(DatesHelper::isIn($past, null))->toBeTrue()
|
||||
->and(DatesHelper::isIn($future, null))->toBeFalse()
|
||||
->and(DatesHelper::isIn(null, $future))->toBeTrue()
|
||||
->and(DatesHelper::isIn(null, $past))->toBeFalse()
|
||||
->and(DatesHelper::isIn(null, null))->toBeFalse();
|
||||
});
|
||||
|
||||
it('can verify a date is not in between', function (): void {
|
||||
it('formats string or DateTimeInterface to RFC3339', function (): void {
|
||||
// GIVEN
|
||||
$now = Date::create(2025, 12, 25, 12, 0, 0);
|
||||
$from = Date::createFromDate(2025, 12, 26);
|
||||
$to = Date::createFromDate(2025, 12, 27);
|
||||
$dateTimeObj = new \DateTimeImmutable('2025-12-25 12:10:23');
|
||||
|
||||
// WHEN
|
||||
Date::setTestNow($now);
|
||||
$result = DatesHelper::isIn($from, $to);
|
||||
$resultFromObj = DatesHelper::toRfc3339($dateTimeObj);
|
||||
$resultFromStr = DatesHelper::toRfc3339('2025-12-25 12:10:23');
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeFalse();
|
||||
});
|
||||
|
||||
it('can format a date time to RFC3339', function (): void {
|
||||
// GIVEN
|
||||
$date = '2025-12-25 12:10:23';
|
||||
|
||||
// WHEN
|
||||
$result = DatesHelper::toRfc3339($date);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe('2025-12-25T12:10:23+00:00');
|
||||
expect($resultFromObj)->toContain('2025-12-25T12:10:23')
|
||||
->and($resultFromStr)->toContain('2025-12-25T12:10:23');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue