|
| 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 Respect\Stringifier\Stringifier; |
| 17 | +use Respect\Stringifier\Stringifiers\StringableObjectStringifier; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use stdClass; |
| 20 | + |
| 21 | +/** |
| 22 | + * @covers \Respect\Stringifier\Stringifiers\StringableObjectStringifier |
| 23 | + * |
| 24 | + * @author Henrique Moody <[email protected]> |
| 25 | + */ |
| 26 | +final class StringableObjectStringifierTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @test |
| 30 | + */ |
| 31 | + public function shouldNotConvertToStringWhenValueIsNotAnObject(): void |
| 32 | + { |
| 33 | + $raw = 'not-an-object'; |
| 34 | + $depth = 1; |
| 35 | + |
| 36 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 37 | + $stringifierMock |
| 38 | + ->expects($this->never()) |
| 39 | + ->method('stringify'); |
| 40 | + |
| 41 | + $stringableObjectStringifier = new StringableObjectStringifier($stringifierMock); |
| 42 | + |
| 43 | + self::assertNull($stringableObjectStringifier->stringify($raw, $depth)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @test |
| 48 | + */ |
| 49 | + public function shouldNotConvertToStringWhenValueIsANonStringableObject(): void |
| 50 | + { |
| 51 | + $raw = new stdClass(); |
| 52 | + $depth = 1; |
| 53 | + |
| 54 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 55 | + $stringifierMock |
| 56 | + ->expects($this->never()) |
| 57 | + ->method('stringify'); |
| 58 | + |
| 59 | + $stringableObjectStringifier = new StringableObjectStringifier($stringifierMock); |
| 60 | + |
| 61 | + self::assertNull($stringableObjectStringifier->stringify($raw, $depth)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @test |
| 66 | + */ |
| 67 | + public function shouldConvertToStringWhenValueIsAnStringableObject(): void |
| 68 | + { |
| 69 | + $raw = new StringableObject(); |
| 70 | + $depth = 0; |
| 71 | + |
| 72 | + $expectedValue = StringableObject::STRING_VALUE; |
| 73 | + |
| 74 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 75 | + $stringifierMock |
| 76 | + ->expects($this->once()) |
| 77 | + ->method('stringify') |
| 78 | + ->with(StringableObject::STRING_VALUE, $depth) |
| 79 | + ->willReturn($expectedValue); |
| 80 | + |
| 81 | + $stringableObjectStringifier = new StringableObjectStringifier($stringifierMock); |
| 82 | + |
| 83 | + self::assertSame($expectedValue, $stringableObjectStringifier->stringify($raw, $depth)); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +final class StringableObject |
| 88 | +{ |
| 89 | + public const STRING_VALUE = 'String'; |
| 90 | + |
| 91 | + public function __toString() |
| 92 | + { |
| 93 | + return self::STRING_VALUE; |
| 94 | + } |
| 95 | +} |
0 commit comments