|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lloople\PHPUnitExtensions\Runners\SlowestTests; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use PDO; |
|
7
|
|
|
|
|
8
|
|
|
class MySQL extends Channel |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The connection used to store the results. |
|
12
|
|
|
* |
|
13
|
|
|
* @var PDO |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $connection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The credentials needed to connect to the database. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $credentials = [ |
|
23
|
|
|
'database' => 'phpunit_results', |
|
24
|
|
|
'table' => 'default', |
|
25
|
|
|
'username' => 'root', |
|
26
|
|
|
'password' => '', |
|
27
|
|
|
'host' => '127.0.0.1' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( array $credentials = [], ?int $rows = null, ?int $min = 200) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($rows, $min); |
|
33
|
|
|
|
|
34
|
|
|
try { |
|
35
|
|
|
$this->credentials = array_merge($this->credentials, $credentials); |
|
36
|
|
|
|
|
37
|
|
|
$this->connect(); |
|
38
|
|
|
|
|
39
|
|
|
$this->createTableIfNotExists(); |
|
40
|
|
|
} catch (Exception $e) { |
|
41
|
|
|
echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function connect(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->connection = new PDO( |
|
48
|
|
|
"mysql:dbname={$this->credentials['database']};host={$this->credentials['host']}", |
|
49
|
|
|
$this->credentials['username'], |
|
50
|
|
|
$this->credentials['password'], |
|
51
|
|
|
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function createTableIfNotExists(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->connection->prepare( |
|
58
|
|
|
"CREATE TABLE IF NOT EXISTS `{$this->credentials['table']}` ( |
|
59
|
|
|
`time` float DEFAULT NULL, |
|
60
|
|
|
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, |
|
61
|
|
|
`method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, |
|
62
|
|
|
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, |
|
63
|
|
|
PRIMARY KEY (`name`) |
|
64
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" |
|
65
|
|
|
)->execute(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function printResults(): void |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
foreach ($this->testsToPrint() as $test => $time) { |
|
72
|
|
|
$this->insert($test, $time); |
|
73
|
|
|
} |
|
74
|
|
|
} catch (Exception $e) { |
|
75
|
|
|
echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function insert(string $test, string $time): void |
|
80
|
|
|
{ |
|
81
|
|
|
[$class, $method] = explode('::', $test); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$this->connection |
|
84
|
|
|
->prepare( |
|
85
|
|
|
"INSERT INTO `{$this->credentials['table']}` (time, method, class, name) |
|
86
|
|
|
VALUES(:time, :method, :class, :name) |
|
87
|
|
|
ON DUPLICATE KEY UPDATE time = :time;" |
|
88
|
|
|
) |
|
89
|
|
|
->execute([ |
|
90
|
|
|
'time' => $time, |
|
91
|
|
|
'method' => $method, |
|
92
|
|
|
'class' => $class, |
|
93
|
|
|
'name' => $test, |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.