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": { "require-dev": {
"phpunit/dbunit": "^2.0",
"phpdocumentor/reflection-docblock": "^3.0.2", "phpdocumentor/reflection-docblock": "^3.0.2",
"phpunit/php-token-stream": "^1.4.2", "phpunit/php-token-stream": "^1.4.2",
"mikey179/vfsstream": "^1.6", "mikey179/vfsstream": "^1.6",
"mockery/mockery": "^1.2", "mockery/mockery": "^1.2",
"johnkary/phpunit-speedtrap": "1.1" "johnkary/phpunit-speedtrap": "1.1",
"symfony/yaml": "^3.0"
}, },
"scripts": { "scripts": {
"test": "phpunit" "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", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "b9ea7162aa7ede630a2090c883e1174b", "content-hash": "ab69901def2561415a0de62fb86df9bf",
"packages": [ "packages": [
{ {
"name": "asika/simple-console", "name": "asika/simple-console",
@ -4267,16 +4267,16 @@
}, },
{ {
"name": "symfony/yaml", "name": "symfony/yaml",
"version": "v3.4.16", "version": "v3.4.30",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/yaml.git", "url": "https://github.com/symfony/yaml.git",
"reference": "61973ecda60e9f3561e929e19c07d4878b960fc1" "reference": "051d045c684148060ebfc9affb7e3f5e0899d40b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/61973ecda60e9f3561e929e19c07d4878b960fc1", "url": "https://api.github.com/repos/symfony/yaml/zipball/051d045c684148060ebfc9affb7e3f5e0899d40b",
"reference": "61973ecda60e9f3561e929e19c07d4878b960fc1", "reference": "051d045c684148060ebfc9affb7e3f5e0899d40b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -4322,7 +4322,7 @@
], ],
"description": "Symfony Yaml Component", "description": "Symfony Yaml Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"time": "2018-09-24T08:15:45+00:00" "time": "2019-07-24T13:01:31+00:00"
}, },
{ {
"name": "webmozart/assert", "name": "webmozart/assert",

View file

@ -6,41 +6,28 @@
namespace Friendica\Test; namespace Friendica\Test;
use Friendica\Test\Util\Database\StaticDatabase; 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 used by tests that need a database.
*/ */
abstract class DatabaseTest extends MockedTest abstract class DatabaseTest extends MockedTest
{ {
use TestCaseTrait; protected function setUp()
/**
* 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()
{ {
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();
} }
/** protected function tearDown()
* Get dataset to populate the database with.
*
* @return YamlDataSet
* @see https://phtablepunit.de/manual/5.7/en/database.html
*/
protected function getDataSet()
{ {
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 * Overrides the Friendica database class for re-using the connection
* for different tests * for different tests
*
* Overrides functionality to enforce one transaction per call (for nested transactions)
*/ */
class StaticDatabase extends Database class StaticDatabase extends Database
{ {
@ -29,41 +31,7 @@ class StaticDatabase extends Database
} }
if (!isset(self::$staticConnection)) { if (!isset(self::$staticConnection)) {
self::statConnect($_SERVER);
$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!
}
} }
$this->driver = 'pdo'; $this->driver = 'pdo';
@ -80,7 +48,7 @@ class StaticDatabase extends Database
*/ */
public function transaction() public function transaction()
{ {
if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) { if (!$this->in_transaction && !$this->connection->beginTransaction()) {
return false; return false;
} }
@ -102,6 +70,64 @@ class StaticDatabase extends Database
return true; 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 * @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 visible: 1
contact-id: 42 contact-id: 42
author-id: 42 author-id: 42
owner-id: 45 owner-id: 42
uid: 42 uid: 42
verb: http://activitystrea.ms/schema/1.0/post verb: http://activitystrea.ms/schema/1.0/post
unseen: 1 unseen: 1
@ -99,7 +99,7 @@ item:
visible: 1 visible: 1
contact-id: 42 contact-id: 42
author-id: 42 author-id: 42
owner-id: 45 owner-id: 42
uid: 42 uid: 42
verb: http://activitystrea.ms/schema/1.0/post verb: http://activitystrea.ms/schema/1.0/post
unseen: 0 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 Dice\Dice;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Test\DatabaseTest; use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\Database\StaticDatabase;
class DBATest extends DatabaseTest class DBATest extends DatabaseTest
{ {
@ -15,6 +17,7 @@ class DBATest extends DatabaseTest
$dice = new Dice(); $dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php'); $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
$dice = $dice->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]);
BaseObject::setDependencyInjection($dice); BaseObject::setDependencyInjection($dice);
// Default config // Default config

View file

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