Innmind /
Immutable
| 1 | <?php |
||||
| 2 | declare(strict_types = 1); |
||||
| 3 | |||||
| 4 | namespace Tests\Innmind\Immutable; |
||||
| 5 | |||||
| 6 | use Innmind\Immutable\Either; |
||||
| 7 | use PHPUnit\Framework\TestCase; |
||||
| 8 | use Innmind\BlackBox\{ |
||||
| 9 | PHPUnit\BlackBox, |
||||
| 10 | Set, |
||||
| 11 | }; |
||||
| 12 | |||||
| 13 | class EitherTest extends TestCase |
||||
| 14 | { |
||||
| 15 | use BlackBox; |
||||
| 16 | |||||
| 17 | public function testMatchLeft() |
||||
| 18 | { |
||||
| 19 | $this |
||||
| 20 | ->forAll( |
||||
| 21 | Set\AnyType::any(), |
||||
| 22 | Set\AnyType::any(), |
||||
| 23 | ) |
||||
| 24 | ->then(function($left, $right) { |
||||
| 25 | $either = Either::left($left); |
||||
| 26 | |||||
| 27 | $this->assertSame( |
||||
| 28 | $left, |
||||
| 29 | $either->match( |
||||
| 30 | static fn($value) => $right, |
||||
|
0 ignored issues
–
show
|
|||||
| 31 | function($value) use ($left) { |
||||
| 32 | $this->assertSame($left, $value); |
||||
| 33 | |||||
| 34 | return $value; |
||||
| 35 | }, |
||||
| 36 | ), |
||||
| 37 | ); |
||||
| 38 | }); |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | public function testMatchRight() |
||||
| 42 | { |
||||
| 43 | $this |
||||
| 44 | ->forAll( |
||||
| 45 | Set\AnyType::any(), |
||||
| 46 | Set\AnyType::any(), |
||||
| 47 | ) |
||||
| 48 | ->then(function($left, $right) { |
||||
| 49 | $either = Either::right($right); |
||||
| 50 | |||||
| 51 | $this->assertSame( |
||||
| 52 | $right, |
||||
| 53 | $either->match( |
||||
| 54 | function($value) use ($right) { |
||||
| 55 | $this->assertSame($right, $value); |
||||
| 56 | |||||
| 57 | return $value; |
||||
| 58 | }, |
||||
| 59 | static fn($value) => $value, |
||||
| 60 | ), |
||||
| 61 | ); |
||||
| 62 | }); |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | public function testLeftValueIsNotMapped() |
||||
| 66 | { |
||||
| 67 | $this |
||||
| 68 | ->forAll(Set\AnyType::any()) |
||||
| 69 | ->then(function($initial) { |
||||
| 70 | $either = Either::left($initial)->map(static function() { |
||||
| 71 | throw new \Exception; |
||||
| 72 | }); |
||||
| 73 | |||||
| 74 | $this->assertInstanceOf(Either::class, $either); |
||||
| 75 | $this->assertSame( |
||||
| 76 | $initial, |
||||
| 77 | $either->match( |
||||
| 78 | static fn($value) => $value, |
||||
| 79 | static fn($value) => $value, |
||||
| 80 | ), |
||||
| 81 | ); |
||||
| 82 | }); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | public function testRightValueIsMapped() |
||||
| 86 | { |
||||
| 87 | $this |
||||
| 88 | ->forAll( |
||||
| 89 | Set\AnyType::any(), |
||||
| 90 | Set\AnyType::any(), |
||||
| 91 | ) |
||||
| 92 | ->then(function($initial, $mapped) { |
||||
| 93 | $either = Either::right($initial)->map(function($value) use ($initial, $mapped) { |
||||
| 94 | $this->assertSame($initial, $value); |
||||
| 95 | |||||
| 96 | return $mapped; |
||||
| 97 | }); |
||||
| 98 | |||||
| 99 | $this->assertInstanceOf(Either::class, $either); |
||||
| 100 | $this->assertSame( |
||||
| 101 | $mapped, |
||||
| 102 | $either->match( |
||||
| 103 | static fn($value) => $value, |
||||
| 104 | static fn($value) => $value, |
||||
| 105 | ), |
||||
| 106 | ); |
||||
| 107 | }); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | public function testLeftValueIsNotFlatMapped() |
||||
| 111 | { |
||||
| 112 | $this |
||||
| 113 | ->forAll(Set\AnyType::any()) |
||||
| 114 | ->then(function($left) { |
||||
| 115 | $either = Either::left($left)->flatMap(static function() { |
||||
| 116 | throw new \Exception; |
||||
| 117 | }); |
||||
| 118 | |||||
| 119 | $this->assertInstanceOf(Either::class, $either); |
||||
| 120 | $this->assertSame( |
||||
| 121 | $left, |
||||
| 122 | $either->match( |
||||
| 123 | static fn($value) => $value, |
||||
| 124 | static fn($value) => $value, |
||||
| 125 | ), |
||||
| 126 | ); |
||||
| 127 | }); |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | public function testRightValueIsFlatMapped() |
||||
| 131 | { |
||||
| 132 | $this |
||||
| 133 | ->forAll( |
||||
| 134 | Set\AnyType::any(), |
||||
| 135 | new Set\Either( |
||||
| 136 | Set\Decorate::immutable( |
||||
| 137 | static fn($value) => Either::left($value), |
||||
| 138 | Set\AnyType::any(), |
||||
| 139 | ), |
||||
| 140 | Set\Decorate::immutable( |
||||
| 141 | static fn($value) => Either::right($value), |
||||
| 142 | Set\AnyType::any(), |
||||
| 143 | ), |
||||
| 144 | ), |
||||
| 145 | ) |
||||
| 146 | ->then(function($right, $expected) { |
||||
| 147 | $either = Either::right($right)->flatMap(function($value) use ($right, $expected) { |
||||
| 148 | $this->assertSame($right, $value); |
||||
| 149 | |||||
| 150 | return $expected; |
||||
| 151 | }); |
||||
| 152 | |||||
| 153 | $this->assertSame($expected, $either); |
||||
| 154 | }); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | public function testOtherwiseIsCalledWhenLeftValue() |
||||
| 158 | { |
||||
| 159 | $this |
||||
| 160 | ->forAll( |
||||
| 161 | Set\AnyType::any(), |
||||
| 162 | new Set\Either( |
||||
| 163 | Set\Decorate::immutable( |
||||
| 164 | static fn($value) => Either::left($value), |
||||
| 165 | Set\AnyType::any(), |
||||
| 166 | ), |
||||
| 167 | Set\Decorate::immutable( |
||||
| 168 | static fn($value) => Either::right($value), |
||||
| 169 | Set\AnyType::any(), |
||||
| 170 | ), |
||||
| 171 | ), |
||||
| 172 | ) |
||||
| 173 | ->then(function($left, $expected) { |
||||
| 174 | $either = Either::left($left)->otherwise(static fn() => $expected); |
||||
| 175 | |||||
| 176 | $this->assertSame($expected, $either); |
||||
| 177 | }); |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | public function testOtherwiseIsNotCalledWhenRightValue() |
||||
| 181 | { |
||||
| 182 | $this |
||||
| 183 | ->forAll( |
||||
| 184 | Set\AnyType::any(), |
||||
| 185 | Set\AnyType::any(), |
||||
| 186 | ) |
||||
| 187 | ->then(function($right, $left) { |
||||
| 188 | $either = Either::right($right)->otherwise(static function() { |
||||
| 189 | throw new \Exception; |
||||
| 190 | }); |
||||
| 191 | |||||
| 192 | $this->assertInstanceOf(Either::class, $either); |
||||
| 193 | $this->assertSame( |
||||
| 194 | $right, |
||||
| 195 | $either->match( |
||||
| 196 | static fn($value) => $value, |
||||
| 197 | static fn() => $left, |
||||
| 198 | ), |
||||
| 199 | ); |
||||
| 200 | }); |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | public function testKeepSameValueWhenFilteringLeftValue() |
||||
| 204 | { |
||||
| 205 | $this |
||||
| 206 | ->forAll( |
||||
| 207 | Set\AnyType::any(), |
||||
| 208 | Set\AnyType::any(), |
||||
| 209 | Set\Elements::of(true, false), |
||||
| 210 | Set\AnyType::any(), |
||||
| 211 | ) |
||||
| 212 | ->then(function($left, $right, $predicate, $otherwise) { |
||||
| 213 | $either = Either::left($left)->filter( |
||||
| 214 | static fn() => $predicate, |
||||
| 215 | static fn() => $otherwise, |
||||
| 216 | ); |
||||
| 217 | |||||
| 218 | $this->assertInstanceOf(Either::class, $either); |
||||
| 219 | $this->assertSame( |
||||
| 220 | $left, |
||||
| 221 | $either->match( |
||||
| 222 | static fn() => $right, |
||||
| 223 | static fn($value) => $value, |
||||
| 224 | ), |
||||
| 225 | ); |
||||
| 226 | }); |
||||
| 227 | } |
||||
| 228 | |||||
| 229 | public function testKeepSameValueWhenRightValueMatchPredicate() |
||||
| 230 | { |
||||
| 231 | $this |
||||
| 232 | ->forAll( |
||||
| 233 | Set\AnyType::any(), |
||||
| 234 | Set\AnyType::any(), |
||||
| 235 | Set\AnyType::any(), |
||||
| 236 | ) |
||||
| 237 | ->then(function($right, $left, $otherwise) { |
||||
| 238 | $either = Either::right($right)->filter( |
||||
| 239 | function($value) use ($right) { |
||||
| 240 | $this->assertSame($right, $value); |
||||
| 241 | |||||
| 242 | return true; |
||||
| 243 | }, |
||||
| 244 | static fn() => $otherwise, |
||||
| 245 | ); |
||||
| 246 | |||||
| 247 | $this->assertInstanceOf(Either::class, $either); |
||||
| 248 | $this->assertSame( |
||||
| 249 | $right, |
||||
| 250 | $either->match( |
||||
| 251 | static fn($value) => $value, |
||||
| 252 | static fn() => $left, |
||||
| 253 | ), |
||||
| 254 | ); |
||||
| 255 | }); |
||||
| 256 | } |
||||
| 257 | |||||
| 258 | public function testUseOtherValueWhenRightValueDoesntMatchPredicate() |
||||
| 259 | { |
||||
| 260 | $this |
||||
| 261 | ->forAll( |
||||
| 262 | Set\AnyType::any(), |
||||
| 263 | Set\AnyType::any(), |
||||
| 264 | Set\AnyType::any(), |
||||
| 265 | ) |
||||
| 266 | ->then(function($right, $unwanted, $otherwise) { |
||||
| 267 | $either = Either::right($right)->filter( |
||||
| 268 | function($value) use ($right) { |
||||
| 269 | $this->assertSame($right, $value); |
||||
| 270 | |||||
| 271 | return false; |
||||
| 272 | }, |
||||
| 273 | static fn() => $otherwise, |
||||
| 274 | ); |
||||
| 275 | |||||
| 276 | $this->assertInstanceOf(Either::class, $either); |
||||
| 277 | $this->assertSame( |
||||
| 278 | $otherwise, |
||||
| 279 | $either->match( |
||||
| 280 | static fn($value) => $unwanted, |
||||
|
0 ignored issues
–
show
The parameter
$value 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...
|
|||||
| 281 | static fn($value) => $value, |
||||
| 282 | ), |
||||
| 283 | ); |
||||
| 284 | }); |
||||
| 285 | } |
||||
| 286 | |||||
| 287 | public function testRightValueIsNotLeftMapped() |
||||
| 288 | { |
||||
| 289 | $this |
||||
| 290 | ->forAll(Set\AnyType::any()) |
||||
| 291 | ->then(function($initial) { |
||||
| 292 | $either = Either::right($initial)->leftMap(static function() { |
||||
| 293 | throw new \Exception; |
||||
| 294 | }); |
||||
| 295 | |||||
| 296 | $this->assertInstanceOf(Either::class, $either); |
||||
| 297 | $this->assertSame( |
||||
| 298 | $initial, |
||||
| 299 | $either->match( |
||||
| 300 | static fn($value) => $value, |
||||
| 301 | static fn($value) => $value, |
||||
| 302 | ), |
||||
| 303 | ); |
||||
| 304 | }); |
||||
| 305 | } |
||||
| 306 | |||||
| 307 | public function testLeftValueIsLeftMapped() |
||||
| 308 | { |
||||
| 309 | $this |
||||
| 310 | ->forAll( |
||||
| 311 | Set\AnyType::any(), |
||||
| 312 | Set\AnyType::any(), |
||||
| 313 | ) |
||||
| 314 | ->then(function($initial, $mapped) { |
||||
| 315 | $either = Either::left($initial)->leftMap(function($value) use ($initial, $mapped) { |
||||
| 316 | $this->assertSame($initial, $value); |
||||
| 317 | |||||
| 318 | return $mapped; |
||||
| 319 | }); |
||||
| 320 | |||||
| 321 | $this->assertInstanceOf(Either::class, $either); |
||||
| 322 | $this->assertSame( |
||||
| 323 | $mapped, |
||||
| 324 | $either->match( |
||||
| 325 | static fn($value) => $value, |
||||
| 326 | static fn($value) => $value, |
||||
| 327 | ), |
||||
| 328 | ); |
||||
| 329 | }); |
||||
| 330 | } |
||||
| 331 | |||||
| 332 | public function testMaybe() |
||||
| 333 | { |
||||
| 334 | $this |
||||
| 335 | ->forAll( |
||||
| 336 | Set\AnyType::any(), |
||||
| 337 | Set\AnyType::any(), |
||||
| 338 | ) |
||||
| 339 | ->then(function($left, $right) { |
||||
| 340 | $this->assertSame( |
||||
| 341 | $right, |
||||
| 342 | Either::right($right)->maybe()->match( |
||||
| 343 | static fn($value) => $value, |
||||
| 344 | static fn() => null, |
||||
| 345 | ), |
||||
| 346 | ); |
||||
| 347 | $this->assertNull( |
||||
| 348 | Either::left($left)->maybe()->match( |
||||
| 349 | static fn($value) => $value, |
||||
| 350 | static fn() => null, |
||||
| 351 | ), |
||||
| 352 | ); |
||||
| 353 | }); |
||||
| 354 | } |
||||
| 355 | } |
||||
| 356 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.