From df9ebf5e8e71f51bf7cffb9d5500c3c662c471c2 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 28 Jul 2019 20:46:29 +0200 Subject: [PATCH] Replace api.yml to api.fixture.php - Remove yaml dependency - Add PHP array based fixture possibility --- composer.json | 3 +- composer.lock | 14 +- tests/DatabaseTest.php | 27 ++++ tests/Util/Database/YamlDataSet.php | 50 ------ tests/datasets/api.fixture.php | 242 ++++++++++++++++++++++++++++ tests/datasets/api.yml | 219 ------------------------- tests/include/ApiTest.php | 4 +- 7 files changed, 278 insertions(+), 281 deletions(-) delete mode 100644 tests/Util/Database/YamlDataSet.php create mode 100644 tests/datasets/api.fixture.php delete mode 100644 tests/datasets/api.yml diff --git a/composer.json b/composer.json index 3108b1935d..82ab4d93ba 100644 --- a/composer.json +++ b/composer.json @@ -118,8 +118,7 @@ "phpunit/php-token-stream": "^1.4.2", "mikey179/vfsstream": "^1.6", "mockery/mockery": "^1.2", - "johnkary/phpunit-speedtrap": "1.1", - "symfony/yaml": "^3.0" + "johnkary/phpunit-speedtrap": "1.1" }, "scripts": { "test": "phpunit" diff --git a/composer.lock b/composer.lock index a4d076f32e..e1cefeed8c 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "ab69901def2561415a0de62fb86df9bf", + "content-hash": "9f101b2b4a651f425e155d0029223774", "packages": [ { "name": "asika/simple-console", @@ -4209,16 +4209,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.9.0", + "version": "v1.11.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19" + "reference": "82ebae02209c21113908c229e9883c419720738a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/82ebae02209c21113908c229e9883c419720738a", + "reference": "82ebae02209c21113908c229e9883c419720738a", "shasum": "" }, "require": { @@ -4230,7 +4230,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.9-dev" + "dev-master": "1.11-dev" } }, "autoload": { @@ -4263,7 +4263,7 @@ "polyfill", "portable" ], - "time": "2018-08-06T14:22:27+00:00" + "time": "2019-02-06T07:57:58+00:00" }, { "name": "symfony/yaml", diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 1783bc6053..4f0275493c 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -5,6 +5,7 @@ namespace Friendica\Test; +use Friendica\Database\Database; use Friendica\Test\Util\Database\StaticDatabase; /** @@ -30,4 +31,30 @@ abstract class DatabaseTest extends MockedTest 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); + } + } + } } diff --git a/tests/Util/Database/YamlDataSet.php b/tests/Util/Database/YamlDataSet.php deleted file mode 100644 index 58e363e8f1..0000000000 --- a/tests/Util/Database/YamlDataSet.php +++ /dev/null @@ -1,50 +0,0 @@ -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); - } - } - } -} diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php new file mode 100644 index 0000000000..475fa03854 --- /dev/null +++ b/tests/datasets/api.fixture.php @@ -0,0 +1,242 @@ + [ + [ + '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, + ], + ], +]; \ No newline at end of file diff --git a/tests/datasets/api.yml b/tests/datasets/api.yml deleted file mode 100644 index 1fc133cf92..0000000000 --- a/tests/datasets/api.yml +++ /dev/null @@ -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 diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 28e2542654..465641af2c 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -15,7 +15,6 @@ use Friendica\Core\System; use Friendica\Database\Database; use Friendica\Network\HTTPException; use Friendica\Test\Util\Database\StaticDatabase; -use Friendica\Test\Util\Database\YamlDataSet; use Monolog\Handler\TestHandler; require_once __DIR__ . '/../../include/api.php'; @@ -61,8 +60,7 @@ class ApiTest extends DatabaseTest $dba = $dice->create(Database::class); // Load the API dataset for the whole API - $ymlTester = new YamlDataSet(__DIR__ . '/../datasets/api.yml'); - $ymlTester->load($dba); + $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba); $this->app = BaseObject::getApp(); -- 2.39.5