4
4
5
5
use Noodlehaus \Config ;
6
6
use Codedungeon \PHPCliColors \Color ;
7
+ use Noodlehaus \Exception \EmptyDirectoryException ;
7
8
8
9
trait PrinterTrait
9
10
{
11
+ /**
12
+ * @var bool
13
+ */
10
14
protected static $ init = false ;
11
15
/**
12
16
* @var string
@@ -40,10 +44,18 @@ trait PrinterTrait
40
44
* @var string
41
45
*/
42
46
private $ configFileName ;
47
+ /**
48
+ * @var array|null
49
+ */
43
50
private $ printerOptions ;
51
+ /**
52
+ * @var mixed|null
53
+ */
44
54
private $ showConfig ;
45
- private $ passMark ;
46
- private $ failMark ;
55
+ /**
56
+ * @var array
57
+ */
58
+ private $ markers = [];
47
59
48
60
/**
49
61
* {@inheritdoc}
@@ -59,19 +71,29 @@ public function __construct(
59
71
60
72
$ this ->configFileName = $ this ->getConfigurationFile ('phpunit-printer.yml ' );
61
73
$ this ->colorsTool = new Color ();
62
- $ this ->configuration = new Config ($ this ->configFileName );
74
+ try {
75
+ $ this ->configuration = new Config ($ this ->configFileName );
76
+ } catch (EmptyDirectoryException $ e ) {
77
+ echo $ this ->colorsTool ->red () . 'Unable to locate valid configuration file ' . PHP_EOL ;
78
+ echo $ this ->colorsTool ->reset ();
79
+ }
63
80
64
81
$ this ->maxNumberOfColumns = $ this ->getWidth ();
65
- $ this ->maxClassNameLength = min ((int ) ($ this ->maxNumberOfColumns / 2 ), $ this ->maxClassNameLength );
82
+ $ this ->maxClassNameLength = min ((int )($ this ->maxNumberOfColumns / 2 ), $ this ->maxClassNameLength );
66
83
67
84
// setup module options
68
85
$ this ->printerOptions = $ this ->configuration ->all ();
69
86
$ this ->hideClassName = $ this ->configuration ->get ('options.cd-printer-hide-class ' );
70
87
$ this ->simpleOutput = $ this ->configuration ->get ('options.cd-printer-simple-output ' );
71
88
$ this ->showConfig = $ this ->configuration ->get ('options.cd-printer-show-config ' );
72
89
73
- $ this ->passMark = $ this ->configuration ->get ('marks.cd-pass ' );
74
- $ this ->failMark = $ this ->configuration ->get ('marks.cd-fail ' );
90
+ $ this ->markers = [
91
+ 'pass ' => $ this ->configuration ->get ('markers.cd-pass ' ),
92
+ 'fail ' => $ this ->configuration ->get ('markers.cd-fail ' ),
93
+ 'error ' => $ this ->configuration ->get ('markers.cd-error ' ),
94
+ 'skipped ' => $ this ->configuration ->get ('markers.cd-skipped ' ),
95
+ 'incomplete ' => $ this ->configuration ->get ('markers.cd-incomplete ' ),
96
+ ];
75
97
76
98
$ this ->init ();
77
99
}
@@ -102,48 +124,38 @@ public function getConfigurationFile($configFileName = 'phpunit-printer.yml')
102
124
}
103
125
104
126
/**
105
- * @return string | returns package root
127
+ * @return string
106
128
*/
107
- private function getPackageRoot ()
129
+ public function version ()
108
130
{
109
- return \dirname (__FILE__ , 2 );
110
- }
131
+ $ content = file_get_contents ($ this ->getPackageRoot () . DIRECTORY_SEPARATOR . 'composer.json ' );
132
+ if ($ content ) {
133
+ $ content = json_decode ($ content , true );
111
134
112
- /**
113
- * @return bool
114
- */
115
- private function isWindows ()
116
- {
117
- return strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' ;
135
+ return $ content ['version ' ];
136
+ }
137
+
138
+ return 'n/a ' ;
118
139
}
119
140
120
141
/**
121
- * Gets the terminal width.
122
- *
123
- * @return int
142
+ * @return string
124
143
*/
125
- private function getWidth ()
144
+ public function packageName ()
126
145
{
127
- $ width = 0 ;
128
- if ($ this ->isWindows ()) {
129
- return 96 ; // create a default width to be used on windows
130
- }
131
-
132
- exec ('stty size 2>/dev/null ' , $ out , $ exit );
133
-
134
- // 'stty size' output example: 36 120
135
- if (\count ($ out ) > 0 ) {
136
- $ width = (int ) explode (' ' , array_pop ($ out ))[1 ];
137
- }
146
+ $ content = file_get_contents ($ this ->getPackageRoot () . DIRECTORY_SEPARATOR . 'composer.json ' );
147
+ if ($ content ) {
148
+ $ content = json_decode ($ content , true );
138
149
139
- // handle CircleCI case (probably the same with TravisCI as well)
140
- if ($ width === 0 ) {
141
- $ width = 96 ;
150
+ return $ content ['description ' ];
142
151
}
143
152
144
- return $ width ;
153
+ return ' n/a ' ;
145
154
}
146
155
156
+ /**
157
+ *
158
+ */
147
159
protected function init ()
148
160
{
149
161
if (!self ::$ init ) {
@@ -154,8 +166,11 @@ protected function init()
154
166
echo $ this ->colorsTool ->reset ();
155
167
156
168
if ($ this ->showConfig ) {
157
- echo $ this ->colorsTool ->white () . 'Configuration: ' ;
158
- echo $ this ->colorsTool ->white () . $ this ->configFileName ;
169
+ $ home = getenv ('HOME ' );
170
+ $ filename = str_replace ($ home , '~ ' , $ this ->configFileName );
171
+
172
+ echo $ this ->colorsTool ->yellow () . 'Configuration: ' ;
173
+ echo $ this ->colorsTool ->yellow () . $ filename ;
159
174
echo $ this ->colorsTool ->reset ();
160
175
echo PHP_EOL . PHP_EOL ;
161
176
}
@@ -164,33 +179,9 @@ protected function init()
164
179
}
165
180
}
166
181
167
- public function version ()
168
- {
169
- $ content = file_get_contents ($ this ->getPackageRoot () . DIRECTORY_SEPARATOR . 'composer.json ' );
170
- if ($ content ) {
171
- $ content = json_decode ($ content , true );
172
-
173
- return $ content ['version ' ];
174
- }
175
-
176
- return 'n/a ' ;
177
- }
178
-
179
182
/**
180
- * @return string
183
+ * @param $progress
181
184
*/
182
- public function packageName ()
183
- {
184
- $ content = file_get_contents ($ this ->getPackageRoot () . DIRECTORY_SEPARATOR . 'composer.json ' );
185
- if ($ content ) {
186
- $ content = json_decode ($ content , true );
187
-
188
- return $ content ['description ' ];
189
- }
190
-
191
- return 'n/a ' ;
192
- }
193
-
194
185
protected function writeProgressEx ($ progress )
195
186
{
196
187
if (!$ this ->debug ) {
@@ -218,6 +209,61 @@ protected function printClassName()
218
209
$ this ->lastClassName = $ this ->className ;
219
210
}
220
211
212
+ /**
213
+ * {@inheritdoc}
214
+ */
215
+ protected function writeProgressWithColorEx ($ color , $ buffer )
216
+ {
217
+ if (!$ this ->debug ) {
218
+ $ this ->printClassName ();
219
+ }
220
+
221
+ $ this ->printTestCaseStatus ($ color , $ buffer );
222
+ }
223
+
224
+ /**
225
+ * @return string | returns package root
226
+ */
227
+ private function getPackageRoot ()
228
+ {
229
+ return \dirname (__FILE__ , 2 );
230
+ }
231
+
232
+ /**
233
+ * @return bool
234
+ */
235
+ private function isWindows ()
236
+ {
237
+ return strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' ;
238
+ }
239
+
240
+ /**
241
+ * Gets the terminal width.
242
+ *
243
+ * @return int
244
+ */
245
+ private function getWidth ()
246
+ {
247
+ $ width = 0 ;
248
+ if ($ this ->isWindows ()) {
249
+ return 96 ; // create a default width to be used on windows
250
+ }
251
+
252
+ exec ('stty size 2>/dev/null ' , $ out , $ exit );
253
+
254
+ // 'stty size' output example: 36 120
255
+ if (\count ($ out ) > 0 ) {
256
+ $ width = (int )explode (' ' , array_pop ($ out ))[1 ];
257
+ }
258
+
259
+ // handle CircleCI case (probably the same with TravisCI as well)
260
+ if ($ width === 0 ) {
261
+ $ width = 96 ;
262
+ }
263
+
264
+ return $ width ;
265
+ }
266
+
221
267
/**
222
268
* @param string $className
223
269
*
@@ -268,27 +314,27 @@ private function printTestCaseStatus($color, $buffer)
268
314
switch (strtoupper ($ buffer )) {
269
315
case '. ' :
270
316
$ color = 'fg-green,bold ' ;
271
- $ buffer = $ this ->simpleOutput ? '. ' : mb_convert_encoding ("\x27\x13" , 'UTF-8 ' , 'UTF-16BE ' );
317
+ $ buffer = $ this ->simpleOutput ? '. ' : $ this -> markers [ ' pass ' ]; // mb_convert_encoding("\x27\x13", 'UTF-8', 'UTF-16BE');
272
318
$ buffer .= (!$ this ->debug ) ? '' : ' Passed ' ;
273
319
break ;
274
320
case 'S ' :
275
321
$ color = 'fg-yellow,bold ' ;
276
- $ buffer = $ this ->simpleOutput ? 'S ' : mb_convert_encoding ("\x27\xA6" , 'UTF-8 ' , 'UTF-16BE ' );
322
+ $ buffer = $ this ->simpleOutput ? 'S ' : $ this -> markers [ ' skipped ' ]; // mb_convert_encoding("\x27\xA6", 'UTF-8', 'UTF-16BE');
277
323
$ buffer .= !$ this ->debug ? '' : ' Skipped ' ;
278
324
break ;
279
325
case 'I ' :
280
326
$ color = 'fg-blue,bold ' ;
281
- $ buffer = $ this ->simpleOutput ? 'I ' : 'ℹ ' ;
327
+ $ buffer = $ this ->simpleOutput ? 'I ' : $ this -> markers [ ' incomplete ' ]; // 'ℹ';
282
328
$ buffer .= !$ this ->debug ? '' : ' Incomplete ' ;
283
329
break ;
284
330
case 'F ' :
285
331
$ color = 'fg-red,bold ' ;
286
- $ buffer = $ this ->simpleOutput ? 'F ' : mb_convert_encoding ("\x27\x16" , 'UTF-8 ' , 'UTF-16BE ' );
332
+ $ buffer = $ this ->simpleOutput ? 'F ' : $ this -> markers [ ' fail ' ]; // mb_convert_encoding("\x27\x16", 'UTF-8', 'UTF-16BE');
287
333
$ buffer .= (!$ this ->debug ) ? '' : ' Fail ' ;
288
334
break ;
289
335
case 'E ' :
290
336
$ color = 'fg-red,bold ' ;
291
- $ buffer = $ this ->simpleOutput ? 'E ' : '⚈ ' ;
337
+ $ buffer = $ this ->simpleOutput ? 'E ' : $ this -> makers [ ' error ' ]; // '⚈';
292
338
$ buffer .= !$ this ->debug ? '' : ' Error ' ;
293
339
break ;
294
340
}
@@ -300,16 +346,4 @@ private function printTestCaseStatus($color, $buffer)
300
346
}
301
347
$ this ->column += 2 ;
302
348
}
303
-
304
- /**
305
- * {@inheritdoc}
306
- */
307
- protected function writeProgressWithColorEx ($ color , $ buffer )
308
- {
309
- if (!$ this ->debug ) {
310
- $ this ->printClassName ();
311
- }
312
-
313
- $ this ->printTestCaseStatus ($ color , $ buffer );
314
- }
315
349
}
0 commit comments