|
8 | 8 |
|
9 | 9 | declare(strict_types=1); |
10 | 10 |
|
11 | | -namespace Respect\Stringifier\Test\Stringifiers; |
| 11 | +namespace Respect\Stringifier\Test\Unit\Stringifiers; |
12 | 12 |
|
13 | 13 | use DateTime; |
14 | 14 | use DateTimeImmutable; |
|
17 | 17 | use PHPUnit\Framework\Attributes\DataProvider; |
18 | 18 | use PHPUnit\Framework\Attributes\Test; |
19 | 19 | use PHPUnit\Framework\TestCase; |
20 | | -use Respect\Stringifier\Quoter; |
21 | | -use Respect\Stringifier\Stringifier; |
22 | 20 | use Respect\Stringifier\Stringifiers\DateTimeStringifier; |
| 21 | +use Respect\Stringifier\Test\Double\FakeQuoter; |
23 | 22 |
|
24 | 23 | #[CoversClass(DateTimeStringifier::class)] |
25 | 24 | final class DateTimeStringifierTest extends TestCase |
26 | 25 | { |
| 26 | + private const DEPTH = 0; |
| 27 | + |
27 | 28 | #[Test] |
28 | | - public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void |
| 29 | + public function itShouldNotStringifyRawValueWhenItIsNotInstanceOfDateTimeInterface(): void |
29 | 30 | { |
30 | | - $stringifierMock = $this->createMock(Stringifier::class); |
31 | | - $stringifierMock |
32 | | - ->expects($this->never()) |
33 | | - ->method('stringify'); |
34 | | - |
35 | | - $quoterMock = $this->createMock(Quoter::class); |
36 | | - $quoterMock |
37 | | - ->expects($this->never()) |
38 | | - ->method('quote'); |
39 | | - |
40 | | - $dateTimeStringifier = new DateTimeStringifier($stringifierMock, $quoterMock, 'c'); |
| 31 | + $sut = new DateTimeStringifier(new FakeQuoter(), 'c'); |
41 | 32 |
|
42 | | - self::assertNull($dateTimeStringifier->stringify('NotDateTimeInterface', 0)); |
| 33 | + self::assertNull($sut->stringify('NotDateTimeInterface', self::DEPTH)); |
43 | 34 | } |
44 | 35 |
|
45 | 36 | #[Test] |
46 | | - #[DataProvider('validValuesProvider')] |
47 | | - public function shouldConvertDateTimeInterfaceToString( |
| 37 | + #[DataProvider('stringableRawValuesProvider')] |
| 38 | + public function itShouldStringifyRawValueWhenItIsInstanceOfDateTimeInterface( |
48 | 39 | DateTimeInterface $raw, |
49 | 40 | string $format, |
50 | | - string $expected |
| 41 | + string $string |
51 | 42 | ): void { |
52 | | - $depth = 0; |
53 | | - |
54 | | - $formattedDateTime = $raw->format($format); |
55 | | - |
56 | | - $stringifierMock = $this->createMock(Stringifier::class); |
57 | | - $stringifierMock |
58 | | - ->expects($this->once()) |
59 | | - ->method('stringify') |
60 | | - ->with($formattedDateTime, $depth + 1) |
61 | | - ->willReturn($formattedDateTime); |
| 43 | + $quoter = new FakeQuoter(); |
62 | 44 |
|
63 | | - $quoterMock = $this->createMock(Quoter::class); |
64 | | - $quoterMock |
65 | | - ->expects($this->once()) |
66 | | - ->method('quote') |
67 | | - ->with($expected) |
68 | | - ->willReturn($expected); |
| 45 | + $sut = new DateTimeStringifier($quoter, $format); |
69 | 46 |
|
70 | | - $dateTimeStringifier = new DateTimeStringifier($stringifierMock, $quoterMock, $format); |
| 47 | + $actual = $sut->stringify($raw, self::DEPTH); |
| 48 | + $expected = $quoter->quote($string, self::DEPTH); |
71 | 49 |
|
72 | | - self::assertSame($expected, $dateTimeStringifier->stringify($raw, $depth)); |
| 50 | + self::assertSame($expected, $actual); |
73 | 51 | } |
74 | 52 |
|
75 | 53 | /** |
76 | | - * @return mixed[][] |
| 54 | + * @return array<int, array{0: DateTimeInterface, 1: string, 2: string}> |
77 | 55 | */ |
78 | | - public static function validValuesProvider(): array |
| 56 | + public static function stringableRawValuesProvider(): array |
79 | 57 | { |
80 | 58 | $dateTime = new DateTime('2017-12-31T23:59:59+00:00'); |
81 | 59 | $dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime); |
82 | 60 |
|
83 | 61 | return [ |
84 | | - [$dateTime, 'd/m/Y', '[date-time] (DateTime: 31/12/2017)'], |
85 | | - [$dateTime, 'c', '[date-time] (DateTime: 2017-12-31T23:59:59+00:00)'], |
86 | | - [$dateTimeImmutable, 'Y-m-d H:i:s', '[date-time] (DateTimeImmutable: 2017-12-31 23:59:59)'], |
| 62 | + [$dateTime, 'd/m/Y', 'DateTime { 31/12/2017 }'], |
| 63 | + [$dateTime, 'c', 'DateTime { 2017-12-31T23:59:59+00:00 }'], |
| 64 | + [$dateTime, DateTimeInterface::ATOM, 'DateTime { 2017-12-31T23:59:59+00:00 }'], |
| 65 | + [$dateTimeImmutable, 'Y-m-d H:i:s', 'DateTimeImmutable { 2017-12-31 23:59:59 }'], |
87 | 66 | ]; |
88 | 67 | } |
89 | 68 | } |
0 commit comments