Simplify json testing

This commit is contained in:
Philipp 2021-12-09 20:53:29 +01:00
parent 6a813eec92
commit 354c2d828a
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
17 changed files with 45 additions and 66 deletions

View file

@ -22,6 +22,7 @@
namespace Friendica\Test\src\Module\Api; namespace Friendica\Test\src\Module\Api;
use Friendica\App; use Friendica\App;
use Friendica\Capabilities\ICanCreateResponses;
use Friendica\Core\Addon; use Friendica\Core\Addon;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Database\Database; use Friendica\Database\Database;
@ -32,6 +33,7 @@ use Friendica\Test\FixtureTest;
use Friendica\Test\Util\AppDouble; use Friendica\Test\Util\AppDouble;
use Friendica\Test\Util\AuthenticationDouble; use Friendica\Test\Util\AuthenticationDouble;
use Friendica\Test\Util\AuthTestConfig; use Friendica\Test\Util\AuthTestConfig;
use Psr\Http\Message\ResponseInterface;
abstract class ApiTest extends FixtureTest abstract class ApiTest extends FixtureTest
{ {
@ -50,6 +52,24 @@ abstract class ApiTest extends FixtureTest
// We could probably do more checks here. // 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 protected function setUp(): void
{ {
parent::setUp(); // TODO: Change the autogenerated stub parent::setUp(); // TODO: Change the autogenerated stub
@ -63,7 +83,7 @@ abstract class ApiTest extends FixtureTest
DI::app()->setIsLoggedIn(true); DI::app()->setIsLoggedIn(true);
AuthTestConfig::$authenticated = true; AuthTestConfig::$authenticated = true;
AuthTestConfig::$user_id = 42; AuthTestConfig::$user_id = 42;
$this->installAuthTest(); $this->installAuthTest();
} }

View 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']); $notification = new Notification(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
$response = $notification->run(); $response = $notification->run();
self::assertJson($response->getBody()); $this->toJson($response);
self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders()); self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
} }
} }

View 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]); $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']); $response = $delete->run(['photo_id' => '709057080661a283a6aa598501504178']);
$responseText = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($responseText);
$json = json_decode($responseText);
self::assertEquals('deleted', $json->result); self::assertEquals('deleted', $json->result);
self::assertEquals('photo with id `709057080661a283a6aa598501504178` has been deleted from server.', $json->message); self::assertEquals('photo with id `709057080661a283a6aa598501504178` has been deleted from server.', $json->message);

View 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]); $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']); $response = $delete->run(['album' => 'test_album']);
$responseText = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($responseText);
$json = json_decode($responseText);
self::assertEquals('deleted', $json->result); self::assertEquals('deleted', $json->result);
self::assertEquals('album `test_album` with all containing photos has been deleted.', $json->message); self::assertEquals('album `test_album` with all containing photos has been deleted.', $json->message);

View 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']); $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(); $json = $this->toJson($response);
self::assertJson($responseBody);
$json = json_decode($responseBody);
self::assertEquals('updated', $json->result); self::assertEquals('updated', $json->result);
self::assertEquals('album `test_album` with all containing photos has been renamed to `test_album_2`.', $json->message); self::assertEquals('album `test_album` with all containing photos has been renamed to `test_album_2`.', $json->message);

View 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]); $config = new Config(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
$response = $config->run(); $response = $config->run();
$body = (string)$response->getBody();
self::assertJson($body); $json = $this->toJson($response);
$json = json_decode($body);
self::assertEquals(1, 1);
self::assertEquals('localhost', $json->site->server); self::assertEquals('localhost', $json->site->server);
self::assertEquals('frio', $json->site->theme); self::assertEquals('frio', $json->site->theme);

View 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']); $test = new Test(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
$response = $test->run(); $response = $test->run();
$json = $this->toJson($response);
self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders()); self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals('"ok"', $response->getBody()); self::assertEquals('ok', $json);
} }
public function testXml() public function testXml()

View 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]); $verifyCredentials = new VerifyCredentials(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
$response = $verifyCredentials->run(); $response = $verifyCredentials->run();
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
self::assertEquals(48, $json->id); self::assertEquals(48, $json->id);
self::assertIsArray($json->emojis); self::assertIsArray($json->emojis);

View 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']); $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(); $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(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals(150, $result->remaining_hits); self::assertEquals(150, $result->remaining_hits);

View 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']); $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']); $response = $updateProfile->run(['name' => 'new_name', 'description' => 'new_description']);
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
self::assertEquals('new_name', $json->name); self::assertEquals('new_name', $json->name);
self::assertEquals('new_description', $json->description); self::assertEquals('new_description', $json->description);

View 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]); $lists = new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
$response = $lists->run(); $response = $lists->run();
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
self::assertIsArray($json->users); self::assertIsArray($json->users);
} }

View 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]); $lists = new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
$response = $lists->run(); $response = $lists->run();
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
self::assertIsArray($json->users); self::assertIsArray($json->users);
} }

View 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]); $lists = new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
$response = $lists->run(); $response = $lists->run();
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
self::assertIsArray($json->users); self::assertIsArray($json->users);
} }

View 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]); $lists = new Incoming(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]);
$response = $lists->run(); $response = $lists->run();
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
self::assertIsArray($json->ids); self::assertIsArray($json->ids);
} }

View 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]); $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]); $response = $lists->run(['list_id' => 1, 'page' => -1, 'max_id' => 10]);
$body = (string)$response->getBody(); $json = $this->toJson($response);
self::assertJson($body);
$json = json_decode($body);
foreach ($json as $status) { foreach ($json as $status) {
self::assertIsString($status->text); self::assertIsString($status->text);

View 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(); $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']); $media = $this->toJson($response);
self::assertEquals(1, $media['image']['w']);
self::assertEquals(1, $media['image']['h']); self::assertEquals('image/png', $media->image->image_type);
self::assertNotEmpty($media['image']['friendica_preview_url']); self::assertEquals(1, $media->image->w);
self::assertEquals(1, $media->image->h);
self::assertNotEmpty($media->image->friendica_preview_url);
} }
/** /**

View 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']); $savedSearch = new SavedSearches(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
$response = $savedSearch->run(); $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(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals(1, $result[0]->id); self::assertEquals(1, $result[0]->id);