Completed
Push — master ( d2992a...8fe16f )
by Michał
06:47
created

testShowShortReportExecuteWithErrors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 13

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
dl 35
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace CloverReporterTest\Console;
4
5
use Symfony\Component\Console\Tester\CommandTester;
6
use Symfony\Component\Console\Application;
7
use PHPUnit\Framework\TestCase;
8
use CloverReporter\Console\Commands;
9
10
class CommandsTest extends TestCase
11
{
12
    use \CloverReporterTest\Helper;
13
14
    public function setUp()
15
    {
16
        $this->copyFixedReports('clover_log.xml');
17
        $this->copyFixedReports('clover_100_percent.xml');
18
    }
19
20
    /**
21
     * @expectedException \InvalidArgumentException
22
     */
23
    public function testIncorrectReportFile()
24
    {
25
        $this->prepareCommand();
26
    }
27
28
    public function testBasicExecute()
29
    {
30
        $commandTester = $this->prepareCommand([
31
            'report_file' => $this->reportPaths['fix'] . 'clover_log.xml',
32
            '--skip-dir' => '',
33
        ]);
34
35
        $output = <<<EOT
36
37
Clover report generator.
38
========================
39
40
[Coverage report file] tests/reports/fixed/clover_log.xml
41
42
Total coverage: 61.404% 
43
EOT;
44
45
        $this->assertEquals(
46
            $output,
47
            $this->clearExecutionTime($commandTester->getDisplay())
48
        );
49
    }
50
51
    public function testShowCoverageExecute()
52
    {
53
        $commandTester = $this->prepareCommand([
54
            'report_file' => $this->reportPaths['fix'] . 'clover_log.xml',
55
            '--skip-dir' => '',
56
            '--show-coverage' => true,
57
        ]);
58
59
        $output = <<<EOT
60
61
Clover report generator.
62
========================
63
64
[Coverage report file] tests/reports/fixed/clover_log.xml
65
66
Found 3 source files:
67
  - 84.211%     SimpleLog\Log
68
  - 100%        SimpleLog\LogStatic
69
  - 0%          SimpleLog\Message\DefaultJsonMessage
70
71
Total coverage: 61.404% 
72
EOT;
73
74
        $this->assertEquals(
75
            $output,
76
            $this->clearExecutionTime($commandTester->getDisplay())
77
        );
78
    }
79
80 View Code Duplication
    public function testShowShortReportExecute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $commandTester = $this->prepareCommand([
83
            'report_file' => $this->reportPaths['fix'] . 'clover_log.xml',
84
            '--skip-dir' => '',
85
            '--short-report' => true
86
        ]);
87
88
        $output = <<<EOT
89
90
Clover report generator.
91
========================
92
93
[Coverage report file] tests/reports/fixed/clover_log.xml
94
95
Found 3 source files:
96
  - 84.211%     SimpleLog\Log
97
93:         {
98
94:             if (\$this->defaultParams['storage'] instanceof StorageInterface) {
99
107:        {
100
108:            if (\$this->defaultParams['message'] instanceof MessageInterface) {
101
102
  - 100%        SimpleLog\LogStatic
103
  - 0%          SimpleLog\Message\DefaultJsonMessage
104
18:          * @param string|array|object \$message
105
21:          */
106
26:             list(\$date, \$time) = explode(';', strftime(self::DATE_FORMAT . ';' . self::TIME_FORMAT, time()));
107
28:             \$this->messageScheme['date'] = \$date;
108
109
110
Total coverage: 61.404% 
111
EOT;
112
113
        $this->assertEquals(
114
            $output,
115
            $this->clearSpaces(
116
                $this->clearExecutionTime(
117
                    $commandTester->getDisplay()
118
                )
119
            )
120
        );
121
    }
122
123 View Code Duplication
    public function testShowShortReportExecuteWithErrors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $commandTester = $this->prepareCommand([
126
            'report_file' => $this->reportPaths['base'] . 'clover_log.xml',
127
            '--skip-dir' => '',
128
            '--short-report' => true
129
        ]);
130
131
        $output = <<<EOT
132
133
Clover report generator.
134
========================
135
136
[Coverage report file] tests/reports/clover_log.xml
137
138
Found 3 source files:
139
  - 84.211%     SimpleLog\\Log
140
[ERROR]              File don't exists: {\$path}log/src/Log.php
141
  - 100%        SimpleLog\\LogStatic
142
[ERROR]              File don't exists: {\$path}log/src/LogStatic.php
143
  - 0%          SimpleLog\\Message\\DefaultJsonMessage
144
[ERROR]              File don't exists: {\$path}log/src/DefaultJsonMessage.php
145
146
Total coverage: 61.404% 
147
EOT;
148
149
        $this->assertEquals(
150
            $output,
151
            $this->clearSpaces(
152
                $this->clearExecutionTime(
153
                    $commandTester->getDisplay()
154
                )
155
            )
156
        );
157
    }
158
159 View Code Duplication
    public function testFullReport()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $commandTester = $this->prepareCommand([
162
            'report_file' => $this->reportPaths['fix'] . 'clover_log.xml',
163
            '--skip-dir' => '',
164
            '--full-report' => true
165
        ]);
166
167
        $output = <<<EOT
168
169
Clover report generator.
170
========================
171
172
[Coverage report file] tests/reports/fixed/clover_log.xml
173
174
  - 84.211%     SimpleLog\\Log
175
0:-:      <?php
176
1:-:
177
2:-:      namespace SimpleLog;
178
3:-:
179
4:-:      use Psr\\Log\\LoggerInterface;
180
5:-:      use Psr\\Log\\LoggerTrait;
181
6:-:      use Psr\\Log\\LogLevel;
182
7:-:      use Psr\\Log\\InvalidArgumentException;
183
8:-:      use SimpleLog\\Storage\\StorageInterface;
184
9:-:      use SimpleLog\\Message\\MessageInterface;
185
10:-:
186
11:-:     class Log implements LogInterface, LoggerInterface
187
12:-:     {
188
13:-:         use LoggerTrait;
189
14:-:
190
15:-:         /**
191
16:-:          * @var array
192
17:-:          */
193
18:-:         protected \$defaultParams = [
194
19:-:             'log_path' => './log',
195
20:-:             'level' => 'notice',
196
21:-:             'storage' => \\SimpleLog\\Storage\\File::class,
197
22:-:             'message' => \\SimpleLog\\Message\\DefaultMessage::class,
198
23:-:         ];
199
24:-:
200
25:-:         /**
201
26:-:          * @var \\SimpleLog\\Storage\\StorageInterface
202
27:-:          */
203
28:-:         protected \$storage;
204
29:-:
205
30:-:         /**
206
31:-:          * @var array
207
32:-:          */
208
33:-:         protected \$levels = [];
209
34:-:
210
35:-:         /**
211
36:-:          * @var \\SimpleLog\\Message\\MessageInterface
212
37:-:          */
213
38:-:         protected \$message;
214
39:-:
215
40:-:         /**
216
41:-:          * @param array \$params
217
42:-:          * @throws \\ReflectionException
218
43:-:          */
219
44:9:         public function __construct(array \$params = [])
220
45:-:         {
221
46:9:             \$this->defaultParams = array_merge(\$this->defaultParams, \$params);
222
47:-:
223
48:9:             \$levels = new \\ReflectionClass(new LogLevel);
224
49:9:             \$this->levels = \$levels->getConstants();
225
50:-:
226
51:9:             \$this->reloadStorage();
227
52:9:             \$this->reloadMessage();
228
53:9:         }
229
54:-:
230
55:-:         /**
231
56:-:          * log event information into file
232
57:-:          *
233
58:-:          * @param array|string|object \$message
234
59:-:          * @param array \$context
235
60:-:          * @return \$this
236
61:-:          */
237
62:8:         public function makeLog(\$message, array \$context = [])
238
63:-:         {
239
64:8:             \$this->log(\$this->defaultParams['level'], \$message, \$context);
240
65:-:
241
66:-:             return \$this;
242
67:-:         }
243
68:-:
244
69:-:         /**
245
70:-:          * @param string \$level
246
71:-:          * @param string|array|object \$message
247
72:-:          * @param array \$context
248
73:10:          * @throws \\Psr\\Log\\InvalidArgumentException
249
74:-:          */
250
75:10:         public function log(\$level, \$message, array \$context = [])
251
76:1:         {
252
77:-:             if (!in_array(\$level, \$this->levels, true)) {
253
78:-:                 throw new InvalidArgumentException('Level not defined: ' . \$level);
254
79:9:             }
255
80:9:
256
81:8:             \$newMessage = \$this->message
257
82:-:                 ->createMessage(\$message, \$context)
258
83:8:                 ->getMessage();
259
84:8:
260
85:-:             \$this->storage->store(\$newMessage, \$level);
261
86:-:         }
262
87:-:
263
88:-:         /**
264
89:10:          * @return \$this
265
90:-:          */
266
91:10:         protected function reloadStorage()
267
93:0:         {
268
94:0:             if (\$this->defaultParams['storage'] instanceof StorageInterface) {
269
94:-:                 \$this->storage = \$this->defaultParams['storage'];
270
95:-:                 return \$this;
271
96:10:             }
272
97:10:
273
98:-:             \$this->storage = new \$this->defaultParams['storage'](\$this->defaultParams);
274
99:-:             return \$this;
275
100:-:        }
276
101:-:
277
102:-:        /**
278
103:9:         * @return \$this
279
104:-:         */
280
105:9:        protected function reloadMessage()
281
107:0:        {
282
108:0:            if (\$this->defaultParams['message'] instanceof MessageInterface) {
283
108:-:                \$this->message = \$this->defaultParams['message'];
284
109:-:                return \$this;
285
110:9:            }
286
111:9:
287
112:-:            \$this->message = new \$this->defaultParams['message'](\$this->defaultParams);
288
113:-:            return \$this;
289
114:-:        }
290
115:-:
291
116:-:        /**
292
117:-:         * set log option for all future executions of makeLog
293
118:-:         *
294
119:-:         * @param string \$key
295
120:-:         * @param mixed \$val
296
121:3:         * @return \$this
297
122:-:         */
298
123:3:        public function setOption(\$key, \$val)
299
124:3:        {
300
125:-:            \$this->defaultParams[\$key] = \$val;
301
126:-:            return \$this->reloadStorage();
302
127:-:        }
303
128:-:
304
129:-:        /**
305
130:-:         * return all configuration or only given key value
306
131:-:         *
307
132:-:         * @param null|string \$key
308
133:3:         * @return array|mixed
309
134:-:         */
310
135:3:        public function getOption(\$key = null)
311
136:1:        {
312
137:-:            if (is_null(\$key)) {
313
138:-:                return \$this->defaultParams;
314
139:3:            }
315
140:-:
316
141:-:            return \$this->defaultParams[\$key];
317
142:-:        }
318
143:-:
319
144:-:        /**
320
145:1:         * @return string
321
146:-:         */
322
147:1:        public function getLastMessage()
323
148:-:        {
324
149:-:            return \$this->message->getMessage();
325
150:-:        }
326
151:-:    }
327
152:-:
328
329
  - 100%        SimpleLog\\LogStatic
330
0:-:      <?php
331
1:-:
332
2:-:      namespace SimpleLog;
333
3:-:
334
4:-:      class LogStatic
335
5:-:      {
336
6:-:          /**
337
7:-:           * @var Log
338
8:-:           */
339
9:-:          protected static \$instance;
340
10:-:
341
11:-:         /**
342
12:-:          * log event information into file
343
13:-:          *
344
14:-:          * @param string \$level
345
15:-:          * @param array|string \$message
346
16:-:          * @param array \$context
347
17:-:          * @param array \$params
348
18:-:          */
349
19:-:         public static function log(\$level, \$message, array \$context = [], array \$params = [])
350
20:1:         {
351
21:-:             self::init(\$params);
352
22:1:             self::\$instance->log(\$level, \$message, \$context);
353
23:1:         }
354
24:-:
355
25:-:         /**
356
26:-:          * log event information into file
357
27:-:          *
358
28:-:          * @param array|string \$message
359
29:-:          * @param array \$context
360
30:-:          * @param array \$params
361
31:-:          */
362
32:-:         public static function makeLog(\$message, array \$context = [], array \$params = [])
363
33:-:         {
364
34:1:             self::init(\$params);
365
35:-:             self::\$instance->makeLog(\$message, \$context);
366
36:1:         }
367
37:1:
368
38:-:         /**
369
39:-:          * set log option for all future executions of makeLog
370
40:-:          *
371
41:-:          * @param string \$key
372
42:-:          * @param mixed \$val
373
43:-:          * @return Log
374
44:-:          */
375
45:-:         public static function setOption(\$key, \$val)
376
46:-:         {
377
47:2:             self::init();
378
48:-:             return self::\$instance->setOption(\$key, \$val);
379
49:2:         }
380
50:2:
381
51:-:         /**
382
52:-:          * return all configuration or only given key value
383
53:-:          *
384
54:-:          * @param null|string \$key
385
55:-:          * @return array|mixed
386
56:-:          */
387
57:-:         public static function getOption(\$key = null)
388
58:-:         {
389
59:2:             self::init();
390
60:-:             return self::\$instance->getOption(\$key);
391
61:2:         }
392
62:2:
393
63:-:         /**
394
64:-:          * create Log object if not exists
395
65:-:          *
396
66:-:          * @param array \$params
397
67:-:          */
398
68:-:         protected static function init(array \$params = [])
399
69:-:         {
400
70:2:             if (is_null(self::\$instance)) {
401
71:-:                 self::\$instance = new Log(\$params);
402
72:2:             }
403
73:1:         }
404
74:-:     }
405
75:2:
406
407
  - 0%          SimpleLog\\Message\\DefaultJsonMessage
408
0:-:      <?php
409
1:-:
410
2:-:      namespace SimpleLog\\Message;
411
3:-:
412
4:-:      class DefaultJsonMessage extends DefaultMessage
413
5:-:      {
414
6:-:          /**
415
7:-:           * @var array
416
8:-:           */
417
9:-:          protected \$messageScheme = [];
418
10:-:
419
11:-:         /**
420
12:-:          * @var array
421
13:-:          */
422
14:-:         protected \$context = [];
423
15:-:
424
16:-:         /**
425
18:0:          * @param string|array|object \$message
426
18:-:          * @param array \$context
427
19:-:          * @return \$this
428
21:0:          */
429
21:-:         public function createMessage(\$message, array \$context)
430
22:-:         {
431
23:-:             \$this->context = \$context;
432
24:-:
433
26:0:             list(\$date, \$time) = explode(';', strftime(self::DATE_FORMAT . ';' . self::TIME_FORMAT, time()));
434
26:-:
435
28:0:             \$this->messageScheme['date'] = \$date;
436
28:-:             \$this->messageScheme['time'] = \$time;
437
29:-:
438
30:-:             if (method_exists(\$message, '__toString')) {
439
31:-:                 \$message = (string)\$message;
440
32:-:             }
441
33:-:
442
34:-:             \$this->messageScheme['data'] = \$message;
443
35:-:
444
36:-:             return \$this;
445
37:-:         }
446
38:-:
447
39:-:         /**
448
40:-:          * @return string
449
41:-:          */
450
42:-:         public function getMessage()
451
43:-:         {
452
44:-:             \$this->message = json_encode(\$this->messageScheme);
453
45:-:             \$this->buildContext(\$this->context);
454
46:-:
455
47:-:             return \$this->message;
456
48:-:         }
457
49:-:     }
458
50:-:
459
460
Total coverage: 61.404% 
461
EOT;
462
463
        $this->assertEquals(
464
            $output,
465
            $this->clearSpaces(
466
                $this->clearExecutionTime(
467
                    $commandTester->getDisplay()
468
                )
469
            )
470
        );
471
    }
472
473
    public function testFullCovered()
474
    {
475
        $commandTester = $this->prepareCommand([
476
            'report_file' => $this->reportPaths['fix'] . 'clover_100_percent.xml',
477
            '--skip-dir' => '',
478
            '--show-coverage' => true,
479
        ]);
480
481
        $output = <<<EOT
482
483
Clover report generator.
484
========================
485
486
[Coverage report file] tests/reports/fixed/clover_100_percent.xml
487
488
Found 3 source files:
489
  - 100%        BlueEvent\\Event\\Base\\Event
490
  - 100%        BlueEvent\\Event\\Base\\EventDispatcher
491
  - 100%        BlueEvent\\Event\\Base\\EventLog
492
493
Total coverage: 100% 🍺🍺🍺
494
EOT;
495
496
        $this->assertEquals(
497
            $output,
498
            $this->clearExecutionTime($commandTester->getDisplay())
499
        );
500
    }
501
502
    /**
503
     * @param string $report
504
     * @return string
505
     */
506
    protected function clearExecutionTime($report)
507
    {
508
        return substr($report, 0, strrpos($report, "\n[Execution"));
509
    }
510
511
    /**
512
     * @param string $report
513
     * @return string
514
     */
515
    protected function clearSpaces($report)
516
    {
517
        return preg_replace('#[ ]+\n#', "\n", $report);
518
    }
519
520
    /**
521
     * @param array $parameters
522
     * @return CommandTester
523
     */
524
    protected function prepareCommand(array $parameters = [])
525
    {
526
        $application = new Application;
527
528
        $application->add(new Commands);
529
530
        $command = $application->find('reporter');
531
532
        $commandTester = new CommandTester($command);
533
534
        $commandTester->execute(
535
            array_merge(['command' => $command->getName()], $parameters),
536
            ['decorated' => false]
537
        );
538
539
        return $commandTester;
540
    }
541
}
542