|
| 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 JsonSerializable; |
| 17 | +use Respect\Stringifier\Quoter; |
| 18 | +use Respect\Stringifier\Stringifier; |
| 19 | +use Respect\Stringifier\Stringifiers\JsonSerializableStringifier; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use stdClass; |
| 22 | + |
| 23 | +/** |
| 24 | + * @covers \Respect\Stringifier\Stringifiers\JsonSerializableStringifier |
| 25 | + * |
| 26 | + * @author Henrique Moody <[email protected]> |
| 27 | + */ |
| 28 | +final class JsonSerializableStringifierTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @test |
| 32 | + */ |
| 33 | + public function shouldConvertToStringWhenRawValueIsAJsonSerializableObject(): void |
| 34 | + { |
| 35 | + $raw = new JsonSerializableObject(); |
| 36 | + $depth = 0; |
| 37 | + |
| 38 | + $stringifiedData = '-stringified-'; |
| 39 | + |
| 40 | + $expectedValue = '[json-serializable] ('.JsonSerializableObject::class.': '.$stringifiedData.')'; |
| 41 | + |
| 42 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 43 | + $stringifierMock |
| 44 | + ->expects($this->once()) |
| 45 | + ->method('stringify') |
| 46 | + ->with(JsonSerializableObject::JSON_VALUE, $depth + 1) |
| 47 | + ->willReturn($stringifiedData); |
| 48 | + |
| 49 | + $quoterMock = $this->createMock(Quoter::class); |
| 50 | + $quoterMock |
| 51 | + ->expects($this->once()) |
| 52 | + ->method('quote') |
| 53 | + ->with($expectedValue, $depth) |
| 54 | + ->willReturn($expectedValue); |
| 55 | + |
| 56 | + $jsonSerializableStringifier = new JsonSerializableStringifier($stringifierMock, $quoterMock); |
| 57 | + |
| 58 | + self::assertSame($expectedValue, $jsonSerializableStringifier->stringify($raw, $depth)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @test |
| 63 | + */ |
| 64 | + public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void |
| 65 | + { |
| 66 | + $raw = new stdClass(); |
| 67 | + $depth = 1; |
| 68 | + |
| 69 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 70 | + $stringifierMock |
| 71 | + ->expects($this->never()) |
| 72 | + ->method('stringify'); |
| 73 | + |
| 74 | + $quoterMock = $this->createMock(Quoter::class); |
| 75 | + $quoterMock |
| 76 | + ->expects($this->never()) |
| 77 | + ->method('quote'); |
| 78 | + |
| 79 | + $jsonSerializableStringifier = new JsonSerializableStringifier($stringifierMock, $quoterMock); |
| 80 | + |
| 81 | + self::assertNull($jsonSerializableStringifier->stringify($raw, $depth)); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +final class JsonSerializableObject implements JsonSerializable |
| 86 | +{ |
| 87 | + public const JSON_VALUE = [1, true, 'foo', null]; |
| 88 | + |
| 89 | + public function jsonSerialize(): array |
| 90 | + { |
| 91 | + return self::JSON_VALUE; |
| 92 | + } |
| 93 | +} |
0 commit comments