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;
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
{
// 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
DI::app()->setIsLoggedIn(true);
AuthTestConfig::$authenticated = true;
- AuthTestConfig::$user_id = 42;
+ AuthTestConfig::$user_id = 42;
$this->installAuthTest();
}
$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());
}
}
$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);
$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);
$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);
$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);
$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()
$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);
$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);
$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);
$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);
}
$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);
}
$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);
}
$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);
}
$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);
];
$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);
}
/**
$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);