engelsystem/tests/Unit/Helpers/CarbonTest.php

93 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2022-04-13 01:02:37 +02:00
<?php
declare(strict_types=1);
namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Helpers\Carbon;
use PHPUnit\Framework\TestCase;
use Traversable;
class CarbonTest extends TestCase
{
public function validDates(): Traversable
{
$format = '!Y-m-d H:i';
yield '2022-04-16T10:44' => ['2022-04-16T10:44', Carbon::createFromFormat($format, '2022-04-16 10:44')];
yield '2022-04-16 10:44' => ['2022-04-16T10:44', Carbon::createFromFormat($format, '2022-04-16 10:44')];
yield '2020-12-24T13:37' => ['2020-12-24T13:37', Carbon::createFromFormat($format, '2020-12-24 13:37')];
yield '2020-12-24 13:37' => ['2020-12-24T13:37', Carbon::createFromFormat($format, '2020-12-24 13:37')];
}
public function invalidDates(): Traversable
{
yield '202-12-12 11:11' => ['202-12-12T11:11'];
yield '2022-23-24' => ['2022-23-24'];
yield '16.04.2022 11:24' => ['16.04.2022 11:24'];
}
/**
* @covers \Engelsystem\Helpers\Carbon::createFromDatetime
* @dataProvider validDates
*/
public function testCreateFromValidDatetime(string $value, Carbon $expected): void
{
$date = Carbon::createFromDatetime($value);
self::assertSame($expected->timestamp, $date->timestamp);
}
/**
* @covers \Engelsystem\Helpers\Carbon::createFromDatetime
* @dataProvider invalidDates
*/
public function testCreateFromInvalidDatetime(string $value): void
{
$date = Carbon::createFromDatetime($value);
self::assertNull($date);
}
/**
* @covers \Engelsystem\Helpers\Carbon::createTimestampFromDatetime
* @dataProvider validDates
*/
public function testCreateTimestampFromValidDatetime(string $value, Carbon $expected): void
{
$timestamp = Carbon::createTimestampFromDatetime($value);
self::assertSame($expected->timestamp, $timestamp);
}
/**
* @covers \Engelsystem\Helpers\Carbon::createTimestampFromDatetime
* @dataProvider invalidDates
*/
public function testCreateTimestampFromInvalidDatetime(string $value): void
{
$timestamp = Carbon::createTimestampFromDatetime($value);
self::assertNull($timestamp);
}
2023-08-27 19:40:33 +02:00
public function startOfHourDates(): array
{
return [
['2022-04-16 10:00:00.000000', true],
['2022-04-16 10:00:00.000000', true, true],
['2022-04-16 10:00:00.123456', true, false],
['2022-04-16 23:00:00.000000', true, false],
['2022-04-16 10:00:42.000000', false],
['2022-04-16 10:23:00.000000', false],
['2022-04-16 10:00:00.123456', false, true],
];
}
/**
* @covers \Engelsystem\Helpers\Carbon::isStartOfHour
* @dataProvider startOfHourDates
*/
public function testIsStartOfHour(string $value, bool $expected, bool $checkMicroseconds = false): void
{
$date = Carbon::createFromFormat('Y-m-d H:i:s.u', $value);
$this->assertEquals($expected, $date->isStartOfHour($checkMicroseconds));
}
2022-04-13 01:02:37 +02:00
}