Skip to content

Commit

Permalink
Reorganize code, database ones perform inserts at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
Lloople committed Jan 4, 2020
1 parent 4fcd9c3 commit 580ac7c
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 223 deletions.
47 changes: 0 additions & 47 deletions src/Runners/Console.php

This file was deleted.

95 changes: 0 additions & 95 deletions src/Runners/SQLite.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/Runners/SlowestTests/Channel.php
@@ -0,0 +1,52 @@
<?php

namespace Lloople\PHPUnitExtensions\Runners\SlowestTests;

use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\AfterTestHook;

abstract class Channel implements AfterTestHook, AfterLastTestHook
{
protected $tests = [];

protected $rows;

public function __construct(int $rows = 5)
{
$this->rows = $rows;
}

public function executeAfterTest(string $test, float $time): void
{
$this->tests[$test] = $this->timeToMiliseconds($time);
}

protected function timeToMiliseconds(float $time)
{
return (int)($time * 1000);
}

public function executeAfterLastTest(): void
{
$this->sortTestsBySpeed();

$this->printResults();
}

protected function sortTestsBySpeed(): void
{
arsort($this->tests);
}

abstract protected function printResults(): void;

protected function testsToPrint(): array
{
return array_slice($this->tests, 0, $this->rows, true);
}

protected function getClassName(): string
{
return get_class($this);
}
}
17 changes: 17 additions & 0 deletions src/Runners/SlowestTests/Console.php
@@ -0,0 +1,17 @@
<?php

namespace Lloople\PHPUnitExtensions\Runners\SlowestTests;

class Console extends Channel
{
protected function printResults(): void
{
echo PHP_EOL . "Showing the top {$this->rows} slowest tests:" . PHP_EOL;

foreach ($this->testsToPrint() as $test => $time) {
echo str_pad($time, 5, ' ', STR_PAD_LEFT) . " ms: {$test}" . PHP_EOL;
}

echo PHP_EOL;
}
}
7 changes: 2 additions & 5 deletions src/Runners/Csv.php → src/Runners/SlowestTests/Csv.php
@@ -1,12 +1,9 @@
<?php

namespace Lloople\PHPUnitExtensions\Runners;
namespace Lloople\PHPUnitExtensions\Runners\SlowestTests;

use Lloople\PHPUnitExtensions\Time;

class Csv extends Console
class Csv extends Channel
{

protected $file;

public function __construct(int $rows = 5, string $file = 'phpunit_results.csv')
Expand Down
98 changes: 40 additions & 58 deletions src/Runners/MySQL.php → src/Runners/SlowestTests/MySQL.php
@@ -1,13 +1,11 @@
<?php

namespace Lloople\PHPUnitExtensions\Runners;
namespace Lloople\PHPUnitExtensions\Runners\SlowestTests;

use Exception;
use Lloople\PHPUnitExtensions\Time;
use PDO;
use PHPUnit\Runner\AfterTestHook;

class MySQL implements AfterTestHook
class MySQL extends Channel
{
protected $connection = null;

Expand All @@ -19,37 +17,62 @@ class MySQL implements AfterTestHook
'host' => '127.0.0.1'
];

public function __construct(array $credentials = [])
public function __construct(int $rows = 5, array $credentials = [])
{
parent::__construct($rows);

try {
$this->credentials = array_merge($this->credentials, $credentials);

$this->connect();

if (! $this->tableExists()) {
$this->createTable();
}
$this->createTableIfNotExists();
} catch (Exception $e) {
echo "MySQL Runner skipped: {$e->getMessage()}" . PHP_EOL;
echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL;
}
}

public function executeAfterTest(string $test, float $time): void
protected function connect(): void
{
$this->connection = new PDO(
"mysql:dbname={$this->credentials['database']};host={$this->credentials['host']}",
$this->credentials['username'],
$this->credentials['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
}

protected function createTableIfNotExists(): void
{
$this->connection->prepare(
"CREATE TABLE IF NOT EXISTS {$this->credentials['table']} (
`time` float DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
)->execute();
}

protected function printResults(): void
{
if ($this->connection === null) {
return;
}

try {
$this->insert($test, $time);
} catch (Exception $e) {
echo "MySQL Runner skipped: {$e->getMessage()}" . PHP_EOL;
foreach ($this->testsToPrint() as $test => $time) {
try {
$this->insert($test, $time);
} catch (Exception $e) {
echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL;

$this->connection = null;
break;
}
}
}

protected function insert(string $test, float $time): void
protected function insert(string $test, string $time): void
{
[$class, $method] = explode('::', $test);

Expand All @@ -60,51 +83,10 @@ protected function insert(string $test, float $time): void
ON DUPLICATE KEY UPDATE time = :time;"
)
->execute([
'time' => new Time($time),
'time' => $time,
'method' => $method,
'class' => $class,
'name' => $test,
]);
}

protected function connect(): void
{
$this->connection = new PDO(
"mysql:dbname={$this->credentials['database']};host={$this->credentials['host']}",
$this->credentials['username'],
$this->credentials['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
}

protected function tableExists(): bool
{
$query = $this->connection->prepare(
"SELECT TABLE_NAME
FROM information_schema.tables
WHERE table_schema = :database
AND table_name = :table
LIMIT 1;"
);

$query->execute([
'database' => $this->credentials['database'],
'table' => $this->credentials['table']
]);

return $query->fetch() !== false;
}

protected function createTable(): void
{
$this->connection->prepare(
"CREATE TABLE {$this->credentials['table']} (
`time` float DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
)->execute();
}
}

0 comments on commit 580ac7c

Please sign in to comment.