2021-11-12 21:29:58 +00:00
< ? php
namespace Friendica\Test\src\Module\Api ;
use Friendica\App\Arguments ;
2021-11-21 10:45:50 +00:00
use Friendica\App\BaseURL ;
2021-11-12 21:29:58 +00:00
use Friendica\Core\L10n ;
2021-11-21 10:45:50 +00:00
use Friendica\Factory\Api\Twitter\User ;
2021-11-21 20:52:36 +00:00
use Friendica\Module\Api\ApiResponse ;
2021-11-12 21:29:58 +00:00
use Friendica\Test\MockedTest ;
use Psr\Log\NullLogger ;
class ApiResponseTest extends MockedTest
{
public function testErrorWithJson ()
{
$l10n = \Mockery :: mock ( L10n :: class );
$args = \Mockery :: mock ( Arguments :: class );
$args -> shouldReceive ( 'getQueryString' ) -> andReturn ( '' );
2021-11-21 10:45:50 +00:00
$baseUrl = \Mockery :: mock ( BaseURL :: class );
$twitterUser = \Mockery :: mock ( User :: class );
2021-11-12 21:29:58 +00:00
2021-11-21 20:52:36 +00:00
$response = new ApiResponse ( $l10n , $args , new NullLogger (), $baseUrl , $twitterUser );
2021-11-12 21:29:58 +00:00
$response -> error ( 200 , 'OK' , 'error_message' , 'json' );
2021-11-21 20:52:36 +00:00
self :: assertEquals ( '{"error":"error_message","code":"200 OK","request":""}' , $response -> getContent ());
2021-11-12 21:29:58 +00:00
}
public function testErrorWithXml ()
{
$l10n = \Mockery :: mock ( L10n :: class );
$args = \Mockery :: mock ( Arguments :: class );
$args -> shouldReceive ( 'getQueryString' ) -> andReturn ( '' );
2021-11-21 10:45:50 +00:00
$baseUrl = \Mockery :: mock ( BaseURL :: class );
$twitterUser = \Mockery :: mock ( User :: class );
2021-11-12 21:29:58 +00:00
2021-11-21 20:52:36 +00:00
$response = new ApiResponse ( $l10n , $args , new NullLogger (), $baseUrl , $twitterUser );
2021-11-12 21:29:58 +00:00
$response -> error ( 200 , 'OK' , 'error_message' , 'xml' );
self :: assertEquals ( '<?xml version="1.0"?>' . " \n " .
'<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
'xmlns:georss="http://www.georss.org/georss">' . " \n " .
' <error>error_message</error>' . " \n " .
' <code>200 OK</code>' . " \n " .
' <request/>' . " \n " .
'</status>' . " \n " ,
2021-11-21 20:52:36 +00:00
$response -> getContent ());
2021-11-12 21:29:58 +00:00
}
public function testErrorWithRss ()
{
$l10n = \Mockery :: mock ( L10n :: class );
$args = \Mockery :: mock ( Arguments :: class );
$args -> shouldReceive ( 'getQueryString' ) -> andReturn ( '' );
2021-11-21 10:45:50 +00:00
$baseUrl = \Mockery :: mock ( BaseURL :: class );
$twitterUser = \Mockery :: mock ( User :: class );
2021-11-12 21:29:58 +00:00
2021-11-21 20:52:36 +00:00
$response = new ApiResponse ( $l10n , $args , new NullLogger (), $baseUrl , $twitterUser );
2021-11-12 21:29:58 +00:00
$response -> error ( 200 , 'OK' , 'error_message' , 'rss' );
self :: assertEquals (
'<?xml version="1.0"?>' . " \n " .
'<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
'xmlns:georss="http://www.georss.org/georss">' . " \n " .
' <error>error_message</error>' . " \n " .
' <code>200 OK</code>' . " \n " .
' <request/>' . " \n " .
'</status>' . " \n " ,
2021-11-21 20:52:36 +00:00
$response -> getContent ());
2021-11-12 21:29:58 +00:00
}
public function testErrorWithAtom ()
{
$l10n = \Mockery :: mock ( L10n :: class );
$args = \Mockery :: mock ( Arguments :: class );
$args -> shouldReceive ( 'getQueryString' ) -> andReturn ( '' );
2021-11-21 10:45:50 +00:00
$baseUrl = \Mockery :: mock ( BaseURL :: class );
$twitterUser = \Mockery :: mock ( User :: class );
2021-11-12 21:29:58 +00:00
2021-11-21 20:52:36 +00:00
$response = new ApiResponse ( $l10n , $args , new NullLogger (), $baseUrl , $twitterUser );
2021-11-12 21:29:58 +00:00
$response -> error ( 200 , 'OK' , 'error_message' , 'atom' );
self :: assertEquals (
'<?xml version="1.0"?>' . " \n " .
'<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
'xmlns:georss="http://www.georss.org/georss">' . " \n " .
' <error>error_message</error>' . " \n " .
' <code>200 OK</code>' . " \n " .
' <request/>' . " \n " .
'</status>' . " \n " ,
2021-11-21 20:52:36 +00:00
$response -> getContent ());
2021-11-12 21:29:58 +00:00
}
2021-11-12 21:44:59 +00:00
public function testUnsupported ()
{
$l10n = \Mockery :: mock ( L10n :: class );
$l10n -> shouldReceive ( 't' ) -> andReturnUsing ( function ( $args ) {
return $args ;
});
$args = \Mockery :: mock ( Arguments :: class );
$args -> shouldReceive ( 'getQueryString' ) -> andReturn ( '' );
2021-11-21 10:45:50 +00:00
$baseUrl = \Mockery :: mock ( BaseURL :: class );
$twitterUser = \Mockery :: mock ( User :: class );
2021-11-12 21:44:59 +00:00
2021-11-21 20:52:36 +00:00
$response = new ApiResponse ( $l10n , $args , new NullLogger (), $baseUrl , $twitterUser );
2021-11-12 21:44:59 +00:00
$response -> unsupported ();
2021-11-21 20:52:36 +00:00
self :: assertEquals ( '{"error":"API endpoint %s %s is not implemented","error_description":"The API endpoint is currently not implemented but might be in the future."}' , $response -> getContent ());
2021-11-12 21:44:59 +00:00
}
2021-11-12 21:29:58 +00:00
}