Merge pull request #7441 from nupplaphil/task/remove_dbunit

Remove Phpunit/Dbunit
This commit is contained in:
Hypolite Petovan 2019-07-28 16:21:22 -04:00 committed by GitHub
commit b1049d1d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 987 additions and 648 deletions

View File

@ -114,7 +114,6 @@
] ]
}, },
"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",

24
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": "9f101b2b4a651f425e155d0029223774",
"packages": [ "packages": [
{ {
"name": "asika/simple-console", "name": "asika/simple-console",
@ -4209,16 +4209,16 @@
}, },
{ {
"name": "symfony/polyfill-ctype", "name": "symfony/polyfill-ctype",
"version": "v1.9.0", "version": "v1.11.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git", "url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "e3d826245268269cd66f8326bd8bc066687b4a19" "reference": "82ebae02209c21113908c229e9883c419720738a"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/82ebae02209c21113908c229e9883c419720738a",
"reference": "e3d826245268269cd66f8326bd8bc066687b4a19", "reference": "82ebae02209c21113908c229e9883c419720738a",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -4230,7 +4230,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.9-dev" "dev-master": "1.11-dev"
} }
}, },
"autoload": { "autoload": {
@ -4263,20 +4263,20 @@
"polyfill", "polyfill",
"portable" "portable"
], ],
"time": "2018-08-06T14:22:27+00:00" "time": "2019-02-06T07:57:58+00:00"
}, },
{ {
"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

@ -5,42 +5,56 @@
namespace Friendica\Test; namespace Friendica\Test;
use Friendica\Database\Database;
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()
{
// Rollbacks every DB usage so we don't commit anything into the DB
StaticDatabase::statRollback();
parent::tearDown();
} }
/** /**
* Get dataset to populate the database with. * Loads a given DB fixture for this DB test
* *
* @return YamlDataSet * @param string $fixture The path to the fixture
* @see https://phtablepunit.de/manual/5.7/en/database.html * @param Database $dba The DB connection
*
* @throws \Exception
*/ */
protected function getDataSet() protected function loadFixture(string $fixture, Database $dba)
{ {
return new YamlDataSet(__DIR__ . '/datasets/api.yml'); $this->assertFileExists($fixture);
$data = include $fixture;
foreach ($data as $tableName => $rows) {
if (!is_array($rows)) {
$dba->p('TRUNCATE TABLE `' . $tableName . '``');
continue;
}
foreach ($rows as $row) {
$dba->insert($tableName, $row);
}
}
} }
} }

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,242 @@
<?php
return [
// Empty these tables
'cache',
'conversation',
'pconfig',
'photo',
'workerqueue',
'mail',
'item-delivery-data',
// Base test config to avoid notice messages
'config' => [
[
'cat' => 'system',
'k' => 'url',
'v' => 'http://localhost',
],
[
'cat' => 'config',
'k' => 'hostname',
'v' => 'localhost',
],
[
'cat' => 'system',
'k' => 'worker_dont_fork',
'v' => '1',
],
],
'user' => [
[
'uid' => 42,
'username' => 'Test user',
'nickname' => 'selfcontact',
'verified' => 1,
'password' => '$2y$10$DLRNTRmJgKe1cSrFJ5Jb0edCqvXlA9sh/RHdSnfxjbR.04yZRm4Qm',
'theme' => 'frio',
],
],
'contact' => [
[
'id' => 42,
'uid' => 42,
'name' => 'Self contact',
'nick' => 'selfcontact',
'self' => 1,
'nurl' => 'http://localhost/profile/selfcontact',
'url' => 'http://localhost/profile/selfcontact',
'about' => 'User used in tests',
'pending' => 0,
'blocked' => 0,
'rel' => 1,
'network' => 'dfrn',
],
// Having the same name and nick allows us to test
// the fallback to api_get_nick() in api_get_user()
[
'id' => 43,
'uid' => 0,
'name' => 'othercontact',
'nick' => 'othercontact',
'self' => 0,
'nurl' => 'http://localhost/profile/othercontact',
'url' => 'http://localhost/profile/othercontact',
'pending' => 0,
'blocked' => 0,
'rel' => 0,
'network' => 'dfrn',
],
[
'id' => 44,
'uid' => 42,
'name' => 'Friend contact',
'nick' => 'friendcontact',
'self' => 0,
'nurl' => 'http://localhost/profile/friendcontact',
'url' => 'http://localhost/profile/friendcontact',
'pending' => 0,
'blocked' => 0,
'rel' => 2,
'network' => 'dfrn',
],
],
'item' => [
[
'id' => 1,
'visible' => 1,
'contact-id' => 42,
'author-id' => 42,
'owner-id' => 42,
'uid' => 42,
'verb' => 'http://activitystrea.ms/schema/1.0/post',
'unseen' => 1,
'body' => 'Parent status',
'parent' => 1,
'author-link' => 'http://localhost/profile/selfcontact',
'wall' => 1,
'starred' => 1,
'origin' => 1,
'allow_cid' => '',
'allow_gid' => '',
'deny_cid' => '',
'deny_gid' => '',
],
[
'id' => 2,
'visible' => 1,
'contact-id' => 42,
'author-id' => 42,
'owner-id' => 42,
'uid' => 42,
'verb' => 'http://activitystrea.ms/schema/1.0/post',
'unseen' => 0,
'body' => 'Reply',
'parent' => 1,
'author-link' => 'http://localhost/profile/selfcontact',
'wall' => 1,
'starred' => 0,
'origin' => 1,
],
[
'id' => 3,
'visible' => 1,
'contact-id' => 43,
'author-id' => 43,
'owner-id' => 42,
'uid' => 42,
'verb' => 'http://activitystrea.ms/schema/1.0/post',
'unseen' => 0,
'body' => 'Other user status',
'parent' => 3,
'author-link' => 'http://localhost/profile/othercontact',
'wall' => 1,
'starred' => 0,
'origin' => 1,
],
[
'id' => 4,
'visible' => 1,
'contact-id' => 44,
'author-id' => 44,
'owner-id' => 42,
'uid' => 42,
'verb' => 'http://activitystrea.ms/schema/1.0/post',
'unseen' => 0,
'body' => 'Friend user reply',
'parent' => 1,
'author-link' => 'http://localhost/profile/othercontact',
'wall' => 1,
'starred' => 0,
'origin' => 1,
],
[
'id' => 5,
'visible' => 1,
'contact-id' => 42,
'author-id' => 42,
'owner-id' => 42,
'uid' => 42,
'verb' => 'http://activitystrea.ms/schema/1.0/post',
'unseen' => 0,
'body' => '[share]Shared status[/share]',
'parent' => 1,
'author-link' => 'http://localhost/profile/othercontact',
'wall' => 1,
'starred' => 0,
'origin' => 1,
'allow_cid' => '',
'allow_gid' => '',
'deny_cid' => '',
'deny_gid' => '',
],
[
'id' => 6,
'visible' => 1,
'contact-id' => 44,
'author-id' => 44,
'owner-id' => 42,
'uid' => 42,
'verb' => 'http://activitystrea.ms/schema/1.0/post',
'unseen' => 0,
'body' => 'Friend user status',
'parent' => 6,
'author-link' => 'http://localhost/profile/othercontact',
'wall' => 1,
'starred' => 0,
'origin' => 1,
],
],
'thread' => [
[
'iid' => 1,
'visible' => 1,
'contact-id' => 42,
'author-id' => 42,
'owner-id' => 42,
'uid' => 42,
'wall' => 1,
],
[
'iid' => 3,
'visible' => 1,
'contact-id' => 43,
'author-id' => 43,
'owner-id' => 43,
'uid' => 0,
'wall' => 1,
],
[
'iid' => 6,
'visible' => 1,
'contact-id' => 44,
'author-id' => 44,
'owner-id' => 44,
'uid' => 0,
'wall' => 1,
],
],
'group' => [
[
'id' => 1,
'uid' => 42,
'visible' => 1,
'name' => 'Visible list',
],
[
'id' => 2,
'uid' => 42,
'visible' => 0,
'name' => 'Private list',
],
],
'search' => [
[
'id' => 1,
'term' => 'Saved search',
'uid' => 42,
],
],
];

View File

@ -1,219 +0,0 @@
---
# Empty these tables
cache:
conversation:
pconfig:
photo:
workerqueue:
mail:
item-delivery-data:
# Base test config to avoid notice messages
config:
-
cat: system
k: url
v: http://localhost
-
cat: config
k: hostname
v: localhost
-
cat: system
k: worker_dont_fork
v: 1
# Populate tables with test data
user:
-
uid: 42
username: Test user
nickname: selfcontact
verified: 1
password: $2y$10$DLRNTRmJgKe1cSrFJ5Jb0edCqvXlA9sh/RHdSnfxjbR.04yZRm4Qm
theme: frio
contact:
-
id: 42
uid: 42
name: Self contact
nick: selfcontact
self: 1
nurl: http://localhost/profile/selfcontact
url: http://localhost/profile/selfcontact
about: User used in tests
pending: 0
blocked: 0
rel: 1
network: dfrn
-
id: 43
uid: 0
# Having the same name and nick allows us to test
# the fallback to api_get_nick() in api_get_user()
name: othercontact
nick: othercontact
self: 0
nurl: http://localhost/profile/othercontact
url: http://localhost/profile/othercontact
pending: 0
blocked: 0
rel: 0
network: dfrn
-
id: 44
uid: 42
name: Friend contact
nick: friendcontact
self: 0
nurl: http://localhost/profile/friendcontact
url: http://localhost/profile/friendcontact
pending: 0
blocked: 0
rel: 2
network: dfrn
item:
-
id: 1
visible: 1
contact-id: 42
author-id: 42
owner-id: 45
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 1
body: Parent status
parent: 1
author-link: http://localhost/profile/selfcontact
wall: 1
starred: 1
origin: 1
allow_cid: ''
allow_gid: ''
deny_cid: ''
deny_gid: ''
-
id: 2
visible: 1
contact-id: 42
author-id: 42
owner-id: 45
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 0
body: Reply
parent: 1
author-link: http://localhost/profile/selfcontact
wall: 1
starred: 0
origin: 1
-
id: 3
visible: 1
contact-id: 43
author-id: 43
owner-id: 42
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 0
body: Other user status
parent: 3
author-link: http://localhost/profile/othercontact
wall: 1
starred: 0
origin: 1
-
id: 4
visible: 1
contact-id: 44
author-id: 44
owner-id: 42
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 0
body: Friend user reply
parent: 1
author-link: http://localhost/profile/othercontact
wall: 1
starred: 0
origin: 1
-
id: 5
visible: 1
contact-id: 42
author-id: 42
owner-id: 42
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 0
body: '[share]Shared status[/share]'
parent: 1
author-link: http://localhost/profile/othercontact
wall: 1
starred: 0
origin: 1
allow_cid: ''
allow_gid: ''
deny_cid: ''
deny_gid: ''
-
id: 6
visible: 1
contact-id: 44
author-id: 44
owner-id: 42
uid: 42
verb: http://activitystrea.ms/schema/1.0/post
unseen: 0
body: Friend user status
parent: 6
author-link: http://localhost/profile/othercontact
wall: 1
starred: 0
origin: 1
thread:
-
iid: 1
visible: 1
contact-id: 42
author-id: 42
owner-id: 42
uid: 42
wall: 1
-
iid: 3
visible: 1
contact-id: 43
author-id: 43
owner-id: 43
uid: 0
wall: 1
-
iid: 6
visible: 1
contact-id: 44
author-id: 44
owner-id: 44
uid: 0
wall: 1
group:
-
id: 1
uid: 42
visible: 1
name: Visible list
-
id: 2
uid: 42
visible: 0
name: Private list
search:
-
id: 1
term: Saved search
uid: 42

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);
} }