Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Runner;
11
 
12
use const DIRECTORY_SEPARATOR;
13
use const LOCK_EX;
14
use function assert;
15
use function dirname;
16
use function file_get_contents;
17
use function file_put_contents;
18
use function in_array;
19
use function is_array;
20
use function is_dir;
21
use function is_file;
22
use function json_decode;
23
use function json_encode;
24
use function sprintf;
25
use PHPUnit\Util\Filesystem;
26
 
27
/**
28
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
29
 */
30
final class DefaultTestResultCache implements TestResultCache
31
{
32
    /**
33
     * @var int
34
     */
35
    private const VERSION = 1;
36
 
37
    /**
38
     * @psalm-var list<int>
39
     */
40
    private const ALLOWED_TEST_STATUSES = [
41
        BaseTestRunner::STATUS_SKIPPED,
42
        BaseTestRunner::STATUS_INCOMPLETE,
43
        BaseTestRunner::STATUS_FAILURE,
44
        BaseTestRunner::STATUS_ERROR,
45
        BaseTestRunner::STATUS_RISKY,
46
        BaseTestRunner::STATUS_WARNING,
47
    ];
48
 
49
    /**
50
     * @var string
51
     */
52
    private const DEFAULT_RESULT_CACHE_FILENAME = '.phpunit.result.cache';
53
 
54
    /**
55
     * @var string
56
     */
57
    private $cacheFilename;
58
 
59
    /**
60
     * @psalm-var array<string, int>
61
     */
62
    private $defects = [];
63
 
64
    /**
65
     * @psalm-var array<string, float>
66
     */
67
    private $times = [];
68
 
69
    public function __construct(?string $filepath = null)
70
    {
71
        if ($filepath !== null && is_dir($filepath)) {
72
            $filepath .= DIRECTORY_SEPARATOR . self::DEFAULT_RESULT_CACHE_FILENAME;
73
        }
74
 
75
        $this->cacheFilename = $filepath ?? $_ENV['PHPUNIT_RESULT_CACHE'] ?? self::DEFAULT_RESULT_CACHE_FILENAME;
76
    }
77
 
78
    public function setState(string $testName, int $state): void
79
    {
80
        if (!in_array($state, self::ALLOWED_TEST_STATUSES, true)) {
81
            return;
82
        }
83
 
84
        $this->defects[$testName] = $state;
85
    }
86
 
87
    public function getState(string $testName): int
88
    {
89
        return $this->defects[$testName] ?? BaseTestRunner::STATUS_UNKNOWN;
90
    }
91
 
92
    public function setTime(string $testName, float $time): void
93
    {
94
        $this->times[$testName] = $time;
95
    }
96
 
97
    public function getTime(string $testName): float
98
    {
99
        return $this->times[$testName] ?? 0.0;
100
    }
101
 
102
    public function load(): void
103
    {
104
        if (!is_file($this->cacheFilename)) {
105
            return;
106
        }
107
 
108
        $data = json_decode(
109
            file_get_contents($this->cacheFilename),
110
            true
111
        );
112
 
113
        if ($data === null) {
114
            return;
115
        }
116
 
117
        if (!isset($data['version'])) {
118
            return;
119
        }
120
 
121
        if ($data['version'] !== self::VERSION) {
122
            return;
123
        }
124
 
125
        assert(isset($data['defects']) && is_array($data['defects']));
126
        assert(isset($data['times']) && is_array($data['times']));
127
 
128
        $this->defects = $data['defects'];
129
        $this->times   = $data['times'];
130
    }
131
 
132
    /**
133
     * @throws Exception
134
     */
135
    public function persist(): void
136
    {
137
        if (!Filesystem::createDirectory(dirname($this->cacheFilename))) {
138
            throw new Exception(
139
                sprintf(
140
                    'Cannot create directory "%s" for result cache file',
141
                    $this->cacheFilename
142
                )
143
            );
144
        }
145
 
146
        file_put_contents(
147
            $this->cacheFilename,
148
            json_encode(
149
                [
150
                    'version' => self::VERSION,
151
                    'defects' => $this->defects,
152
                    'times'   => $this->times,
153
                ]
154
            ),
155
            LOCK_EX
156
        );
157
    }
158
}