]> git.mxchange.org Git - friendica.git/commitdiff
Simplify json testing
authorPhilipp <admin@philipp.info>
Thu, 9 Dec 2021 19:53:29 +0000 (20:53 +0100)
committerPhilipp <admin@philipp.info>
Thu, 9 Dec 2021 19:53:29 +0000 (20:53 +0100)
17 files changed:
tests/src/Module/Api/ApiTest.php
tests/src/Module/Api/Friendica/NotificationTest.php
tests/src/Module/Api/Friendica/Photo/DeleteTest.php
tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php
tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php
tests/src/Module/Api/GnuSocial/GnuSocial/ConfigTest.php
tests/src/Module/Api/GnuSocial/Help/TestTest.php
tests/src/Module/Api/Mastodon/Accounts/VerifyCredentialsTest.php
tests/src/Module/Api/Twitter/Account/RateLimitStatusTest.php
tests/src/Module/Api/Twitter/Account/UpdateProfileTest.php
tests/src/Module/Api/Twitter/Blocks/ListsTest.php
tests/src/Module/Api/Twitter/Followers/ListsTest.php
tests/src/Module/Api/Twitter/Friends/ListsTest.php
tests/src/Module/Api/Twitter/Friendships/IncomingTest.php
tests/src/Module/Api/Twitter/Lists/StatusesTest.php
tests/src/Module/Api/Twitter/Media/UploadTest.php
tests/src/Module/Api/Twitter/SavedSearchesTest.php

index 154f54a1cf41debd8fb8e331136e5358ea2e523d..9d690b1af3ac6dcfab614882bb6fb53f86ecb448 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Test\src\Module\Api;
 
 use Friendica\App;
+use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\Core\Addon;
 use Friendica\Core\Hook;
 use Friendica\Database\Database;
@@ -32,6 +33,7 @@ use Friendica\Test\FixtureTest;
 use Friendica\Test\Util\AppDouble;
 use Friendica\Test\Util\AuthenticationDouble;
 use Friendica\Test\Util\AuthTestConfig;
+use Psr\Http\Message\ResponseInterface;
 
 abstract class ApiTest extends FixtureTest
 {
@@ -50,6 +52,24 @@ abstract class ApiTest extends FixtureTest
                // We could probably do more checks here.
        }
 
+       /**
+        * Transforms a response into a JSON class
+        *
+        * @param ResponseInterface $response
+        *
+        * @return mixed
+        */
+       protected function toJson(ResponseInterface $response)
+       {
+               self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
+
+               $body = (string)$response->getBody();
+
+               self::assertJson($body);
+
+               return json_decode($body);
+       }
+
        protected function setUp(): void
        {
                parent::setUp(); // TODO: Change the autogenerated stub
@@ -63,7 +83,7 @@ abstract class ApiTest extends FixtureTest
                DI::app()->setIsLoggedIn(true);
 
                AuthTestConfig::$authenticated = true;
-               AuthTestConfig::$user_id = 42;
+               AuthTestConfig::$user_id       = 42;
 
                $this->installAuthTest();
        }
index b5e2a4166517a6e796c45ff17027ae57790d3683..c9ab001418cc2dfb56d63022c6ed7ab6606fa424 100644 (file)
@@ -78,7 +78,8 @@ XML;
                $notification = new Notification(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
                $response = $notification->run();
 
-               self::assertJson($response->getBody());
+               $this->toJson($response);
+
                self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
        }
 }
index 8c49456b36f528ccec9ddfadea31f8966163e4c2..05808d56c4407cfeacee104d81ca51e79c6f5c8f 100644 (file)
@@ -53,11 +53,7 @@ class DeleteTest extends ApiTest
                $delete   = new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]);
                $response = $delete->run(['photo_id' => '709057080661a283a6aa598501504178']);
 
-               $responseText = (string)$response->getBody();
-
-               self::assertJson($responseText);
-
-               $json = json_decode($responseText);
+               $json = $this->toJson($response);
 
                self::assertEquals('deleted', $json->result);
                self::assertEquals('photo with id `709057080661a283a6aa598501504178` has been deleted from server.', $json->message);
index 88355483a32b343779d7ac759d7df0fe6e5456fd..230ba70c894189e1818b8960f3e34242ca16f8f0 100644 (file)
@@ -49,11 +49,7 @@ class DeleteTest extends ApiTest
                $delete   = new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]);
                $response = $delete->run(['album' => 'test_album']);
 
-               $responseText = (string)$response->getBody();
-
-               self::assertJson($responseText);
-
-               $json = json_decode($responseText);
+               $json = $this->toJson($response);
 
                self::assertEquals('deleted', $json->result);
                self::assertEquals('album `test_album` with all containing photos has been deleted.', $json->message);
index 22ca155cbb82390fcfad15cd2ac9cf6339a61d0c..fa4638e370d057cbfd70f3d4768618064e6effee 100644 (file)
@@ -58,11 +58,7 @@ class UpdateTest extends ApiTest
 
                $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(['album' => 'test_album', 'album_new' => 'test_album_2']);
 
-               $responseBody = (string)$response->getBody();
-
-               self::assertJson($responseBody);
-
-               $json = json_decode($responseBody);
+               $json = $this->toJson($response);
 
                self::assertEquals('updated', $json->result);
                self::assertEquals('album `test_album` with all containing photos has been renamed to `test_album_2`.', $json->message);
index 3cc1df2961954984f18e73f58d5e99aebbdb03ef..b1874d838675fd648dc0fb190e69c21b6ee4de18 100644 (file)
@@ -19,13 +19,8 @@ class ConfigTest extends ApiTest
 
                $config   = new Config(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response = $config->run();
-               $body     = (string)$response->getBody();
 
-               self::assertJson($body);
-
-               $json = json_decode($body);
-
-               self::assertEquals(1, 1);
+               $json = $this->toJson($response);
 
                self::assertEquals('localhost', $json->site->server);
                self::assertEquals('frio', $json->site->theme);
index 5aa4286812c7dd4a61f5389a0e240b173740c57d..858c9b6cdcd236c5b4b9010b2697dada7058743a 100644 (file)
@@ -14,8 +14,10 @@ class TestTest extends ApiTest
                $test = new Test(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
                $response = $test->run();
 
+               $json = $this->toJson($response);
+
                self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
-               self::assertEquals('"ok"', $response->getBody());
+               self::assertEquals('ok', $json);
        }
 
        public function testXml()
index 5170f1cb2fa57591918e8835ee47cba50961f922..51d76696f6985194c887e9fe157015adb289ab27 100644 (file)
@@ -19,11 +19,7 @@ class VerifyCredentialsTest extends ApiTest
                $verifyCredentials = new VerifyCredentials(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response          = $verifyCredentials->run();
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                self::assertEquals(48, $json->id);
                self::assertIsArray($json->emojis);
index 1368990d081593f284e30a96d9dbff553c567d54..a76b5d87e07e2156524813224f216243f8dde08f 100644 (file)
@@ -15,7 +15,7 @@ class RateLimitStatusTest extends ApiTest
                $rateLimitStatus = new RateLimitStatus(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json']);
                $response = $rateLimitStatus->run();
 
-               $result = json_decode($response->getBody());
+               $result = $this->toJson($response);
 
                self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
                self::assertEquals(150, $result->remaining_hits);
index 4ce3420d266b799eded64a7d92d4a3575cc5190a..e06e8d484828da48ca2db431d223e7a915f4e37c 100644 (file)
@@ -17,11 +17,7 @@ class UpdateProfileTest extends ApiTest
                $updateProfile = new UpdateProfile(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST], ['extension' => 'json']);
                $response      = $updateProfile->run(['name' => 'new_name', 'description' => 'new_description']);
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                self::assertEquals('new_name', $json->name);
                self::assertEquals('new_description', $json->description);
index 4f63581a7026ac722d812e904685ba571eb85b5c..a37b59f0367e9aafdeece2ef1a6fae57cbdc9f75 100644 (file)
@@ -17,11 +17,7 @@ class ListsTest extends ApiTest
                $lists    = new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response = $lists->run();
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                self::assertIsArray($json->users);
        }
index c1e053e5a1a6f038f5d153381883384b7f675ead..7ea3863eafcdccbeb1f02182f58e0e81e8238b4a 100644 (file)
@@ -17,11 +17,7 @@ class ListsTest extends ApiTest
                $lists    = new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response = $lists->run();
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                self::assertIsArray($json->users);
        }
index 363f8d73c36907f7b959eb4ac0671367a92b6de8..7cb5bd10a7e50d0ce7d0b6a16c7fc6f65b66eae5 100644 (file)
@@ -19,11 +19,7 @@ class ListsTest extends ApiTest
                $lists    = new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response = $lists->run();
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                self::assertIsArray($json->users);
        }
index 8c5cb6426b087147629d5a86f0636f0e3a48ccac..c1309e1f0aa19f085d32bc0603e96fc12e89b895 100644 (file)
@@ -19,11 +19,7 @@ class IncomingTest extends ApiTest
                $lists    = new Incoming(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response = $lists->run();
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                self::assertIsArray($json->ids);
        }
index 5d989b71e74ef856cd078852ff64c4bd549123a9..3f47fcaca6f60d5f09404be8b65843699c5fb98e 100644 (file)
@@ -31,11 +31,7 @@ class StatusesTest extends ApiTest
                $lists    = new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
                $response = $lists->run(['list_id' => 1, 'page' => -1, 'max_id' => 10]);
 
-               $body = (string)$response->getBody();
-
-               self::assertJson($body);
-
-               $json = json_decode($body);
+               $json = $this->toJson($response);
 
                foreach ($json as $status) {
                        self::assertIsString($status->text);
index abc9d8fdc6e169bf82c6b2911ef7fc3a844a2896..5b0ce1186bd65f70e3be28d4d981ed37c180af01 100644 (file)
@@ -72,12 +72,13 @@ class UploadTest extends ApiTest
                ];
 
                $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run();
-               $media    = json_decode($response->getBody(), true);
 
-               self::assertEquals('image/png', $media['image']['image_type']);
-               self::assertEquals(1, $media['image']['w']);
-               self::assertEquals(1, $media['image']['h']);
-               self::assertNotEmpty($media['image']['friendica_preview_url']);
+               $media = $this->toJson($response);
+
+               self::assertEquals('image/png', $media->image->image_type);
+               self::assertEquals(1, $media->image->w);
+               self::assertEquals(1, $media->image->h);
+               self::assertNotEmpty($media->image->friendica_preview_url);
        }
 
        /**
index 14973b3912a726110e5788f087d2d67a139cacb2..5735f8eef09f2685b1ee07005c098412988e0a5d 100644 (file)
@@ -14,7 +14,7 @@ class SavedSearchesTest extends ApiTest
                $savedSearch = new SavedSearches(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
                $response = $savedSearch->run();
 
-               $result = json_decode($response->getBody());
+               $result = $this->toJson($response);
 
                self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
                self::assertEquals(1, $result[0]->id);