]> git.mxchange.org Git - friendica.git/commitdiff
Reenable Twitter/Lookup tests & add standard assertions
authorPhilipp <admin@philipp.info>
Thu, 9 Dec 2021 20:32:17 +0000 (21:32 +0100)
committerPhilipp <admin@philipp.info>
Thu, 9 Dec 2021 20:33:02 +0000 (21:33 +0100)
src/Module/Api/Twitter/Users/Lookup.php
tests/legacy/ApiTest.php
tests/src/Module/Api/ApiTest.php
tests/src/Module/Api/Twitter/Users/LookupTest.php

index 9d4c460c6eb890df8e0bffad4cfc614012193515..2633020e846d8eab23d624079fe5899feaf7bf8d 100644 (file)
@@ -39,8 +39,8 @@ class Lookup extends BaseApi
 
                $users = [];
 
-               if (!empty($_REQUEST['user_id'])) {
-                       foreach (explode(',', $_REQUEST['user_id']) as $cid) {
+               if (!empty($request['user_id'])) {
+                       foreach (explode(',', $request['user_id']) as $cid) {
                                if (!empty($cid) && is_numeric($cid)) {
                                        $users[] = DI::twitterUser()->createFromContactId((int)$cid, $uid, false)->toArray();
                                }
index 2067a35362d99326e603fb223ae6e570a92b84c1..fea2bd5f1bf4b07bc5baaec89dca5091ebc759a5 100644 (file)
@@ -112,55 +112,6 @@ class ApiTest extends FixtureTest
                BasicAuth::setCurrentUserID($this->selfUser['id']);
        }
 
-       /**
-        * Assert that an user array contains expected keys.
-        *
-        * @param array $user User array
-        *
-        * @return void
-        */
-       private function assertSelfUser(array $user)
-       {
-               self::assertEquals($this->selfUser['id'], $user['uid']);
-               self::assertEquals($this->selfUser['id'], $user['cid']);
-               self::assertEquals(1, $user['self']);
-               self::assertEquals('DFRN', $user['location']);
-               self::assertEquals($this->selfUser['name'], $user['name']);
-               self::assertEquals($this->selfUser['nick'], $user['screen_name']);
-               self::assertEquals('dfrn', $user['network']);
-               self::assertTrue($user['verified']);
-       }
-
-       /**
-        * Assert that an user array contains expected keys.
-        *
-        * @param array $user User array
-        *
-        * @return void
-        */
-       private function assertOtherUser(array $user = [])
-       {
-               self::assertEquals($this->otherUser['id'], $user['id']);
-               self::assertEquals($this->otherUser['id'], $user['id_str']);
-               self::assertEquals($this->otherUser['name'], $user['name']);
-               self::assertEquals($this->otherUser['nick'], $user['screen_name']);
-               self::assertFalse($user['verified']);
-       }
-
-       /**
-        * Assert that a status array contains expected keys.
-        *
-        * @param array $status Status array
-        *
-        * @return void
-        */
-       private function assertStatus(array $status = [])
-       {
-               self::assertIsString($status['text'] ?? '');
-               self::assertIsInt($status['id'] ?? '');
-               // We could probably do more checks here.
-       }
-
        /**
         * Assert that a list array contains expected keys.
         *
index 9d690b1af3ac6dcfab614882bb6fb53f86ecb448..09fbb7bbe95e2a82ccbec2e5db480b5e804d75bb 100644 (file)
@@ -37,6 +37,31 @@ use Psr\Http\Message\ResponseInterface;
 
 abstract class ApiTest extends FixtureTest
 {
+       // User data that the test database is populated with
+       const SELF_USER = [
+               'id'   => 42,
+               'name' => 'Self contact',
+               'nick' => 'selfcontact',
+               'nurl' => 'http://localhost/profile/selfcontact'
+       ];
+
+       const FRIEND_USER = [
+               'id'   => 44,
+               'name' => 'Friend contact',
+               'nick' => 'friendcontact',
+               'nurl' => 'http://localhost/profile/friendcontact'
+       ];
+
+       const OTHER_USER = [
+               'id'   => 43,
+               'name' => 'othercontact',
+               'nick' => 'othercontact',
+               'nurl' => 'http://localhost/profile/othercontact'
+       ];
+
+       // User ID that we know is not in the database
+       const WRONG_USER_ID = 666;
+
        /**
         * Assert that the string is XML and contain the root element.
         *
@@ -52,6 +77,55 @@ abstract class ApiTest extends FixtureTest
                // We could probably do more checks here.
        }
 
+       /**
+        * Assert that an user array contains expected keys.
+        *
+        * @param \stdClass $user User
+        *
+        * @return void
+        */
+       protected function assertSelfUser(\stdClass $user)
+       {
+               self::assertEquals(self::SELF_USER['id'], $user->uid);
+               self::assertEquals(self::SELF_USER['id'], $user->cid);
+               self::assertEquals(1, $user->self);
+               self::assertEquals('DFRN', $user->location);
+               self::assertEquals(self::SELF_USER['name'], $user->name);
+               self::assertEquals(self::SELF_USER['nick'], $user->screen_name);
+               self::assertEquals('dfrn', $user->network);
+               self::assertTrue($user->verified);
+       }
+
+       /**
+        * Assert that an user array contains expected keys.
+        *
+        * @param \stdClass $user User
+        *
+        * @return void
+        */
+       protected function assertOtherUser(\stdClass $user)
+       {
+               self::assertEquals(self::OTHER_USER['id'], $user->id);
+               self::assertEquals(self::OTHER_USER['id'], $user->id_str);
+               self::assertEquals(self::OTHER_USER['name'], $user->name);
+               self::assertEquals(self::OTHER_USER['nick'], $user->screen_name);
+               self::assertFalse($user->verified);
+       }
+
+       /**
+        * Assert that a status array contains expected keys.
+        *
+        * @param array $status Status array
+        *
+        * @return void
+        */
+       protected function assertStatus(array $status = [])
+       {
+               self::assertIsString($status['text'] ?? '');
+               self::assertIsInt($status['id'] ?? '');
+               // We could probably do more checks here.
+       }
+
        /**
         * Transforms a response into a JSON class
         *
index ddcedbc7636fdbc2929410f34947814ca885ef9e..e54d20dcfb598986fe3b6afc964e4610c174de0c 100644 (file)
@@ -2,6 +2,10 @@
 
 namespace Friendica\Test\src\Module\Api\Twitter\Users;
 
+use Friendica\App\Router;
+use Friendica\DI;
+use Friendica\Module\Api\Twitter\Users\Lookup;
+use Friendica\Network\HTTPException\NotFoundException;
 use Friendica\Test\src\Module\Api\ApiTest;
 
 class LookupTest extends ApiTest
@@ -13,8 +17,10 @@ class LookupTest extends ApiTest
         */
        public function testApiUsersLookup()
        {
-               // $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
-               // api_users_lookup('json');
+               $this->expectException(NotFoundException::class);
+
+               $lookup = new Lookup(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
+               $lookup->run();
        }
 
        /**
@@ -24,8 +30,11 @@ class LookupTest extends ApiTest
         */
        public function testApiUsersLookupWithUserId()
        {
-               // $_REQUEST['user_id'] = $this->otherUser['id'];
-               // $result              = api_users_lookup('json');
-               // self::assertOtherUser($result['users'][0]);
+               $lookup = new Lookup(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
+               $respone = $lookup->run(['user_id' => static::OTHER_USER['id']]);
+
+               $json = $this->toJson($respone);
+
+               self::assertOtherUser($json[0]);
        }
 }