Skip to content

Commit 526498e

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

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

app/Providers/Foo.php

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

99
final class Foo
1010
{
11-
public static function dump(): string
11+
public static function getDateTime(?string $format = 'Y-m-d H:i:s'): string
1212
{
13-
$date = (new DateTimeImmutable())->format('Y-m-d H:i:s');
14-
15-
$line = 'Executed method [ ' . __FUNCTION__ . ' ] in [ ' . getenv('ENV') . ' ] mode' . PHP_EOL;
16-
17-
return sprintf('[%s] %s: %s', $date, self::class, $line);
13+
return (new DateTimeImmutable())->format($format);
1814
}
1915

2016
public function ping(): string

tests/Unit/Providers/FooTest.php

Lines changed: 14 additions & 11 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 DateTimeImmutable;
89
use PHPUnit\Framework\Attributes\CoversClass;
910
use PHPUnit\Framework\Attributes\DataProvider;
1011
use PHPUnit\Framework\Attributes\Test;
@@ -16,7 +17,7 @@ final class FooTest extends TestCase
1617
{
1718
protected function setUp(): void
1819
{
19-
ClockMock::freeze(new \DateTime('2023-01-01 00:00:00'));
20+
ClockMock::freeze(new \DateTime('2024-01-01 00:00:00'));
2021
}
2122

2223
protected function tearDown(): void
@@ -25,32 +26,34 @@ protected function tearDown(): void
2526
}
2627

2728
#[Test]
28-
#[DataProvider('dataProviderForDump')]
29-
public function checkInvokeMethod(string $expectedLog): void
29+
#[DataProvider('dataProviderForGetDateTime')]
30+
public function checkGetDateTime(string $format): void
3031
{
31-
self::assertEquals($expectedLog, Foo::dump());
32+
$expectedCurrentDateTime = (new DateTimeImmutable())->format($format);
33+
34+
self::assertEquals($expectedCurrentDateTime, Foo::getDateTime($format));
3235
}
3336

3437
/**
3538
* @return array<string, array<int, string>>
3639
*/
37-
public static function dataProviderForDump(): array
40+
public static function dataProviderForGetDateTime(): array
3841
{
3942
return [
40-
'[DEFAULT CASE]' => [
41-
'[2023-01-01 00:00:00] App\Providers\Foo: Executed method [ dump ] in [ DEVELOPMENT ] mode' . PHP_EOL,
42-
],
43+
'[CURRENT DATE TIME]' => ['Y-m-d H:i:s'],
44+
'[CURRENT DATE]' => ['Y-m-d'],
45+
'[CURRENT TIME]' => ['H:i:s'],
4346
];
4447
}
4548

4649
#[Test]
47-
public function checkMockFinal(): void
50+
public function checkMockFinalClass(): void
4851
{
4952
self::assertEquals('pong', (new Foo())->ping());
5053

5154
$mock = $this->createMock(Foo::class);
52-
$mock->method('ping')->willReturn('pong pong');
55+
$mock->expects(self::once())->method('ping')->willReturn('knock knock');
5356

54-
self::assertEquals('pong pong', $mock->ping());
57+
self::assertEquals('knock knock', $mock->ping());
5558
}
5659
}

0 commit comments

Comments
 (0)