Replace api.yml to api.fixture.php

- Remove yaml dependency
- Add PHP array based fixture possibility
This commit is contained in:
Philipp Holzer 2019-07-28 20:46:29 +02:00
parent b08ac3c0a7
commit df9ebf5e8e
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
7 changed files with 278 additions and 281 deletions

View file

@ -118,8 +118,7 @@
"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"

14
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": "ab69901def2561415a0de62fb86df9bf", "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,7 +4263,7 @@
"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",

View file

@ -5,6 +5,7 @@
namespace Friendica\Test; namespace Friendica\Test;
use Friendica\Database\Database;
use Friendica\Test\Util\Database\StaticDatabase; use Friendica\Test\Util\Database\StaticDatabase;
/** /**
@ -30,4 +31,30 @@ abstract class DatabaseTest extends MockedTest
parent::tearDown(); parent::tearDown();
} }
/**
* Loads a given DB fixture for this DB test
*
* @param string $fixture The path to the fixture
* @param Database $dba The DB connection
*
* @throws \Exception
*/
protected function loadFixture(string $fixture, Database $dba)
{
$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

@ -1,50 +0,0 @@
<?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

@ -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: 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

@ -15,7 +15,6 @@ use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Test\Util\Database\StaticDatabase; use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\Database\YamlDataSet;
use Monolog\Handler\TestHandler; use Monolog\Handler\TestHandler;
require_once __DIR__ . '/../../include/api.php'; require_once __DIR__ . '/../../include/api.php';
@ -61,8 +60,7 @@ class ApiTest extends DatabaseTest
$dba = $dice->create(Database::class); $dba = $dice->create(Database::class);
// Load the API dataset for the whole API // Load the API dataset for the whole API
$ymlTester = new YamlDataSet(__DIR__ . '/../datasets/api.yml'); $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba);
$ymlTester->load($dba);
$this->app = BaseObject::getApp(); $this->app = BaseObject::getApp();