1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Tests\Innmind\Reflection\ExtractionStrategy; |
||
5 | |||
6 | use Innmind\Reflection\{ |
||
7 | ExtractionStrategy\HasserStrategy, |
||
8 | ExtractionStrategy, |
||
9 | Exception\LogicException, |
||
10 | }; |
||
11 | use Fixtures\Innmind\Reflection\Foo; |
||
12 | use PHPUnit\Framework\TestCase; |
||
13 | |||
14 | class HasserStrategyTest extends TestCase |
||
15 | { |
||
16 | public function testInterface() |
||
17 | { |
||
18 | $this->assertInstanceOf( |
||
19 | ExtractionStrategy::class, |
||
20 | new HasserStrategy |
||
21 | ); |
||
22 | } |
||
23 | |||
24 | public function testSupports() |
||
25 | { |
||
26 | $o = new class { |
||
27 | public $c; |
||
28 | |||
29 | public function hasA() |
||
30 | { |
||
31 | } |
||
32 | |||
33 | public function hasB($b) |
||
0 ignored issues
–
show
|
|||
34 | { |
||
35 | } |
||
36 | |||
37 | public function hasSomeLongProperty() |
||
38 | { |
||
39 | } |
||
40 | |||
41 | private function hasFoo() |
||
42 | { |
||
43 | } |
||
44 | |||
45 | protected function hasBar() |
||
46 | { |
||
47 | } |
||
48 | }; |
||
49 | |||
50 | $s = new HasserStrategy; |
||
51 | |||
52 | $this->assertTrue($s->supports($o, 'a')); |
||
53 | $this->assertTrue($s->supports($o, 'some_long_property')); |
||
54 | $this->assertFalse($s->supports($o, 'b')); |
||
55 | $this->assertFalse($s->supports($o, 'c')); |
||
56 | $this->assertFalse($s->supports($o, 'foo')); |
||
57 | $this->assertFalse($s->supports($o, 'bar')); |
||
58 | } |
||
59 | |||
60 | public function testThrowWhenExtractingUnsuppportedProperty() |
||
61 | { |
||
62 | $o = new \stdClass; |
||
63 | $s = new HasserStrategy; |
||
64 | |||
65 | $this->expectException(LogicException::class); |
||
66 | |||
67 | $s->extract($o, 'a'); |
||
68 | } |
||
69 | |||
70 | public function testExtract() |
||
71 | { |
||
72 | $o = new class { |
||
73 | public function hasA() |
||
74 | { |
||
75 | return true; |
||
76 | } |
||
77 | |||
78 | public function hasSomeLongProperty() |
||
79 | { |
||
80 | return true; |
||
81 | } |
||
82 | }; |
||
83 | $s = new HasserStrategy; |
||
84 | |||
85 | $this->assertTrue($s->extract($o, 'a')); |
||
86 | $this->assertTrue($s->extract($o, 'some_long_property')); |
||
87 | } |
||
88 | |||
89 | public function testExtractWithInheritedMethod() |
||
90 | { |
||
91 | $strategy = new HasserStrategy; |
||
92 | $object = new class extends Foo {}; |
||
93 | |||
94 | $this->assertTrue($strategy->extract($object, 'someProperty')); |
||
95 | } |
||
96 | } |
||
97 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.