|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Respect/Stringifier. |
| 5 | + * |
| 6 | + * (c) Henrique Moody <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the "LICENSE.md" |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Respect\Stringifier\Test\Stringifiers; |
| 15 | + |
| 16 | +use DateTime; |
| 17 | +use DateTimeImmutable; |
| 18 | +use DateTimeInterface; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Respect\Stringifier\Stringifier; |
| 21 | +use Respect\Stringifier\Stringifiers\DateTimeStringifier; |
| 22 | + |
| 23 | +/** |
| 24 | + * @covers \Respect\Stringifier\Stringifiers\DateTimeStringifier |
| 25 | + * |
| 26 | + * @author Henrique Moody <[email protected]> |
| 27 | + */ |
| 28 | +final class DateTimeStringifierTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @test |
| 32 | + */ |
| 33 | + public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void |
| 34 | + { |
| 35 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 36 | + $stringifierMock |
| 37 | + ->expects($this->never()) |
| 38 | + ->method('stringify'); |
| 39 | + |
| 40 | + $dateTimeStringifier = new DateTimeStringifier($stringifierMock, 'c'); |
| 41 | + |
| 42 | + self::assertNull($dateTimeStringifier->stringify('NotDateTimeInterface', 0)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @test |
| 47 | + * |
| 48 | + * @dataProvider validValuesProvider |
| 49 | + * |
| 50 | + * @param DateTimeInterface $raw |
| 51 | + * @param string $format |
| 52 | + * @param string $expectedValue |
| 53 | + */ |
| 54 | + public function shouldConvertDateTimeInterfaceToString( |
| 55 | + DateTimeInterface $raw, |
| 56 | + string $format, |
| 57 | + string $expectedValue |
| 58 | + ): void { |
| 59 | + $depth = 0; |
| 60 | + |
| 61 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 62 | + $stringifierMock |
| 63 | + ->expects($this->once()) |
| 64 | + ->method('stringify') |
| 65 | + ->with($expectedValue, $depth) |
| 66 | + ->willReturn($expectedValue); |
| 67 | + |
| 68 | + $dateTimeStringifier = new DateTimeStringifier($stringifierMock, $format); |
| 69 | + |
| 70 | + self::assertSame($expectedValue, $dateTimeStringifier->stringify($raw, $depth)); |
| 71 | + } |
| 72 | + |
| 73 | + public function validValuesProvider(): array |
| 74 | + { |
| 75 | + $dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2017-12-31T23:59:59+00:00'); |
| 76 | + $dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime); |
| 77 | + |
| 78 | + return [ |
| 79 | + [$dateTime, 'd/m/Y', '31/12/2017'], |
| 80 | + [$dateTime, 'c', '2017-12-31T23:59:59+00:00'], |
| 81 | + [$dateTimeImmutable, 'Y-m-d H:i:s', '2017-12-31 23:59:59'], |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments