|
| 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 ArrayIterator; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Respect\Stringifier\Quoter; |
| 19 | +use Respect\Stringifier\Stringifier; |
| 20 | +use Respect\Stringifier\Stringifiers\TraversableStringifier; |
| 21 | + |
| 22 | +/** |
| 23 | + * @covers \Respect\Stringifier\Stringifiers\TraversableStringifier |
| 24 | + * |
| 25 | + * @author Henrique Moody <[email protected]> |
| 26 | + */ |
| 27 | +final class TraversableStringifierTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @test |
| 31 | + */ |
| 32 | + public function shouldConvertToStringWhenValueIsTraversable(): void |
| 33 | + { |
| 34 | + $raw = new ArrayIterator([1, 2, 3]); |
| 35 | + $depth = 0; |
| 36 | + |
| 37 | + $stringifiedData = '-stringified-'; |
| 38 | + |
| 39 | + $expectedValue = '[traversable] (ArrayIterator: '.$stringifiedData.')'; |
| 40 | + |
| 41 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 42 | + $stringifierMock |
| 43 | + ->expects($this->once()) |
| 44 | + ->method('stringify') |
| 45 | + ->with($raw->getArrayCopy(), $depth + 1) |
| 46 | + ->willReturn($stringifiedData); |
| 47 | + |
| 48 | + $quoterMock = $this->createMock(Quoter::class); |
| 49 | + $quoterMock |
| 50 | + ->expects($this->once()) |
| 51 | + ->method('quote') |
| 52 | + ->with($expectedValue, $depth) |
| 53 | + ->willReturn($expectedValue); |
| 54 | + |
| 55 | + $traversableStringifier = new TraversableStringifier($stringifierMock, $quoterMock); |
| 56 | + |
| 57 | + self::assertSame($expectedValue, $traversableStringifier->stringify($raw, $depth)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @test |
| 62 | + */ |
| 63 | + public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void |
| 64 | + { |
| 65 | + $raw = [1, 2, 3, 4]; |
| 66 | + $depth = 123; |
| 67 | + |
| 68 | + $stringifierMock = $this->createMock(Stringifier::class); |
| 69 | + $stringifierMock |
| 70 | + ->expects($this->never()) |
| 71 | + ->method('stringify'); |
| 72 | + |
| 73 | + $quoterMock = $this->createMock(Quoter::class); |
| 74 | + $quoterMock |
| 75 | + ->expects($this->never()) |
| 76 | + ->method('quote'); |
| 77 | + |
| 78 | + $traversableStringifier = new TraversableStringifier($stringifierMock, $quoterMock); |
| 79 | + |
| 80 | + self::assertNull($traversableStringifier->stringify($raw, $depth)); |
| 81 | + } |
| 82 | +} |
0 commit comments