|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DontTest\Exception; |
| 6 | + |
| 7 | +use Dont\Exception\ExceptionInterface; |
| 8 | +use Dont\Exception\NonCloneableObject; |
| 9 | +use Dont\Exception\NonStringableObject; |
| 10 | +use Dont\Exception\TypeError; |
| 11 | +use LogicException; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use stdClass; |
| 14 | + |
| 15 | +/** |
| 16 | + * @covers \Dont\Exception\NonStringableObject |
| 17 | + */ |
| 18 | +final class NonStringableObjectTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider objectProvider |
| 22 | + * |
| 23 | + * @param object $object |
| 24 | + */ |
| 25 | + public function testFromAttemptedCloning($object) : void |
| 26 | + { |
| 27 | + $exception = NonStringableObject::fromAttemptedToString($object); |
| 28 | + |
| 29 | + self::assertInstanceOf(NonStringableObject::class, $exception); |
| 30 | + self::assertInstanceOf(LogicException::class, $exception); |
| 31 | + self::assertInstanceOf(ExceptionInterface::class, $exception); |
| 32 | + |
| 33 | + $expected = 'The given object ' . get_class($object) |
| 34 | + . '#' . spl_object_hash($object) . " is not designed to be stringable.\n\n" |
| 35 | + . "You tried to access a method called \"__toString()\"."; |
| 36 | + |
| 37 | + self::assertSame($expected, $exception->getMessage()); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return object[][] |
| 42 | + */ |
| 43 | + public function objectProvider() : array |
| 44 | + { |
| 45 | + return [ |
| 46 | + [new stdClass()], |
| 47 | + [$this], |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider nonObjectProvider |
| 53 | + * |
| 54 | + * @param mixed $nonObject |
| 55 | + */ |
| 56 | + public function testWillThrowOnNonObject($nonObject) : void |
| 57 | + { |
| 58 | + $this->expectException(TypeError::class); |
| 59 | + |
| 60 | + NonStringableObject::fromAttemptedToString($nonObject); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return mixed[][] |
| 65 | + */ |
| 66 | + public function nonObjectProvider() : array |
| 67 | + { |
| 68 | + return [ |
| 69 | + [null], |
| 70 | + [true], |
| 71 | + [123], |
| 72 | + [12.3], |
| 73 | + ['foo'], |
| 74 | + [[]], |
| 75 | + [STDERR], |
| 76 | + ]; |
| 77 | + } |
| 78 | +} |
0 commit comments