Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace NunoMaduro\Collision\Adapters\Phpunit;
6
 
7
use NunoMaduro\Collision\Contracts\Adapters\Phpunit\HasPrintableTestCaseName;
8
use PHPUnit\Framework\TestCase;
9
use Throwable;
10
 
11
/**
12
 * @internal
13
 */
14
final class TestResult
15
{
16
    public const FAIL = 'failed';
17
 
18
    public const SKIPPED = 'skipped';
19
 
20
    public const INCOMPLETE = 'incomplete';
21
 
22
    public const RISKY = 'risky';
23
 
24
    public const WARN = 'warnings';
25
 
26
    public const RUNS = 'pending';
27
 
28
    public const PASS = 'passed';
29
 
30
    /**
31
     * @readonly
32
     *
33
     * @var string
34
     */
35
    public $testCaseName;
36
 
37
    /**
38
     * @readonly
39
     *
40
     * @var string
41
     */
42
    public $description;
43
 
44
    /**
45
     * @readonly
46
     *
47
     * @var string
48
     */
49
    public $type;
50
 
51
    /**
52
     * @readonly
53
     *
54
     * @var string
55
     */
56
    public $icon;
57
 
58
    /**
59
     * @readonly
60
     *
61
     * @var string
62
     */
63
    public $color;
64
 
65
    /**
66
     * @readonly
67
     *
68
     * @var Throwable|null
69
     */
70
    public $throwable;
71
 
72
    /**
73
     * @readonly
74
     *
75
     * @var string
76
     */
77
    public $warning = '';
78
 
79
    /**
80
     * Test constructor.
81
     */
82
    private function __construct(string $testCaseName, string $description, string $type, string $icon, string $color, Throwable $throwable = null)
83
    {
84
        $this->testCaseName = $testCaseName;
85
        $this->description = $description;
86
        $this->type = $type;
87
        $this->icon = $icon;
88
        $this->color = $color;
89
        $this->throwable = $throwable;
90
 
91
        $asWarning = $this->type === TestResult::WARN
92
             || $this->type === TestResult::RISKY
93
             || $this->type === TestResult::SKIPPED
94
             || $this->type === TestResult::INCOMPLETE;
95
 
96
        if ($throwable instanceof Throwable && $asWarning) {
97
            $this->warning = trim((string) preg_replace("/\r|\n/", ' ', $throwable->getMessage()));
98
        }
99
    }
100
 
101
    /**
102
     * Creates a new test from the given test case.
103
     */
104
    public static function fromTestCase(TestCase $testCase, string $type, Throwable $throwable = null): self
105
    {
106
        $testCaseName = State::getPrintableTestCaseName($testCase);
107
 
108
        $description = self::makeDescription($testCase);
109
 
110
        $icon = self::makeIcon($type);
111
 
112
        $color = self::makeColor($type);
113
 
114
        return new self($testCaseName, $description, $type, $icon, $color, $throwable);
115
    }
116
 
117
    /**
118
     * Get the test case description.
119
     */
120
    public static function makeDescription(TestCase $testCase): string
121
    {
122
        $name = $testCase->getName(false);
123
 
124
        if ($testCase instanceof HasPrintableTestCaseName) {
125
            return $name;
126
        }
127
 
128
        // First, lets replace underscore by spaces.
129
        $name = str_replace('_', ' ', $name);
130
 
131
        // Then, replace upper cases by spaces.
132
        $name = (string) preg_replace('/([A-Z])/', ' $1', $name);
133
 
134
        // Finally, if it starts with `test`, we remove it.
135
        $name = (string) preg_replace('/^test/', '', $name);
136
 
137
        // Removes spaces
138
        $name = trim($name);
139
 
140
        // Lower case everything
141
        $name = mb_strtolower($name);
142
 
143
        // Add the dataset name if it has one
144
        if ($dataName = $testCase->dataName()) {
145
            if (is_int($dataName)) {
146
                $name .= sprintf(' with data set #%d', $dataName);
147
            } else {
148
                $name .= sprintf(' with data set "%s"', $dataName);
149
            }
150
        }
151
 
152
        return $name;
153
    }
154
 
155
    /**
156
     * Get the test case icon.
157
     */
158
    public static function makeIcon(string $type): string
159
    {
160
        switch ($type) {
161
            case self::FAIL:
162
                return '⨯';
163
            case self::SKIPPED:
164
                return '-';
165
            case self::RISKY:
166
                return '!';
167
            case self::INCOMPLETE:
168
                return '…';
169
            case self::WARN:
170
                return '!';
171
            case self::RUNS:
172
                return '•';
173
            default:
174
                return '✓';
175
        }
176
    }
177
 
178
    /**
179
     * Get the test case color.
180
     */
181
    public static function makeColor(string $type): string
182
    {
183
        switch ($type) {
184
            case self::FAIL:
185
                return 'red';
186
            case self::SKIPPED:
187
            case self::INCOMPLETE:
188
            case self::RISKY:
189
            case self::WARN:
190
            case self::RUNS:
191
                return 'yellow';
192
            default:
193
                return 'green';
194
        }
195
    }
196
}