Replace api.yml to api.fixture.php
authorPhilipp Holzer <admin+github@philipp.info>
Sun, 28 Jul 2019 18:46:29 +0000 (20:46 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Sun, 28 Jul 2019 18:46:29 +0000 (20:46 +0200)
- Remove yaml dependency
- Add PHP array based fixture possibility

composer.json
composer.lock
tests/DatabaseTest.php
tests/Util/Database/YamlDataSet.php [deleted file]
tests/datasets/api.fixture.php [new file with mode: 0644]
tests/datasets/api.yml [deleted file]
tests/include/ApiTest.php

index 3108b19..82ab4d9 100644 (file)
                "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"
index a4d076f..e1cefee 100644 (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": "ab69901def2561415a0de62fb86df9bf",
+    "content-hash": "9f101b2b4a651f425e155d0029223774",
     "packages": [
         {
             "name": "asika/simple-console",
         },
         {
             "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": {
             "type": "library",
             "extra": {
                 "branch-alias": {
-                    "dev-master": "1.9-dev"
+                    "dev-master": "1.11-dev"
                 }
             },
             "autoload": {
                 "polyfill",
                 "portable"
             ],
-            "time": "2018-08-06T14:22:27+00:00"
+            "time": "2019-02-06T07:57:58+00:00"
         },
         {
             "name": "symfony/yaml",
index 1783bc6..4f02754 100644 (file)
@@ -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 (file)
index 58e363e..0000000
+++ /dev/null
@@ -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);
-                       }
-               }
-       }
-}
diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php
new file mode 100644 (file)
index 0000000..475fa03
--- /dev/null
@@ -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,
+               ],
+       ],
+];
\ No newline at end of file
diff --git a/tests/datasets/api.yml b/tests/datasets/api.yml
deleted file mode 100644 (file)
index 1fc133c..0000000
+++ /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
index 28e2542..465641a 100644 (file)
@@ -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();