11
11
use PhpSchool \CliMenu \MenuItem \StaticItem ;
12
12
use PhpSchool \Terminal \Terminal ;
13
13
use PHPUnit \Framework \TestCase ;
14
- use RuntimeException ;
15
14
16
15
/**
17
16
* @author Aydin Hassan <[email protected] >
@@ -69,17 +68,18 @@ public function testModifyStyles() : void
69
68
$ builder ->setTitleSeparator ('- ' );
70
69
71
70
$ menu = $ builder ->build ();
71
+ $ style = $ menu ->getStyle ();
72
72
73
- $ this -> checkStyleVariable ( $ menu , ' bg ' , ' red ' );
74
- $ this -> checkStyleVariable ( $ menu , ' fg ' , ' red ' );
75
- $ this -> checkStyleVariable ( $ menu , ' width ' , 40 );
76
- $ this -> checkStyleVariable ( $ menu , ' paddingTopBottom ' , 4 );
77
- $ this -> checkStyleVariable ( $ menu , ' paddingLeftRight ' , 1 );
78
- $ this -> checkStyleVariable ( $ menu , ' margin ' , 4 );
79
- $ this -> checkStyleVariable ( $ menu , ' unselectedMarker ' , ' > ' );
80
- $ this -> checkStyleVariable ( $ menu , ' selectedMarker ' , ' x ' );
81
- $ this -> checkStyleVariable ( $ menu , ' itemExtra ' , ' * ' );
82
- $ this -> checkStyleVariable ( $ menu , ' titleSeparator ' , ' - ' );
73
+ self :: assertEquals ( ' red ' , $ style -> getBg () );
74
+ self :: assertEquals ( ' red ' , $ style -> getFg () );
75
+ self :: assertEquals ( 40 , $ style -> getWidth () );
76
+ self :: assertEquals ( 4 , $ style -> getPaddingTopBottom () );
77
+ self :: assertEquals ( 1 , $ style -> getPaddingLeftRight () );
78
+ self :: assertEquals ( 4 , $ style -> getMargin () );
79
+ self :: assertEquals ( ' > ' , $ style -> getUnselectedMarker () );
80
+ self :: assertEquals ( ' x ' , $ style -> getSelectedMarker () );
81
+ self :: assertEquals ( ' * ' , $ style -> getItemExtra () );
82
+ self :: assertEquals ( ' - ' , $ style -> getTitleSeparator () );
83
83
}
84
84
85
85
public function testSetBorderShorthandFunction () : void
@@ -90,85 +90,93 @@ public function testSetBorderShorthandFunction() : void
90
90
->method ('getWidth ' )
91
91
->will ($ this ->returnValue (200 ));
92
92
93
- $ menu = (new CliMenuBuilder ($ terminal ))
93
+ $ style = (new CliMenuBuilder ($ terminal ))
94
94
->setBorder (2 )
95
- ->build ();
96
-
97
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
98
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 2 );
99
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 2 );
100
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 2 );
101
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'white ' );
95
+ ->build ()
96
+ ->getStyle ();
102
97
103
- $ menu = (new CliMenuBuilder ($ terminal ))
98
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
99
+ self ::assertEquals (2 , $ style ->getBorderRightWidth ());
100
+ self ::assertEquals (2 , $ style ->getBorderBottomWidth ());
101
+ self ::assertEquals (2 , $ style ->getBorderLeftWidth ());
102
+ self ::assertEquals ('white ' , $ style ->getBorderColour ());
103
+
104
+ $ style = (new CliMenuBuilder ($ terminal ))
104
105
->setBorder (2 , 4 )
105
- ->build ();
106
-
107
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
108
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 4 );
109
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 2 );
110
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 4 );
111
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'white ' );
106
+ ->build ()
107
+ ->getStyle ();
112
108
113
- $ menu = (new CliMenuBuilder ($ terminal ))
109
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
110
+ self ::assertEquals (4 , $ style ->getBorderRightWidth ());
111
+ self ::assertEquals (2 , $ style ->getBorderBottomWidth ());
112
+ self ::assertEquals (4 , $ style ->getBorderLeftWidth ());
113
+ self ::assertEquals ('white ' , $ style ->getBorderColour ());
114
+
115
+ $ style = (new CliMenuBuilder ($ terminal ))
114
116
->setBorder (2 , 4 , 6 )
115
- ->build ();
116
-
117
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
118
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 4 );
119
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 6 );
120
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 4 );
121
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'white ' );
117
+ ->build ()
118
+ ->getStyle ();
122
119
123
- $ menu = (new CliMenuBuilder ($ terminal ))
120
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
121
+ self ::assertEquals (4 , $ style ->getBorderRightWidth ());
122
+ self ::assertEquals (6 , $ style ->getBorderBottomWidth ());
123
+ self ::assertEquals (4 , $ style ->getBorderLeftWidth ());
124
+ self ::assertEquals ('white ' , $ style ->getBorderColour ());
125
+
126
+ $ style = (new CliMenuBuilder ($ terminal ))
124
127
->setBorder (2 , 4 , 6 , 8 )
125
- ->build ();
126
-
127
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
128
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 4 );
129
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 6 );
130
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 8 );
131
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'white ' );
128
+ ->build ()
129
+ ->getStyle ();
132
130
133
- $ menu = (new CliMenuBuilder ($ terminal ))
131
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
132
+ self ::assertEquals (4 , $ style ->getBorderRightWidth ());
133
+ self ::assertEquals (6 , $ style ->getBorderBottomWidth ());
134
+ self ::assertEquals (8 , $ style ->getBorderLeftWidth ());
135
+ self ::assertEquals ('white ' , $ style ->getBorderColour ());
136
+
137
+ $ style = (new CliMenuBuilder ($ terminal ))
134
138
->setBorder (2 , 4 , 6 , 8 , 'green ' )
135
- ->build ();
136
-
137
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
138
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 4 );
139
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 6 );
140
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 8 );
141
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'green ' );
139
+ ->build ()
140
+ ->getStyle ();
142
141
143
- $ menu = (new CliMenuBuilder ($ terminal ))
142
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
143
+ self ::assertEquals (4 , $ style ->getBorderRightWidth ());
144
+ self ::assertEquals (6 , $ style ->getBorderBottomWidth ());
145
+ self ::assertEquals (8 , $ style ->getBorderLeftWidth ());
146
+ self ::assertEquals ('green ' , $ style ->getBorderColour ());
147
+
148
+ $ style = (new CliMenuBuilder ($ terminal ))
144
149
->setBorder (2 , 4 , 6 , 'green ' )
145
- ->build ();
146
-
147
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
148
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 4 );
149
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 6 );
150
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 4 );
151
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'green ' );
150
+ ->build ()
151
+ ->getStyle ();
152
152
153
- $ menu = (new CliMenuBuilder ($ terminal ))
153
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
154
+ self ::assertEquals (4 , $ style ->getBorderRightWidth ());
155
+ self ::assertEquals (6 , $ style ->getBorderBottomWidth ());
156
+ self ::assertEquals (4 , $ style ->getBorderLeftWidth ());
157
+ self ::assertEquals ('green ' , $ style ->getBorderColour ());
158
+
159
+ $ style = (new CliMenuBuilder ($ terminal ))
154
160
->setBorder (2 , 4 , 'green ' )
155
- ->build ();
156
-
157
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
158
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 4 );
159
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 2 );
160
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 4 );
161
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'green ' );
161
+ ->build ()
162
+ ->getStyle ();
162
163
163
- $ menu = (new CliMenuBuilder ($ terminal ))
164
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
165
+ self ::assertEquals (4 , $ style ->getBorderRightWidth ());
166
+ self ::assertEquals (2 , $ style ->getBorderBottomWidth ());
167
+ self ::assertEquals (4 , $ style ->getBorderLeftWidth ());
168
+ self ::assertEquals ('green ' , $ style ->getBorderColour ());
169
+
170
+ $ style = (new CliMenuBuilder ($ terminal ))
164
171
->setBorder (2 , 'green ' )
165
- ->build ();
166
-
167
- $ this ->checkStyleVariable ($ menu , 'borderTopWidth ' , 2 );
168
- $ this ->checkStyleVariable ($ menu , 'borderRightWidth ' , 2 );
169
- $ this ->checkStyleVariable ($ menu , 'borderBottomWidth ' , 2 );
170
- $ this ->checkStyleVariable ($ menu , 'borderLeftWidth ' , 2 );
171
- $ this ->checkStyleVariable ($ menu , 'borderColour ' , 'green ' );
172
+ ->build ()
173
+ ->getStyle ();
174
+
175
+ self ::assertEquals (2 , $ style ->getBorderTopWidth ());
176
+ self ::assertEquals (2 , $ style ->getBorderRightWidth ());
177
+ self ::assertEquals (2 , $ style ->getBorderBottomWidth ());
178
+ self ::assertEquals (2 , $ style ->getBorderLeftWidth ());
179
+ self ::assertEquals ('green ' , $ style ->getBorderColour ());
172
180
}
173
181
174
182
public function testSetBorderTopWidth () : void
@@ -232,10 +240,11 @@ public function test256ColoursCodes() : void
232
240
$ builder = new CliMenuBuilder ($ terminal );
233
241
$ builder ->setBackgroundColour (16 , 'white ' );
234
242
$ builder ->setForegroundColour (206 , 'red ' );
235
- $ menu = $ builder ->build ();
243
+ $ style = $ builder ->build ()
244
+ ->getStyle ();
236
245
237
- $ this -> checkStyleVariable ( $ menu , ' bg ' , 16 );
238
- $ this -> checkStyleVariable ( $ menu , ' fg ' , 206 );
246
+ self :: assertEquals ( 16 , $ style -> getBg () );
247
+ self :: assertEquals ( 206 , $ style -> getFg () );
239
248
240
249
$ terminal = static ::createMock (Terminal::class);
241
250
$ terminal
@@ -246,10 +255,11 @@ public function test256ColoursCodes() : void
246
255
$ builder = new CliMenuBuilder ($ terminal );
247
256
$ builder ->setBackgroundColour (16 , 'white ' );
248
257
$ builder ->setForegroundColour (206 , 'red ' );
249
- $ menu = $ builder ->build ();
258
+ $ style = $ builder ->build ()
259
+ ->getStyle ();
250
260
251
- $ this -> checkStyleVariable ( $ menu , ' bg ' , ' white ' );
252
- $ this -> checkStyleVariable ( $ menu , ' fg ' , ' red ' );
261
+ self :: assertEquals ( ' white ' , $ style -> getBg () );
262
+ self :: assertEquals ( ' red ' , $ style -> getFg () );
253
263
}
254
264
255
265
public function testSetFgThrowsExceptionWhenColourCodeIsNotInRange () : void
@@ -288,8 +298,8 @@ public function testDisableDefaultItems() : void
288
298
$ builder ->disableDefaultItems ();
289
299
290
300
$ menu = $ builder ->build ();
291
-
292
- $ this -> checkVariable ( $ menu , ' items ' , [] );
301
+
302
+ self :: assertEquals ([], $ menu -> getItems () );
293
303
}
294
304
295
305
public function testSetTitle () : void
@@ -299,7 +309,7 @@ public function testSetTitle() : void
299
309
300
310
$ menu = $ builder ->build ();
301
311
302
- $ this -> checkVariable ( $ menu , 'title ' , ' title ' );
312
+ self :: assertEquals ( 'title ' , $ menu -> getTitle () );
303
313
}
304
314
305
315
public function testAddItem () : void
@@ -782,11 +792,15 @@ public function testAddSplitItemWithClosureBinding() : void
782
792
783
793
private function checkMenuItems (CliMenu $ menu , array $ expected ) : void
784
794
{
785
- $ this ->checkItems ($ this -> readAttribute ( $ menu , ' items ' ), $ expected );
795
+ $ this ->checkItems ($ menu -> getItems ( ), $ expected );
786
796
}
787
797
788
798
private function checkItems (array $ actualItems , array $ expected ) : void
789
799
{
800
+ $ propMap = [
801
+ 'breakChar ' => 'getText ' ,
802
+ ];
803
+
790
804
self ::assertCount (count ($ expected ), $ actualItems );
791
805
792
806
foreach ($ expected as $ expectedItem ) {
@@ -796,21 +810,14 @@ private function checkItems(array $actualItems, array $expected) : void
796
810
unset($ expectedItem ['class ' ]);
797
811
798
812
foreach ($ expectedItem as $ property => $ value ) {
799
- self ::assertEquals ($ this ->readAttribute ($ actualItem , $ property ), $ value );
813
+ if (isset ($ propMap [$ property ])) {
814
+ $ getter = $ propMap [$ property ];
815
+ } else {
816
+ $ getter = 'get ' . ucfirst ($ property );
817
+ }
818
+
819
+ self ::assertEquals ($ actualItem ->{$ getter }(), $ value );
800
820
}
801
821
}
802
822
}
803
-
804
-
805
- private function checkVariable (CliMenu $ menu , string $ property , $ expected ) : void
806
- {
807
- $ actual = $ this ->readAttribute ($ menu , $ property );
808
- self ::assertEquals ($ expected , $ actual );
809
- }
810
-
811
- private function checkStyleVariable (CliMenu $ menu , string $ property , $ expected ) : void
812
- {
813
- $ style = $ this ->readAttribute ($ menu , 'style ' );
814
- self ::assertEquals ($ this ->readAttribute ($ style , $ property ), $ expected );
815
- }
816
823
}
0 commit comments