Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed refactoring changes #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"require": {
"php": "^7.1.0",
"phpunit/phpunit": "^7.0||^8.0",
"symfony/yaml": "^4.2",
"mongodb/mongodb": "^1.5"
Expand Down
93 changes: 93 additions & 0 deletions src/Adapter/Mongo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit\DbFixtures\Adapter;

use IW\PHPUnit\DbFixtures\Adapter\Mongo\Json;
use IW\PHPUnit\DbFixtures\Cache;
use IW\PHPUnit\DbFixtures\FileCache;

class Mongo
{
/** @var Cache */
private $cache;

/** @var FileCache */
private $fileCache;

public function __construct(Cache $cache, FileCache $fileCache) {
$this->cache = $cache;
$this->fileCache = $fileCache;
}

public function loadFixtures($connection, string ...$filenames) : void {
$this->removeAllDocuments($connection);

foreach ($filenames as $filename) {
$testData = $this->parseJsonp($filename);
if (strpos($filename, '.meta') !== false) {
$this->createIndexes($connection, $testData);
} else {
$this->insertDocuments($connection, $testData);
}
}
}

private function removeAllDocuments($connection) : void {
// remove all documents in it
foreach ($connection->listCollections() as $collection) {
// ignore system collection
if (0 !== mb_strpos($collection->getName(), 'system.')) {
$connection->dropCollection($collection->getName());
}
}
}

private function createIndexes($connection, $testData) : void {
foreach ($testData as $collectionName => $config) {
if (isset($config['indexes'])) {
foreach ($config['indexes'] as $index) {
$keys = $index['key'];
unset($index['key']);
$connection->selectCollection($collectionName)->createIndex($keys, $index);
}
}
}
}

private function insertDocuments($connection, $testData) : void {
foreach ($testData as $collectionName => $documents) {
foreach ($documents as $document) {
$connection->selectCollection($collectionName)->insertOne($document);
}
}
}

private function parseJsonp(string $filename) {
if ($this->cache->has($filename) === false) {
$testData = $this->fileCache->get(
$filename,
static function ($filename) {
if (!is_array($testData = Json::decode(
file_get_contents($filename), true))
) {
throw new \InvalidArgumentException(
sprintf(
'Illegal fixtures %s' . PHP_EOL . 'json_decode: %s',
$filename,
json_last_error_msg()
)
);
}

//transform data into JSONP format which can handle advanced types (eg. MongoId, MongoDate, etc.)
Json::jsonToJsonp($testData);
return $testData;
}
);

$this->cache->set($filename, $testData);
}

return $this->cache->get($filename);
}
}
2 changes: 1 addition & 1 deletion src/Json.php → src/Adapter/Mongo/Json.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit\DbFixtures;
namespace IW\PHPUnit\DbFixtures\Adapter\Mongo;

use DateTime;
use MongoDB\BSON\ObjectId;
Expand Down
155 changes: 155 additions & 0 deletions src/Adapter/Pdo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace IW\PHPUnit\DbFixtures\Adapter;

use IW\PHPUnit\DbFixtures\Adapter\Pdo\PdoDriver;
use IW\PHPUnit\DbFixtures\Cache;
use IW\PHPUnit\DbFixtures\FileCache;
use IW\PHPUnit\DbFixtures\NormalizationStrategy;
use IW\PHPUnit\DbFixtures\Adapter\Pdo\DriverFactory;
use Symfony\Component\Yaml\Yaml;

class Pdo
{
/** @var Cache */
private $cache;

/** @var NormalizationStrategy */
private $normalizationStrategy;

/** @var DriverFactory */
private $driverFactory;

/** @var FileCache */
private $fileCache;

public function __construct(
Cache $cache,
NormalizationStrategy $normalizationStrategy,
DriverFactory $driverFactory,
FileCache $fileCache
) {
$this->cache = $cache;
$this->normalizationStrategy = $normalizationStrategy;
$this->driverFactory = $driverFactory;
$this->fileCache = $fileCache;
}

public function loadFixtures($connection, string ...$filenames) : void {
$data = [];
$pdoAdapter = $this->driverFactory->create($connection);
$bdsFilename = null;

// First file acts as a basic data set
if (count($filenames) > 1) {
$bdsFilename = array_shift($filenames);
}

foreach ($filenames as $filename) {
$data = array_merge_recursive($data, $this->loadFile($filename));
}

if ($bdsFilename !== null) {
$data = $this->normalizationStrategy->normalizeFixtures($this->loadFile($bdsFilename), $data);
}

$sqls = [$pdoAdapter->disableForeignKeys()];

$pdoAdapter->cleanTables($sqls);

foreach ($data as $table => $rows) {
$this->buildSql($connection, $table, $rows, $sqls);
}

$sqls[] = $pdoAdapter->enableForeignKeys();

$this->executeSqls($connection, $sqls);
}

private function executeSqls(\PDO $pdo, array $sqls): void {
if (!$pdo->beginTransaction()) {
$this->throwPDOException($pdo, 'BEGIN TRANSACTION');
}

try {
foreach ($sqls as $sql) {
if ($pdo->exec($sql) === false) {
$this->throwPDOException($pdo, $sql);
}
}
} catch (\Throwable $exception) {
$pdo->rollback();
throw $exception;
}

if (!$pdo->commit()) {
$this->throwPDOException($pdo, 'COMMIT');
}
}

private function throwPDOException($pdo, $sql): void {
[, $code, $message] = $pdo->errorInfo();
throw new \PDOException($message . PHP_EOL . $sql, $code);
}

private function loadFile(string $filename): array {
switch ($extension = \pathinfo($filename, \PATHINFO_EXTENSION)) {
case 'yaml':
case 'yml':
if ($this->cache->has($filename) === false) {
$yaml = $this->fileCache->get(
$filename,
static function ($filename) {
return Yaml::parse(file_get_contents($filename));
}
);
$this->cache->set($filename, $yaml);
}

return $this->cache->get($filename);
default:
throw new \InvalidArgumentException('Unsupported extension "' . $extension . '"');
}
}

private function buildSql(\PDO $pdo, string $table, array $rows, array &$sqls): void {
$adapter = $this->driverFactory->create($pdo);
$columns = [];
foreach ($rows as $row) {
$columns = array_merge($columns, array_keys($row));
}

$columns = array_unique($columns);

$values = [];
foreach ($rows as $row) {
$vals = [];
foreach ($columns as $column) {
if (array_key_exists($column, $row) && $row[$column] !== null) {
$val = $row[$column];
// pack binary string
if (is_string($val) && preg_match('/[^\x20-\x7E\t\r\n]/', $val)) {
$vals[] = $adapter->quoteBinary($val);
} else {
$vals[] = $pdo->quote($val);
}
} else {
$vals[] = 'NULL';
}
}

$values[] = '(' . implode(',', $vals) . ')';
}

foreach ($columns as &$column) {
$column = '`'.$column.'`';
}

$sqls[] = \sprintf(
'INSERT INTO `%s` (%s) VALUES %s;',
$table,
implode(',', $columns),
implode(',', $values)
);
}
}
20 changes: 20 additions & 0 deletions src/Adapter/Pdo/DefaultNormalizationStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit\DbFixtures\Adapter\Pdo;

use IW\PHPUnit\DbFixtures\NormalizationStrategy;

class DefaultNormalizationStrategy implements NormalizationStrategy
{
/**
* Normalize fixtures, override for implementing own normalize strategy
*
* @param array $bdsData Basic data set
* @param array $testData Other fixtures
*
* @return array
*/
public function normalizeFixtures(array $bdsData, array $testData) : array {
return array_merge_recursive($bdsData, $testData);
}
}
55 changes: 55 additions & 0 deletions src/Adapter/Pdo/Driver/MySQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit\DbFixtures\Adapter\Pdo\Driver;

use IW\PHPUnit\DbFixtures\Adapter\Pdo\PdoDriver;

class MySQL implements PdoDriver
{
private $currentDatabase;

/** @var \PDO */
private $pdo;

public function __construct(\PDO $pdo) {
$this->pdo = $pdo;
}

public function cleanTables( &$sqls): void {
$query = 'SELECT TABLE_NAME,TABLE_ROWS,AUTO_INCREMENT FROM `information_schema`.`tables` WHERE table_schema=?';

$stmt = $this->pdo->prepare($query);
$stmt->execute([$this->getCurrentDatabase()]);

while ($row = $stmt->fetch()) {
if ($this->isTableEmpty($row)) {
$sqls[] = \sprintf('TRUNCATE TABLE `%s`;', $row['TABLE_NAME']);
}
}
}

public function disableForeignKeys(): string {
return 'SET foreign_key_checks = 0;';
}

public function enableForeignKeys(): string {
return 'SET foreign_key_checks = 1;';
}

public function quoteBinary(string $value): string {
return sprintf("UNHEX('%s')", bin2hex($value));
}

private function getCurrentDatabase() : string {
if ($this->currentDatabase === null) {
$databaseName = $this->pdo->query('select database()')->fetchColumn();
$this->currentDatabase = $databaseName;
}

return $this->currentDatabase;
}

private function isTableEmpty(array $row) : bool {
return ($row['AUTO_INCREMENT'] > 1 || $row['TABLE_ROWS'] != 0);
}
}
41 changes: 41 additions & 0 deletions src/Adapter/Pdo/Driver/SQLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit\DbFixtures\Adapter\Pdo\Driver;

use IW\PHPUnit\DbFixtures\Adapter\Pdo\PdoDriver;

class SQLite implements PdoDriver
{
/** @var \PDO */
private $pdo;

public function __construct(\PDO $pdo) {
$this->pdo = $pdo;
}

public function cleanTables(&$sqls) : void {
$tables = $this->pdo->query(
"SELECT name FROM sqlite_master WHERE type='table' AND tbl_name<>'sqlite_sequence'"
)->fetchAll();

foreach ($tables as ['name' => $tableName]) {
$sqls[] = \sprintf(
'DELETE FROM `%s`;UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = "%s";',
$tableName,
$tableName
);
}
}

public function disableForeignKeys() : string {
return 'PRAGMA foreign_keys = OFF;';
}

public function enableForeignKeys() : string {
return 'PRAGMA foreign_keys = ON;';
}

public function quoteBinary(string $value) : string {
return sprintf("X'%s'", bin2hex($value));
}
}
Loading