Innmind /
Reflection
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Tests\Innmind\Reflection; |
||
| 5 | |||
| 6 | use Innmind\Reflection\{ |
||
| 7 | ReflectionObject, |
||
|
0 ignored issues
–
show
|
|||
| 8 | ExtractionStrategy\ExtractionStrategies, |
||
| 9 | ExtractionStrategy, |
||
| 10 | InjectionStrategy\InjectionStrategies, |
||
| 11 | InjectionStrategy, |
||
| 12 | Exception\LogicException, |
||
| 13 | }; |
||
| 14 | use Innmind\Immutable\Map; |
||
| 15 | use PHPUnit\Framework\TestCase; |
||
| 16 | |||
| 17 | class ReflectionObjectTest extends TestCase |
||
| 18 | { |
||
| 19 | public function testBuildWithoutProperties() |
||
| 20 | { |
||
| 21 | $o = new \stdClass; |
||
| 22 | $refl = new ReflectionObject($o); |
||
| 23 | |||
| 24 | $o2 = $refl->build(); |
||
| 25 | |||
| 26 | $this->assertSame($o, $o2); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testAddPropertyToInject() |
||
| 30 | { |
||
| 31 | $o = new \stdClass; |
||
| 32 | $refl = new ReflectionObject($o); |
||
| 33 | $refl2 = $refl->withProperty('foo', 'bar'); |
||
| 34 | |||
| 35 | $this->assertInstanceOf(ReflectionObject::class, $refl2); |
||
| 36 | $this->assertNotSame($refl, $refl2); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testBuild() |
||
| 40 | { |
||
| 41 | $o = new class() |
||
| 42 | { |
||
| 43 | private $a; |
||
| 44 | protected $b; |
||
| 45 | private $c; |
||
| 46 | private $d; |
||
| 47 | |||
| 48 | public function setC($value) |
||
| 49 | { |
||
| 50 | $this->c = $value; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function d($value) |
||
| 54 | { |
||
| 55 | $this->d = $value; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function dump() |
||
| 59 | { |
||
| 60 | return [$this->a, $this->b, $this->c, $this->d]; |
||
| 61 | } |
||
| 62 | }; |
||
| 63 | |||
| 64 | $this->assertSame([null, null, null, null], $o->dump()); |
||
| 65 | |||
| 66 | $result = ReflectionObject::of($o) |
||
| 67 | ->withProperty('a', 1) |
||
| 68 | ->withProperty('b', 2) |
||
| 69 | ->withProperty('c', 3) |
||
| 70 | ->withProperty('d', 4) |
||
| 71 | ->build(); |
||
| 72 | |||
| 73 | $this->assertSame($o, $result); |
||
| 74 | $this->assertSame([1, 2, 3, 4], $o->dump()); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function testBuildWithProperties() |
||
| 78 | { |
||
| 79 | $o = new class() |
||
| 80 | { |
||
| 81 | private $a; |
||
| 82 | protected $b; |
||
| 83 | private $c; |
||
| 84 | private $d; |
||
| 85 | |||
| 86 | public function setC($value) |
||
| 87 | { |
||
| 88 | $this->c = $value; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function d($value) |
||
| 92 | { |
||
| 93 | $this->d = $value; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function dump() |
||
| 97 | { |
||
| 98 | return [$this->a, $this->b, $this->c, $this->d]; |
||
| 99 | } |
||
| 100 | }; |
||
| 101 | |||
| 102 | $this->assertSame([null, null, null, null], $o->dump()); |
||
| 103 | |||
| 104 | ReflectionObject::of($o) |
||
| 105 | ->withProperties( |
||
| 106 | [ |
||
| 107 | 'a' => 1, |
||
| 108 | 'b' => 2, |
||
| 109 | ] |
||
| 110 | ) |
||
| 111 | ->withProperties( |
||
| 112 | [ |
||
| 113 | 'c' => 3, |
||
| 114 | 'd' => 4, |
||
| 115 | ] |
||
| 116 | ) |
||
| 117 | ->build(); |
||
| 118 | |||
| 119 | $this->assertSame([1, 2, 3, 4], $o->dump()); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testThrowWhenPropertyNotFound() |
||
| 123 | { |
||
| 124 | $this->expectException(LogicException::class); |
||
| 125 | $this->expectExceptionMessage('Property "a" cannot be injected'); |
||
| 126 | |||
| 127 | ReflectionObject::of(new \stdClass) |
||
| 128 | ->withProperty('a', 1) |
||
| 129 | ->build(); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function testThrowWhenNameMethodDoesntHaveParameter() |
||
| 133 | { |
||
| 134 | $o = new class() |
||
| 135 | { |
||
| 136 | public function a() |
||
| 137 | { |
||
| 138 | //pass |
||
| 139 | } |
||
| 140 | }; |
||
| 141 | |||
| 142 | $this->expectException(LogicException::class); |
||
| 143 | $this->expectExceptionMessage('Property "a" cannot be injected'); |
||
| 144 | |||
| 145 | ReflectionObject::of($o) |
||
| 146 | ->withProperty('a', 1) |
||
| 147 | ->build(); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function testExtract() |
||
| 151 | { |
||
| 152 | $o = new class |
||
| 153 | { |
||
| 154 | public $a = 24; |
||
| 155 | |||
| 156 | public function getB() |
||
| 157 | { |
||
| 158 | return 42; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function c() |
||
| 162 | { |
||
| 163 | return 66; |
||
| 164 | } |
||
| 165 | }; |
||
| 166 | $refl = new ReflectionObject($o); |
||
| 167 | |||
| 168 | $values = $refl->extract('a', 'b', 'c'); |
||
| 169 | |||
| 170 | $this->assertInstanceOf(Map::class, $values); |
||
| 171 | $this->assertSame('string', (string) $values->keyType()); |
||
| 172 | $this->assertSame('mixed', (string) $values->valueType()); |
||
| 173 | $this->assertCount(3, $values); |
||
| 174 | $this->assertSame(24, $values->get('a')); |
||
| 175 | $this->assertSame(42, $values->get('b')); |
||
| 176 | $this->assertSame(66, $values->get('c')); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function testThrowWhenCannotExtractProperty() |
||
| 180 | { |
||
| 181 | $this->expectException(LogicException::class); |
||
| 182 | $this->expectExceptionMessage('Property "a" cannot be extracted'); |
||
| 183 | |||
| 184 | ReflectionObject::of(new \stdClass)->extract('a'); |
||
| 185 | } |
||
| 186 | } |
||
| 187 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths