Completed
Push — master ( 501416...580ac7 )
by David
01:23
created

MySQL::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

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.

Loading history...
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

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.

Loading history...
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