TestResult::makeDescription()   A
last analyzed

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 string|null
61
     */
62
    public $warning;
63
64
    /**
65
     * Test constructor.
66
     *
67
     * @param string $warning
68
     */
69
    private function __construct(string $description, string $type, string $icon, string $color, string $warning = null)
70
    {
71
        $this->description = $description;
72
        $this->type        = $type;
73
        $this->icon        = $icon;
74
        $this->color       = $color;
75
        $this->warning     = trim((string) preg_replace("/\r|\n/", ' ', (string) $warning));
76
    }
77
78
    /**
79
     * Creates a new test from the given test case.
80
     */
81
    public static function fromTestCase(TestCase $testCase, string $type, string $warning = null): self
82
    {
83
        $description = self::makeDescription($testCase);
84
85
        $icon = self::makeIcon($type);
86
87
        $color = self::makeColor($type);
88
89
        return new self($description, $type, $icon, $color, $warning);
90
    }
91
92
    /**
93
     * Get the test case description.
94
     */
95
    public static function makeDescription(TestCase $testCase): string
96
    {
97
        $name = $testCase->getName(true);
98
99
        // First, lets replace underscore by spaces.
100
        $name = str_replace('_', ' ', $name);
101
102
        // Then, replace upper cases by spaces.
103
        $name = (string) preg_replace('/([A-Z])/', ' $1', $name);
104
105
        // Finally, if it starts with `test`, we remove it.
106
        $name = (string) preg_replace('/^test/', '', $name);
107
108
        // Removes spaces
109
        $name = (string) trim($name);
110
111
        // Finally, lower case everything
112
        return (string) mb_strtolower($name);
113
    }
114
115
    /**
116
     * Get the test case icon.
117
     */
118
    public static function makeIcon(string $type): string
119
    {
120
        switch ($type) {
121
            case self::FAIL:
122
                return '✕';
123
            case self::SKIPPED:
124
                return 's';
125
            case self::RISKY:
126
                return 'r';
127
            case self::INCOMPLETE:
128
                return 'i';
129
            case self::WARN:
130
                return 'w';
131
            case self::RUNS:
132
                return '•';
133
            default:
134
                return '✓';
135
        }
136
    }
137
138
    /**
139
     * Get the test case color.
140
     */
141
    public static function makeColor(string $type): string
142
    {
143
        switch ($type) {
144
            case self::FAIL:
145
                return 'red';
146
            case self::SKIPPED:
147
            case self::INCOMPLETE:
148
            case self::RISKY:
149
            case self::WARN:
150
            case self::RUNS:
151
                return 'yellow';
152
            default:
153
                return 'green';
154
        }
155
    }
156
}
157