|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Support; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Support\Traits\Conditionable; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +class SupportConditionableTest extends TestCase |
| 10 | +{ |
| 11 | + public function testWhenConditionCallback() |
| 12 | + { |
| 13 | + $object = (new CustomConditionableObject()) |
| 14 | + ->when(2, function ($object, $condition) { |
| 15 | + $object->on(); |
| 16 | + $this->assertEquals(2, $condition); |
| 17 | + }, function () { |
| 18 | + throw new Exception('when() should not trigger default callback on a truthy value'); |
| 19 | + }); |
| 20 | + |
| 21 | + $this->assertTrue($object->enabled); |
| 22 | + } |
| 23 | + |
| 24 | + public function testWhenDefaultCallback() |
| 25 | + { |
| 26 | + $object = (new CustomConditionableObject()) |
| 27 | + ->when(null, function () { |
| 28 | + throw new Exception('when() should not trigger on a falsy value'); |
| 29 | + }, function ($object, $condition) { |
| 30 | + $object->on(); |
| 31 | + $this->assertNull($condition); |
| 32 | + }); |
| 33 | + |
| 34 | + $this->assertTrue($object->enabled); |
| 35 | + } |
| 36 | + |
| 37 | + public function testUnlessConditionCallback() |
| 38 | + { |
| 39 | + $object = (new CustomConditionableObject()) |
| 40 | + ->unless(null, function ($object, $condition) { |
| 41 | + $object->on(); |
| 42 | + $this->assertNull($condition); |
| 43 | + }, function () { |
| 44 | + throw new Exception('unless() should not trigger default callback on a falsy value'); |
| 45 | + }); |
| 46 | + |
| 47 | + $this->assertTrue($object->enabled); |
| 48 | + } |
| 49 | + |
| 50 | + public function testUnlessDefaultCallback() |
| 51 | + { |
| 52 | + $object = (new CustomConditionableObject()) |
| 53 | + ->unless(2, function () { |
| 54 | + throw new Exception('unless() should not trigger on a truthy value'); |
| 55 | + }, function ($object, $condition) { |
| 56 | + $object->on(); |
| 57 | + $this->assertEquals(2, $condition); |
| 58 | + }); |
| 59 | + |
| 60 | + $this->assertTrue($object->enabled); |
| 61 | + } |
| 62 | + |
| 63 | + public function testWhenProxy() |
| 64 | + { |
| 65 | + $object = (new CustomConditionableObject())->when(true)->on(); |
| 66 | + |
| 67 | + $this->assertInstanceOf(CustomConditionableObject::class, $object); |
| 68 | + $this->assertTrue($object->enabled); |
| 69 | + |
| 70 | + $object = (new CustomConditionableObject())->when(false)->on(); |
| 71 | + |
| 72 | + $this->assertInstanceOf(CustomConditionableObject::class, $object); |
| 73 | + $this->assertFalse($object->enabled); |
| 74 | + } |
| 75 | + |
| 76 | + public function testUnlessProxy() |
| 77 | + { |
| 78 | + $object = (new CustomConditionableObject())->unless(false)->on(); |
| 79 | + |
| 80 | + $this->assertInstanceOf(CustomConditionableObject::class, $object); |
| 81 | + $this->assertTrue($object->enabled); |
| 82 | + |
| 83 | + $object = (new CustomConditionableObject())->unless(true)->on(); |
| 84 | + |
| 85 | + $this->assertInstanceOf(CustomConditionableObject::class, $object); |
| 86 | + $this->assertFalse($object->enabled); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class CustomConditionableObject |
| 91 | +{ |
| 92 | + use Conditionable; |
| 93 | + |
| 94 | + public $enabled = false; |
| 95 | + |
| 96 | + public function on() |
| 97 | + { |
| 98 | + $this->enabled = true; |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + public function off() |
| 104 | + { |
| 105 | + $this->enabled = false; |
| 106 | + |
| 107 | + return $this; |
| 108 | + } |
| 109 | +} |
0 commit comments