|
@@ 327-376 (lines=50) @@
|
| 324 |
|
* ---------------------------------------------------------------------------------------------------------------- |
| 325 |
|
*/ |
| 326 |
|
|
| 327 |
|
public function testUnless() |
| 328 |
|
{ |
| 329 |
|
// -- Basic test. No params for the test nor the callable. |
| 330 |
|
$test = function () { |
| 331 |
|
return true === $this->getWhenExists(); |
| 332 |
|
}; |
| 333 |
|
|
| 334 |
|
$callable = function () {return 1;}; |
| 335 |
|
|
| 336 |
|
$unless = Func::unless($test, $callable); |
| 337 |
|
$this->setWhenExists(true); |
| 338 |
|
$this->assertNull($unless()); |
| 339 |
|
$this->setWhenExists(false); |
| 340 |
|
$this->assertEquals(1, $unless()); |
| 341 |
|
|
| 342 |
|
// -- Params for the callable. |
| 343 |
|
$callable = function ($val) {return $val;}; |
| 344 |
|
$unless = Func::unless($test, $callable); |
| 345 |
|
$this->setWhenExists(true); |
| 346 |
|
$this->assertNull($unless('foo')); |
| 347 |
|
$this->setWhenExists(false); |
| 348 |
|
$this->assertEquals('foo', $unless('foo')); |
| 349 |
|
|
| 350 |
|
// -- Same params for the callable and the test. |
| 351 |
|
$test = function ($bool) { |
| 352 |
|
return $bool === $this->getWhenExists(); |
| 353 |
|
}; |
| 354 |
|
|
| 355 |
|
$unless = Func::unless($test, $callable, Func::PASSTHROUGH); |
| 356 |
|
$this->setWhenExists(true); |
| 357 |
|
$this->assertNull($unless(true)); |
| 358 |
|
$this->assertFalse($unless(false)); |
| 359 |
|
$this->assertEquals('foo', $unless('foo')); |
| 360 |
|
$this->setWhenExists(false); |
| 361 |
|
$this->assertTrue($unless(true)); |
| 362 |
|
|
| 363 |
|
// -- Different params for the callable and the test. |
| 364 |
|
$test = function ($bool) { |
| 365 |
|
return $bool === $this->getWhenExists(); |
| 366 |
|
}; |
| 367 |
|
|
| 368 |
|
// Pass 'true' to the test. |
| 369 |
|
$unless = Func::unless($test, $callable, true); |
| 370 |
|
$this->setWhenExists(true); |
| 371 |
|
$this->assertNull($unless('foo')); |
| 372 |
|
$this->assertNull($unless(false)); |
| 373 |
|
$this->setWhenExists(false); |
| 374 |
|
$this->assertEquals('foo', $unless('foo')); |
| 375 |
|
$this->assertTrue($unless(true)); |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
// Func::when() |
| 379 |
|
public function testWhen() |
|
@@ 379-427 (lines=49) @@
|
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
// Func::when() |
| 379 |
|
public function testWhen() |
| 380 |
|
{ |
| 381 |
|
// -- Basic test. No params for the test nor the callable. |
| 382 |
|
$test = function () { |
| 383 |
|
return true === $this->getWhenExists(); |
| 384 |
|
}; |
| 385 |
|
|
| 386 |
|
$callable = function () {return 1;}; |
| 387 |
|
$when = Func::when($test, $callable); |
| 388 |
|
$this->setWhenExists(true); |
| 389 |
|
$this->assertEquals(1, $when()); |
| 390 |
|
$this->setWhenExists(false); |
| 391 |
|
$this->assertNull($when()); |
| 392 |
|
|
| 393 |
|
// -- Params for the callable. |
| 394 |
|
$callable = function ($val) {return $val;}; |
| 395 |
|
$when = Func::when($test, $callable); |
| 396 |
|
$this->setWhenExists(true); |
| 397 |
|
$this->assertEquals('foo', $when('foo')); |
| 398 |
|
$this->setWhenExists(false); |
| 399 |
|
$this->assertNull($when('foo')); |
| 400 |
|
|
| 401 |
|
// -- Same params for the callable and the test. |
| 402 |
|
$test = function ($bool) { |
| 403 |
|
return $bool === $this->getWhenExists(); |
| 404 |
|
}; |
| 405 |
|
|
| 406 |
|
$when = Func::when($test, $callable, Func::PASSTHROUGH); |
| 407 |
|
$this->setWhenExists(true); |
| 408 |
|
$this->assertTrue($when(true)); |
| 409 |
|
$this->assertNull($when(false)); |
| 410 |
|
$this->assertNull($when('foo')); |
| 411 |
|
$this->setWhenExists(false); |
| 412 |
|
$this->assertFalse($when(false)); |
| 413 |
|
|
| 414 |
|
// -- Different params for the callable and the test. |
| 415 |
|
$test = function ($bool) { |
| 416 |
|
return $bool === $this->getWhenExists(); |
| 417 |
|
}; |
| 418 |
|
|
| 419 |
|
// Pass 'true' to the test. |
| 420 |
|
$when = Func::when($test, $callable, true); |
| 421 |
|
$this->setWhenExists(true); |
| 422 |
|
$this->assertEquals('foo', $when('foo')); |
| 423 |
|
$this->assertFalse($when(false)); |
| 424 |
|
$this->setWhenExists(false); |
| 425 |
|
$this->assertNull($when('foo')); |
| 426 |
|
$this->assertNull($when(false)); |
| 427 |
|
} |
| 428 |
|
|
| 429 |
|
// Used by the when/unless tests. |
| 430 |
|
protected function getWhenExists() |