Skip to content

Commit 87022a9

Browse files
committed
Simplify Conditionable tests
1 parent d49e821 commit 87022a9

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

Diff for: tests/Support/SupportConditionableTest.php

+32-51
Original file line numberDiff line numberDiff line change
@@ -10,99 +10,80 @@ class SupportConditionableTest extends TestCase
1010
{
1111
public function testWhenConditionCallback()
1212
{
13-
$object = (new CustomConditionableObject())
14-
->when(2, function ($object, $condition) {
15-
$object->on();
16-
$this->assertEquals(2, $condition);
13+
$logger = (new ConditionableLogger())
14+
->when(2, function ($logger, $condition) {
15+
$logger->log('when', $condition);
1716
}, function () {
18-
throw new Exception('when() should not trigger default callback on a truthy value');
17+
$logger->log('default', $condition);
1918
});
2019

21-
$this->assertTrue($object->enabled);
20+
$this->assertSame([['when', 2]], $logger->values);
2221
}
2322

2423
public function testWhenDefaultCallback()
2524
{
26-
$object = (new CustomConditionableObject())
25+
$logger = (new ConditionableLogger())
2726
->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);
27+
$logger->log('when', $condition);
28+
}, function ($logger, $condition) {
29+
$logger->log('default', $condition);
3230
});
3331

34-
$this->assertTrue($object->enabled);
32+
$this->assertSame([['default', null]], $logger->values);
3533
}
3634

3735
public function testUnlessConditionCallback()
3836
{
39-
$object = (new CustomConditionableObject())
40-
->unless(null, function ($object, $condition) {
41-
$object->on();
42-
$this->assertNull($condition);
37+
$logger = (new ConditionableLogger())
38+
->unless(null, function ($logger, $condition) {
39+
$logger->log('unless', $condition);
4340
}, function () {
44-
throw new Exception('unless() should not trigger default callback on a falsy value');
41+
$logger->log('default', $condition);
4542
});
4643

47-
$this->assertTrue($object->enabled);
44+
$this->assertSame([['unless', null]], $logger->values);
4845
}
4946

5047
public function testUnlessDefaultCallback()
5148
{
52-
$object = (new CustomConditionableObject())
49+
$logger = (new ConditionableLogger())
5350
->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);
51+
$logger->log('unless', $condition);
52+
}, function ($logger, $condition) {
53+
$logger->log('default', $condition);
5854
});
5955

60-
$this->assertTrue($object->enabled);
56+
$this->assertSame([['default', 2]], $logger->values);
6157
}
6258

6359
public function testWhenProxy()
6460
{
65-
$object = (new CustomConditionableObject())->when(true)->on();
61+
$logger = (new ConditionableLogger())
62+
->when(true)->log('one')
63+
->when(false)->log('two');
6664

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);
65+
$this->assertSame([['one']], $logger->values);
7466
}
7567

7668
public function testUnlessProxy()
7769
{
78-
$object = (new CustomConditionableObject())->unless(false)->on();
79-
80-
$this->assertInstanceOf(CustomConditionableObject::class, $object);
81-
$this->assertTrue($object->enabled);
70+
$logger = (new ConditionableLogger())
71+
->unless(true)->log('one')
72+
->unless(false)->log('two');
8273

83-
$object = (new CustomConditionableObject())->unless(true)->on();
84-
85-
$this->assertInstanceOf(CustomConditionableObject::class, $object);
86-
$this->assertFalse($object->enabled);
74+
$this->assertSame([['two']], $logger->values);
8775
}
8876
}
8977

90-
class CustomConditionableObject
78+
class ConditionableLogger
9179
{
9280
use Conditionable;
9381

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

103-
public function off()
84+
public function log(...$values)
10485
{
105-
$this->enabled = false;
86+
$this->values[] = $values;
10687

10788
return $this;
10889
}

0 commit comments

Comments
 (0)