Innmind /
Reflection
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Tests\Innmind\Reflection\InjectionStrategy; |
||
| 5 | |||
| 6 | use Innmind\Reflection\{ |
||
| 7 | InjectionStrategy\SetterStrategy, |
||
| 8 | InjectionStrategy, |
||
| 9 | Exception\LogicException, |
||
| 10 | }; |
||
| 11 | use Fixtures\Innmind\Reflection\Foo; |
||
| 12 | use PHPUnit\Framework\TestCase; |
||
| 13 | |||
| 14 | class SetterStrategyTest extends TestCase |
||
| 15 | { |
||
| 16 | public function testInterface() |
||
| 17 | { |
||
| 18 | $this->assertInstanceOf( |
||
| 19 | InjectionStrategy::class, |
||
| 20 | new SetterStrategy |
||
| 21 | ); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testSupports() |
||
| 25 | { |
||
| 26 | $s = new SetterStrategy; |
||
| 27 | $o = new class { |
||
| 28 | private $a; |
||
| 29 | private $b; |
||
| 30 | |||
| 31 | public function setB($b) |
||
| 32 | { |
||
| 33 | $this->b = $b; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function setSomeLongProperty($foo) |
||
|
0 ignored issues
–
show
|
|||
| 37 | { |
||
| 38 | } |
||
| 39 | |||
| 40 | private function setFoo($foo) |
||
| 41 | { |
||
| 42 | } |
||
| 43 | |||
| 44 | private function setBar($bar) |
||
| 45 | { |
||
| 46 | } |
||
| 47 | }; |
||
| 48 | |||
| 49 | $this->assertTrue($s->supports($o, 'b', null)); |
||
| 50 | $this->assertTrue($s->supports($o, 'some_long_property', null)); |
||
| 51 | $this->assertFalse($s->supports($o, 'a', null)); |
||
| 52 | $this->assertFalse($s->supports($o, 'foo', null)); |
||
| 53 | $this->assertFalse($s->supports($o, 'bar', null)); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testInject() |
||
| 57 | { |
||
| 58 | $s = new SetterStrategy; |
||
| 59 | $o = new class { |
||
| 60 | private $b; |
||
| 61 | private $foo; |
||
| 62 | |||
| 63 | public function setB($b) |
||
| 64 | { |
||
| 65 | $this->b = $b; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getB() |
||
| 69 | { |
||
| 70 | return $this->b; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function setSomeLongProperty($foo) |
||
| 74 | { |
||
| 75 | $this->foo = $foo; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getFoo() |
||
| 79 | { |
||
| 80 | return $this->foo; |
||
| 81 | } |
||
| 82 | }; |
||
| 83 | |||
| 84 | $this->assertSame($o, $s->inject($o, 'b', 'bar')); |
||
| 85 | $this->assertSame('bar', $o->getB()); |
||
| 86 | $this->assertSame($o, $s->inject($o, 'some_long_property', 42)); |
||
| 87 | $this->assertSame(42, $o->getFoo()); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testThrowWhenInjectingUnsupportedProperty() |
||
| 91 | { |
||
| 92 | $s = new SetterStrategy; |
||
| 93 | $o = new class { |
||
| 94 | public $a; |
||
| 95 | }; |
||
| 96 | |||
| 97 | $this->expectException(LogicException::class); |
||
| 98 | |||
| 99 | $s->inject($o, 'a', 'foo'); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function testInjectWithInheritedMethod() |
||
| 103 | { |
||
| 104 | $strategy = new SetterStrategy; |
||
| 105 | $object = new class extends Foo {}; |
||
| 106 | |||
| 107 | $this->assertSame(42, $object->someProperty()); |
||
| 108 | $this->assertSame($object, $strategy->inject($object, 'someProperty', 24)); |
||
| 109 | $this->assertSame(24, $object->someProperty()); |
||
| 110 | } |
||
| 111 | } |
||
| 112 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.