Completed
Push — stable ( 8ba0c8...0d8cdb )
by Nuno
17:40 queued 15:16
created

TestResult::makeDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 7
cp 0
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of Collision.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\Collision\Adapters\Phpunit;
13
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @internal
18
 */
19
final class TestResult
20
{
21
    public const FAIL = 'failed';
22
    public const SKIPPED = 'skipped';
23
    public const INCOMPLETE = 'incompleted';
24
    public const RISKY = 'risked';
25
    public const WARN = 'warnings';
26
    public const RUNS = 'pending';
27
    public const PASS = 'passed';
28
29
    /**
30
     * @readonly
31
     *
32
     * @var string
33
     */
34
    public $description;
35
36
    /**
37
     * @readonly
38
     *
39
     * @var string
40
     */
41
    public $type;
42
43
    /**
44
     * @readonly
45
     *
46
     * @var string
47
     */
48
    public $icon;
49
50
    /**
51
     * @readonly
52
     *
53
     * @var string
54
     */
55
    public $color;
56
57
    /**
58
     * @readonly
59
     *
60
     * @var null|string
61
     */
62
    public $warning;
63
64
    /**
65
     * Test constructor.
66
     *
67
     * @param  string  $description
68
     * @param  string  $type
69
     * @param  string  $icon
70
     * @param  string  $color
71
     * @param  string  $warning
72
     */
73
    private function __construct(string $description, string $type, string $icon, string $color, string $warning = null)
74
    {
75
        $this->description = $description;
76
        $this->type = $type;
77
        $this->icon = $icon;
78
        $this->color = $color;
79
        $this->warning = trim((string) preg_replace("/\r|\n/", ' ', (string) $warning));
80
    }
81
82
    /**
83
     * Creates a new test from the given test case.
84
     *
85
     * @param  TestCase  $testCase
86
     *
87
     * @return self
88
     */
89
    public static function fromTestCase(TestCase $testCase, string $type, string $warning = null): self
90
    {
91
        $description = self::makeDescription($testCase);
92
93
        $icon = self::makeIcon($type);
94
95
        $color = self::makeColor($type);
96
97
        return new self($description, $type, $icon, $color, $warning);
98
    }
99
100
    /**
101
     * Get the test case description.
102
     *
103
     * @return string
104
     */
105
    public static function makeDescription(TestCase $testCase): string
106
    {
107
        $name = $testCase->getName(true);
108
109
        // First, lets replace underscore by spaces.
110
        $name = str_replace('_', ' ', $name);
111
112
        // Then, replace upper cases by spaces.
113
        $name = (string) preg_replace('/([A-Z])/', ' $1', $name);
114
115
        // Finally, if it starts with `test`, we remove it.
116
        $name = (string) preg_replace('/^test/', '', $name);
117
118
        // Removes spaces
119
        $name = (string) trim($name);
120
121
        // Finally, lower case everything
122
        return (string) mb_strtolower($name);
123
    }
124
125
    /**
126
     * Get the test case icon.
127
     *
128
     * @return string
129
     */
130
    public static function makeIcon(string $type): string
131
    {
132
        switch ($type) {
133
            case self::FAIL:
134
                return '✕';
135
            case self::SKIPPED:
136
                return 's';
137
            case self::RISKY:
138
                return 'r';
139
            case self::INCOMPLETE:
140
                return 'i';
141
            case self::WARN:
142
                return 'w';
143
            case self::RUNS:
144
                return '•';
145
            default:
146
                return '✓';
147
        }
148
    }
149
150
    /**
151
     * Get the test case color.
152
     *
153
     * @return string
154
     */
155
    public static function makeColor(string $type): string
156
    {
157
        switch ($type) {
158
            case self::FAIL:
159
                return 'red';
160
            case self::SKIPPED:
161
            case self::INCOMPLETE:
162
            case self::RISKY:
163
            case self::WARN:
164
            case self::RUNS:
165
                return 'yellow';
166
            default:
167
                return 'green';
168
        }
169
    }
170
}
171