Skip to content

Commit 64a5e74

Browse files
author
Alcides Ramos
committed
tests: allow to mock final classes
1 parent 526498e commit 64a5e74

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

app/Providers/Foo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
final class Foo
1010
{
11-
public static function getDateTime(?string $format = 'Y-m-d H:i:s'): string
11+
public static function getDateTime(?string $format): string
1212
{
13-
return (new DateTimeImmutable())->format($format);
13+
return (new DateTimeImmutable())->format($format ?? 'Y-m-d H:i:s');
1414
}
1515

1616
public function ping(): string

tests/Unit/Providers/FooTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace UnitTests\Providers;
66

77
use App\Providers\Foo;
8+
use DateTime;
89
use DateTimeImmutable;
910
use PHPUnit\Framework\Attributes\CoversClass;
1011
use PHPUnit\Framework\Attributes\DataProvider;
@@ -17,7 +18,7 @@ final class FooTest extends TestCase
1718
{
1819
protected function setUp(): void
1920
{
20-
ClockMock::freeze(new \DateTime('2024-01-01 00:00:00'));
21+
ClockMock::freeze(new DateTime('2024-01-01 00:00:00'));
2122
}
2223

2324
protected function tearDown(): void
@@ -29,20 +30,22 @@ protected function tearDown(): void
2930
#[DataProvider('dataProviderForGetDateTime')]
3031
public function checkGetDateTime(string $format): void
3132
{
32-
$expectedCurrentDateTime = (new DateTimeImmutable())->format($format);
33+
$datetime = new DateTimeImmutable();
3334

34-
self::assertEquals($expectedCurrentDateTime, Foo::getDateTime($format));
35+
self::assertEquals($datetime->format($format), Foo::getDateTime($format));
3536
}
3637

3738
/**
3839
* @return array<string, array<int, string>>
3940
*/
4041
public static function dataProviderForGetDateTime(): array
4142
{
43+
$datetime = new DateTimeImmutable();
44+
4245
return [
43-
'[CURRENT DATE TIME]' => ['Y-m-d H:i:s'],
44-
'[CURRENT DATE]' => ['Y-m-d'],
45-
'[CURRENT TIME]' => ['H:i:s'],
46+
'[CURRENT DATE TIME]' => [$datetime->format('Y-m-d H:i:s')],
47+
'[CURRENT DATE]' => [$datetime->format('Y-m-d')],
48+
'[CURRENT TIME]' => [$datetime->format('H:i:s')],
4649
];
4750
}
4851

0 commit comments

Comments
 (0)