1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lloople\PHPUnitExtensions\Runners\SlowestTests; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PDO; |
7
|
|
|
|
8
|
|
|
class MySQL extends Channel |
9
|
|
|
{ |
10
|
|
|
protected $connection = null; |
11
|
|
|
|
12
|
|
|
protected $credentials = [ |
13
|
|
|
'database' => 'phpunit_results', |
14
|
|
|
'table' => 'default', |
15
|
|
|
'username' => 'root', |
16
|
|
|
'password' => '', |
17
|
|
|
'host' => '127.0.0.1' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
public function __construct(int $rows = 5, array $credentials = []) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($rows); |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
$this->credentials = array_merge($this->credentials, $credentials); |
26
|
|
|
|
27
|
|
|
$this->connect(); |
28
|
|
|
|
29
|
|
|
$this->createTableIfNotExists(); |
30
|
|
|
} catch (Exception $e) { |
31
|
|
|
echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function connect(): void |
36
|
|
|
{ |
37
|
|
|
$this->connection = new PDO( |
38
|
|
|
"mysql:dbname={$this->credentials['database']};host={$this->credentials['host']}", |
39
|
|
|
$this->credentials['username'], |
40
|
|
|
$this->credentials['password'], |
41
|
|
|
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function createTableIfNotExists(): void |
46
|
|
|
{ |
47
|
|
|
$this->connection->prepare( |
48
|
|
|
"CREATE TABLE IF NOT EXISTS {$this->credentials['table']} ( |
49
|
|
|
`time` float DEFAULT NULL, |
50
|
|
|
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, |
51
|
|
|
`method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, |
52
|
|
|
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, |
53
|
|
|
PRIMARY KEY (`name`) |
54
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" |
55
|
|
|
)->execute(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function printResults(): void |
59
|
|
|
{ |
60
|
|
|
if ($this->connection === null) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($this->testsToPrint() as $test => $time) { |
65
|
|
|
try { |
66
|
|
|
$this->insert($test, $time); |
67
|
|
|
} catch (Exception $e) { |
68
|
|
|
echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL; |
69
|
|
|
|
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function insert(string $test, string $time): void |
76
|
|
|
{ |
77
|
|
|
[$class, $method] = explode('::', $test); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->connection |
80
|
|
|
->prepare( |
81
|
|
|
"INSERT INTO `{$this->credentials['table']}` (time, method, class, name) |
82
|
|
|
VALUES(:time, :method, :class, :name) |
83
|
|
|
ON DUPLICATE KEY UPDATE time = :time;" |
84
|
|
|
) |
85
|
|
|
->execute([ |
86
|
|
|
'time' => $time, |
87
|
|
|
'method' => $method, |
88
|
|
|
'class' => $class, |
89
|
|
|
'name' => $test, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.