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