Remove Phpunit/Dbunit

- Introduce own Yaml-to-SQL class
- Introduce new way of MySQL-DB-tests (per rollback)
- Remove dependency phpunit/dbunit
- Introduce new dev-dependency for YAML-ready (Symfony YAML reader)
This commit is contained in:
Philipp Holzer 2019-07-28 17:40:42 +02:00
parent eddcb5ebe9
commit b08ac3c0a7
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
9 changed files with 771 additions and 429 deletions

View File

@ -114,12 +114,12 @@
]
},
"require-dev": {
"phpunit/dbunit": "^2.0",
"phpdocumentor/reflection-docblock": "^3.0.2",
"phpunit/php-token-stream": "^1.4.2",
"mikey179/vfsstream": "^1.6",
"mockery/mockery": "^1.2",
"johnkary/phpunit-speedtrap": "1.1"
"johnkary/phpunit-speedtrap": "1.1",
"symfony/yaml": "^3.0"
},
"scripts": {
"test": "phpunit"

12
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b9ea7162aa7ede630a2090c883e1174b",
"content-hash": "ab69901def2561415a0de62fb86df9bf",
"packages": [
{
"name": "asika/simple-console",
@ -4267,16 +4267,16 @@
},
{
"name": "symfony/yaml",
"version": "v3.4.16",
"version": "v3.4.30",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "61973ecda60e9f3561e929e19c07d4878b960fc1"
"reference": "051d045c684148060ebfc9affb7e3f5e0899d40b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/61973ecda60e9f3561e929e19c07d4878b960fc1",
"reference": "61973ecda60e9f3561e929e19c07d4878b960fc1",
"url": "https://api.github.com/repos/symfony/yaml/zipball/051d045c684148060ebfc9affb7e3f5e0899d40b",
"reference": "051d045c684148060ebfc9affb7e3f5e0899d40b",
"shasum": ""
},
"require": {
@ -4322,7 +4322,7 @@
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
"time": "2018-09-24T08:15:45+00:00"
"time": "2019-07-24T13:01:31+00:00"
},
{
"name": "webmozart/assert",

View File

@ -6,41 +6,28 @@
namespace Friendica\Test;
use Friendica\Test\Util\Database\StaticDatabase;
use PHPUnit\DbUnit\DataSet\YamlDataSet;
use PHPUnit\DbUnit\TestCaseTrait;
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
/**
* Abstract class used by tests that need a database.
*/
abstract class DatabaseTest extends MockedTest
{
use TestCaseTrait;
/**
* Get database connection.
*
* This function is executed before each test in order to get a database connection that can be used by tests.
* If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
*
* If it could not connect to the database, the test is skipped.
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
* @see https://phpunit.de/manual/5.7/en/database.html
*/
protected function getConnection()
protected function setUp()
{
return $this->createDefaultDBConnection(StaticDatabase::getGlobConnection(), getenv('MYSQL_DATABASE'));
parent::setUp();
StaticDatabase::statConnect($_SERVER);
// Rollbacks every DB usage (in case the test couldn't call tearDown)
StaticDatabase::statRollback();
// Start the first, outer transaction
StaticDatabase::getGlobConnection()->beginTransaction();
}
/**
* Get dataset to populate the database with.
*
* @return YamlDataSet
* @see https://phtablepunit.de/manual/5.7/en/database.html
*/
protected function getDataSet()
protected function tearDown()
{
return new YamlDataSet(__DIR__ . '/datasets/api.yml');
// Rollbacks every DB usage so we don't commit anything into the DB
StaticDatabase::statRollback();
parent::tearDown();
}
}

View File

@ -9,6 +9,8 @@ use PDOException;
/**
* Overrides the Friendica database class for re-using the connection
* for different tests
*
* Overrides functionality to enforce one transaction per call (for nested transactions)
*/
class StaticDatabase extends Database
{
@ -29,41 +31,7 @@ class StaticDatabase extends Database
}
if (!isset(self::$staticConnection)) {
$port = 0;
$serveraddr = trim($this->configCache->get('database', 'hostname'));
$serverdata = explode(':', $serveraddr);
$server = $serverdata[0];
if (count($serverdata) > 1) {
$port = trim($serverdata[1]);
}
$server = trim($server);
$user = trim($this->configCache->get('database', 'username'));
$pass = trim($this->configCache->get('database', 'password'));
$db = trim($this->configCache->get('database', 'database'));
$charset = trim($this->configCache->get('database', 'charset'));
if (!(strlen($server) && strlen($user))) {
return false;
}
$connect = "mysql:host=" . $server . ";dbname=" . $db;
if ($port > 0) {
$connect .= ";port=" . $port;
}
if ($charset) {
$connect .= ";charset=" . $charset;
}
try {
self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
self::$staticConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
/// @TODO At least log exception, don't ignore it!
}
self::statConnect($_SERVER);
}
$this->driver = 'pdo';
@ -80,7 +48,7 @@ class StaticDatabase extends Database
*/
public function transaction()
{
if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
if (!$this->in_transaction && !$this->connection->beginTransaction()) {
return false;
}
@ -102,6 +70,64 @@ class StaticDatabase extends Database
return true;
}
/**
* Setup of the global, static connection
* Either through explicit calling or through implicit using the Database
*
* @param array $server $_SERVER variables
*/
public static function statConnect(array $server)
{
// Use environment variables for mysql if they are set beforehand
if (!empty($server['MYSQL_HOST'])
&& !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
&& $server['MYSQL_PASSWORD'] !== false
&& !empty($server['MYSQL_DATABASE']))
{
$db_host = $server['MYSQL_HOST'];
if (!empty($server['MYSQL_PORT'])) {
$db_host .= ':' . $server['MYSQL_PORT'];
}
if (!empty($server['MYSQL_USERNAME'])) {
$db_user = $server['MYSQL_USERNAME'];
} else {
$db_user = $server['MYSQL_USER'];
}
$db_pw = (string) $server['MYSQL_PASSWORD'];
$db_data = $server['MYSQL_DATABASE'];
}
$port = 0;
$serveraddr = trim($db_host);
$serverdata = explode(':', $serveraddr);
$server = $serverdata[0];
if (count($serverdata) > 1) {
$port = trim($serverdata[1]);
}
$server = trim($server);
$user = trim($db_user);
$pass = trim($db_pw);
$db = trim($db_data);
if (!(strlen($server) && strlen($user))) {
return;
}
$connect = "mysql:host=" . $server . ";dbname=" . $db;
if ($port > 0) {
$connect .= ";port=" . $port;
}
try {
self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
} catch (PDOException $e) {
/// @TODO At least log exception, don't ignore it!
}
}
/**
* @return ExtendedPDO The global, static connection
*/

View File

@ -0,0 +1,50 @@
<?php
namespace Friendica\Test\Util\Database;
use Friendica\Database\Database;
use Symfony\Component\Yaml\Yaml;
/**
* Util class to load YAML files into the database
*/
class YamlDataSet
{
/**
* @var array
*/
private $tables = [];
public function __construct(string $yamlFile)
{
$this->addYamlFile($yamlFile);
}
public function addYamlFile(string $yamlFile)
{
$data = Yaml::parse(file_get_contents($yamlFile));
foreach ($data as $tableName => $rows) {
if (!isset($rows)) {
$rows = [];
}
if (!is_array($rows)) {
continue;
}
foreach ($rows as $key => $value) {
$this->tables[$tableName][$key] = $value;
}
}
}
public function load(Database $database)
{
foreach ($this->tables as $tableName => $rows) {
foreach ($rows as $row) {
$database->insert($tableName, $row);
}
}
}
}

View File

@ -80,7 +80,7 @@ item:
visible: 1
contact-id: 42
author-id: 42
owner-id: 45
owner-id: 42
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 1
@ -99,7 +99,7 @@ item:
visible: 1
contact-id: 42
author-id: 42
owner-id: 45
owner-id: 42
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 0

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,10 @@ namespace Friendica\Test\src\Database;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Core\Config;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\Database\StaticDatabase;
class DBATest extends DatabaseTest
{
@ -15,6 +17,7 @@ class DBATest extends DatabaseTest
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
$dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
BaseObject::setDependencyInjection($dice);
// Default config

View File

@ -4,20 +4,20 @@ namespace Friendica\Test\src\Database;
use Dice\Dice;
use Friendica\BaseObject;
use Friendica\Database\Database;
use Friendica\Database\DBStructure;
use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\Database\StaticDatabase;
class DBStructureTest extends DatabaseTest
{
/**
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function setUp()
protected function setUp()
{
parent::setUp();
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
$dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
BaseObject::setDependencyInjection($dice);
}