StandardConsoleTemplate   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A colors() 0 4 1
A render() 0 36 5
B getFail() 0 22 7
1
<?php
2
/**
3
 * ShouldPHP
4
 *
5
 * @author  Gabriel Jacinto <[email protected]>
6
 * @status  dev
7
 * @link    https://github.com/GabrielJMJ/ShouldPHP
8
 * @license MIT
9
 */
10
 
11
namespace Gabrieljmj\Should\Template\Console;
12
13
use Gabrieljmj\Should\Template\TemplateInterface;
14
use Gabrieljmj\Should\Report\Report;
15
16
class StandardConsoleTemplate implements TemplateInterface
17
{
18
    /**
19
     * Colors are enabled or desabled
20
     *
21
     * @var boolean
22
     */
23
    private $colors = false;
24
    
25
    /**
26
     * Enable colors for tests
27
     * 
28
     * @param boolean $enable
29
     */
30
    public function colors($enable = true)
31
    {
32
        $this->colors = $enable;
33
    }
34
    
35
    /**
36
     * Return the created content by determined report
37
     * 
38
     * @param \Gabrieljmj\Should\Report\Report $report
39
     * @return string
40
     */
41
    public function render(Report $report)
42
    {
43
        $should = " 
44
 ____  _   _ _____ _   _ _    _____
45
/  _ \| | | |  _  | | | | |  |  _  \ 
46
| | |_| | | | | | | | | | |  | | \ |
47
| |__ | |_| | | | | | | | |  | | | |
48
|___ \|  _  | | | | | | | |  | | | |
49
 _  | | | | | | | | | | | |  | | | | __      __
50
| | | | | | | | | | | | | |  | | | ||  \|  ||  \
51
| |_| | | | | |_| | |_| | |__| |_/ ||__/|__||__/
52
\____/|_| |_|_____|_____|____|_____/|   |  ||\n";
53
54
        $return = $this->colors ? '<comment>' . $should . '</comment>' : $should;
55
56
        if ($report->getTotal() > 0) {
57
            if (count($this->getFail($report))) {
58
                $return .= "\nREPORT\n--------------------------\n";
59
                $return .= implode("\n\n", $this->getFail($report));
60
            }
61
62
            $total = $report->getTotal();
63
            $success = $report->getSuccessTotal();
64
            $fail = $report->getFailTotal();
65
            $time = round((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) * 100) / 100;
66
67
            $return .= "\n\nRESULT\n--------------------------\n";
68
            $info = "Total: {$total}\nSuccess: {$success}\nFail: {$fail}\nExecution time: {$time}";
69
            $return .= $this->colors ? '<info>' . $info . '</info>' : $info;
70
        } else {
71
            $return .= "-------------------------------------------------\nNo tests executed!";
72
        }
73
74
        return $return;
75
76
    }
77
78
    private function getFail(Report $report)
79
    {
80
        $return = [];
81
        $assertList = $report->getAssertList();
82
83
        foreach ($assertList as $testType => $value) {
84
            if (isset($assertList[$testType]['fail'])) {
85
                foreach ($assertList[$testType]['fail'] as $element => $fails) {
86
                    $return[] = "Fail on tests of the {$testType} {$element}:\n";
87
                    foreach ($fails as $key => $fail) {
88
                        $n = $key + 1;
89
                        $name = $fail->getName();
90
                        $failmsg = $fail->getMessage() !== null ? $fail->getMessage() : $fail->getFailMessage();
91
                        $li = "{$n}) {$name} - {$failmsg}";
92
                        $return[] = $this->colors ? '<error>' . $li . '</error>' : $li;
93
                    }
94
                }
95
            }
96
        }
97
98
        return $return;
99
    }
100
}
101