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;
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

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']);
$response = $notification->run();
self::assertJson($response->getBody());
$this->toJson($response);
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]);
$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);

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]);
$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);

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']);
$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);

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]);
$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);

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']);
$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()

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]);
$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);

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']);
$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);

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']);
$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);

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]);
$response = $lists->run();
$body = (string)$response->getBody();
self::assertJson($body);
$json = json_decode($body);
$json = $this->toJson($response);
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]);
$response = $lists->run();
$body = (string)$response->getBody();
self::assertJson($body);
$json = json_decode($body);
$json = $this->toJson($response);
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]);
$response = $lists->run();
$body = (string)$response->getBody();
self::assertJson($body);
$json = json_decode($body);
$json = $this->toJson($response);
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]);
$response = $lists->run();
$body = (string)$response->getBody();
self::assertJson($body);
$json = json_decode($body);
$json = $this->toJson($response);
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]);
$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);

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();
$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);
}
/**

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']);
$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);