Skip to content

Commit 553a885

Browse files
committed
Simplify Conditionable tests
1 parent d49e821 commit 553a885

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

tests/Support/SupportConditionableTest.php

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,87 @@
22

33
namespace Illuminate\Tests\Support;
44

5-
use Exception;
65
use Illuminate\Support\Traits\Conditionable;
76
use PHPUnit\Framework\TestCase;
87

98
class SupportConditionableTest extends TestCase
109
{
1110
public function testWhenConditionCallback()
1211
{
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);
1715
}, function () {
18-
throw new Exception('when() should not trigger default callback on a truthy value');
16+
$logger->log('default', $condition);
1917
});
2018

21-
$this->assertTrue($object->enabled);
19+
$this->assertSame(['when', 2], $logger->values);
2220
}
2321

2422
public function testWhenDefaultCallback()
2523
{
26-
$object = (new CustomConditionableObject())
24+
$logger = (new ConditionableLogger())
2725
->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);
3229
});
3330

34-
$this->assertTrue($object->enabled);
31+
$this->assertSame(['default', null], $logger->values);
3532
}
3633

3734
public function testUnlessConditionCallback()
3835
{
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);
4339
}, function () {
44-
throw new Exception('unless() should not trigger default callback on a falsy value');
40+
$logger->log('default', $condition);
4541
});
4642

47-
$this->assertTrue($object->enabled);
43+
$this->assertSame(['unless', null], $logger->values);
4844
}
4945

5046
public function testUnlessDefaultCallback()
5147
{
52-
$object = (new CustomConditionableObject())
48+
$logger = (new ConditionableLogger())
5349
->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);
5853
});
5954

60-
$this->assertTrue($object->enabled);
55+
$this->assertSame(['default', 2], $logger->values);
6156
}
6257

6358
public function testWhenProxy()
6459
{
65-
$object = (new CustomConditionableObject())->when(true)->on();
60+
$logger = (new ConditionableLogger())
61+
->when(true)->log('one')
62+
->when(false)->log('two');
6663

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);
7465
}
7566

7667
public function testUnlessProxy()
7768
{
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');
8272

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);
8774
}
8875
}
8976

90-
class CustomConditionableObject
77+
class ConditionableLogger
9178
{
9279
use Conditionable;
9380

94-
public $enabled = false;
95-
96-
public function on()
97-
{
98-
$this->enabled = true;
99-
100-
return $this;
101-
}
81+
public $values = [];
10282

103-
public function off()
83+
public function log(...$values)
10484
{
105-
$this->enabled = false;
85+
array_push($this->values, ...$values);
10686

10787
return $this;
10888
}

0 commit comments

Comments
 (0)