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