Channel::testsToPrint()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Lloople\PHPUnitExtensions\Runners\SlowestTests;
4
5
use PHPUnit\Runner\AfterLastTestHook;
6
use PHPUnit\Runner\AfterTestHook;
7
8
abstract class Channel implements AfterTestHook, AfterLastTestHook
9
{
10
    /**
11
     * Tests marked as slow.
12
     *
13
     * @var array
14
     */
15
    protected $tests = [];
16
17
    /**
18
     * The max number of rows to be reported.
19
     *
20
     * @var int|null
21
     */
22
    protected $rows;
23
24
    /**
25
     * The minimum amount of miliseconds to consider a test slow.
26
     *
27
     * @var int|null
28
     */
29
    protected $min;
30
31
    public function __construct(?int $rows = null, ?int $min = 200)
32
    {
33
        $this->rows = $rows;
34
        $this->min = $min;
35
    }
36
37
    public function executeAfterTest(string $test, float $time): void
38
    {
39
        $time = $this->timeToMiliseconds($time);
40
41
        if ($time <= $this->min) {
42
            return;
43
        }
44
        
45
        $this->tests[$test] = $time;
46
    }
47
48
    protected function timeToMiliseconds(float $time): int
49
    {
50
        return (int)($time * 1000);
51
    }
52
53
    public function executeAfterLastTest(): void
54
    {
55
        $this->sortTestsBySpeed();
56
57
        $this->printResults();
58
    }
59
60
    protected function sortTestsBySpeed(): void
61
    {
62
        arsort($this->tests);
63
    }
64
65
    abstract protected function printResults(): void;
66
67
    protected function testsToPrint(): array
68
    {
69
        if ($this->rows === null) {
70
            return $this->tests;
71
        }
72
73
        return array_slice($this->tests, 0, $this->rows, true);
74
    }
75
76
    protected function getClassName(): string
77
    {
78
        return get_class($this);
79
    }
80
}