|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package Functional-php |
| 5 | + * @author Lars Strojny <lstrojny@php.net> |
| 6 | + * @copyright 2011-2021 Lars Strojny |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + * @link https://github.com/lstrojny/functional-php |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Functional\Tests; |
| 12 | + |
| 13 | +use function Functional\when; |
| 14 | + |
| 15 | +class WhenTest extends AbstractTestCase |
| 16 | +{ |
| 17 | + public function testWhenConditionIsTruthy(): void |
| 18 | + { |
| 19 | + self::assertEquals('yes', when(true, 'yes')); |
| 20 | + } |
| 21 | + |
| 22 | + public function testWhenConditionIsFalsy(): void |
| 23 | + { |
| 24 | + self::assertNull(when(false, 'yes')); |
| 25 | + } |
| 26 | + |
| 27 | + public function testWhenConditionIsFalsyWithDefault(): void |
| 28 | + { |
| 29 | + self::assertEquals('no', when(false, 'yes', 'no')); |
| 30 | + } |
| 31 | + |
| 32 | + public function testWhenValueIsClosure(): void |
| 33 | + { |
| 34 | + self::assertEquals('condition was true', when(true, function ($condition) { |
| 35 | + return $condition ? 'condition was true' : 'condition was false'; |
| 36 | + })); |
| 37 | + } |
| 38 | + |
| 39 | + public function testWhenDefaultIsClosure(): void |
| 40 | + { |
| 41 | + self::assertEquals('fallback', when(false, 'yes', function () { |
| 42 | + return 'fallback'; |
| 43 | + })); |
| 44 | + } |
| 45 | + |
| 46 | + public function testWhenConditionIsClosure(): void |
| 47 | + { |
| 48 | + self::assertEquals('yes', when(function () { |
| 49 | + return true; |
| 50 | + }, 'yes')); |
| 51 | + } |
| 52 | + |
| 53 | + public function testWhenConditionIsClosureReturningFalsy(): void |
| 54 | + { |
| 55 | + self::assertNull(when(function () { |
| 56 | + return false; |
| 57 | + }, 'yes')); |
| 58 | + } |
| 59 | +} |
0 commit comments