Innmind /
Reflection
| 1 | <?php |
||||
| 2 | declare(strict_types = 1); |
||||
| 3 | |||||
| 4 | namespace Tests\Innmind\Reflection\InjectionStrategy; |
||||
| 5 | |||||
| 6 | use Innmind\Reflection\{ |
||||
| 7 | InjectionStrategy\NamedMethodStrategy, |
||||
| 8 | InjectionStrategy, |
||||
| 9 | Exception\LogicException, |
||||
| 10 | }; |
||||
| 11 | use Fixtures\Innmind\Reflection\Foo; |
||||
| 12 | use PHPUnit\Framework\TestCase; |
||||
| 13 | |||||
| 14 | class NamedMethodStrategyTest extends TestCase |
||||
| 15 | { |
||||
| 16 | public function testInterface() |
||||
| 17 | { |
||||
| 18 | $this->assertInstanceOf( |
||||
| 19 | InjectionStrategy::class, |
||||
| 20 | new NamedMethodStrategy |
||||
| 21 | ); |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | public function testSupports() |
||||
| 25 | { |
||||
| 26 | $s = new NamedMethodStrategy; |
||||
| 27 | $o = new class { |
||||
| 28 | private $a; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 29 | private $b; |
||||
| 30 | |||||
| 31 | public function b($b) |
||||
| 32 | { |
||||
| 33 | $this->b = $b; |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function someLongProperty($foo) |
||||
|
0 ignored issues
–
show
The parameter
$foo is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 37 | { |
||||
| 38 | } |
||||
| 39 | |||||
| 40 | private function foo($foo) |
||||
|
0 ignored issues
–
show
The parameter
$foo is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 41 | { |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | private function bar($bar) |
||||
|
0 ignored issues
–
show
The parameter
$bar is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 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 NamedMethodStrategy; |
||||
| 59 | $o = new class { |
||||
| 60 | private $b; |
||||
| 61 | private $foo; |
||||
| 62 | |||||
| 63 | public function b($b) |
||||
| 64 | { |
||||
| 65 | $this->b = $b; |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | public function getB() |
||||
| 69 | { |
||||
| 70 | return $this->b; |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | public function someLongProperty($foo) |
||||
| 74 | { |
||||
| 75 | $this->foo = $foo; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | public function foo() |
||||
| 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->foo()); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | public function testThrowWhenInjectingUnsupportedProperty() |
||||
| 91 | { |
||||
| 92 | $s = new NamedMethodStrategy; |
||||
| 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 NamedMethodStrategy; |
||||
| 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 |