1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace DontTest \Exception ;
6+
7+ use Dont \Exception \ExceptionInterface ;
8+ use Dont \Exception \NonCallableObject ;
9+ use Dont \Exception \TypeError ;
10+ use LogicException ;
11+ use PHPUnit \Framework \TestCase ;
12+ use stdClass ;
13+
14+ /**
15+ * @covers \Dont\Exception\NonCallableObject
16+ */
17+ final class NonCallableObjectTest extends TestCase
18+ {
19+ /**
20+ * @dataProvider objectProvider
21+ *
22+ * @param object $object
23+ */
24+ public function testFromAttemptedCall ($ object ) : void
25+ {
26+ $ exception = NonCallableObject::fromAttemptedCall ($ object , 'methodName ' );
27+
28+ self ::assertInstanceOf (NonCallableObject::class, $ exception );
29+ self ::assertInstanceOf (LogicException::class, $ exception );
30+ self ::assertInstanceOf (ExceptionInterface::class, $ exception );
31+
32+ $ expected = 'The given object ' . get_class ($ object )
33+ . '# ' . spl_object_hash ($ object ) . " is not designed to allow any undefined or inaccessible methods to be called. \n\n"
34+ . 'You tried to call a method called "methodName". ' . "\n\n"
35+ . 'Perhaps you made a typo in the method name, or tried to call an inaccessible method? ' ;
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+ NonCallableObject::fromAttemptedCall ($ nonObject , 'propertyName ' );
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+
79+ }
0 commit comments