2022-12-11 00:08:59 +00:00
< ? php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests ;
2023-02-05 00:55:11 +00:00
use App\Exceptions\CustomTestException ;
2022-12-04 20:13:24 +00:00
use App\Testing\Concerns\MakesHttpRequestsEx ;
2022-12-04 19:53:27 +00:00
use App\Testing\ScoutFlush ;
2022-12-04 21:43:29 +00:00
use App\Testing\SyntheticMongoDbTransaction ;
2023-02-05 00:55:11 +00:00
use App\Testing\TestExceptionsHandler ;
2022-12-21 11:29:53 +00:00
use Faker\Factory as FakerFactory ;
2022-12-11 00:08:59 +00:00
use Faker\Generator ;
2023-02-05 00:55:11 +00:00
use Illuminate\Container\Container ;
use Illuminate\Contracts\Debug\ExceptionHandler ;
2022-12-21 18:33:19 +00:00
use Illuminate\Support\Collection ;
2023-02-05 00:55:11 +00:00
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Facade ;
2022-12-16 11:00:18 +00:00
use Illuminate\Testing\TestResponse ;
2023-02-02 23:37:37 +00:00
use Jikan\MyAnimeList\MalClient ;
2023-02-05 00:55:11 +00:00
use Laravel\Lumen\Application ;
2022-12-11 00:08:59 +00:00
use Laravel\Lumen\Testing\TestCase as LumenTestCase ;
2023-02-05 00:55:11 +00:00
use MongoDB\BSON\UTCDateTime ;
2023-01-02 16:29:05 +00:00
use Spatie\Enum\Faker\FakerEnumProvider ;
2023-02-02 23:37:37 +00:00
use Symfony\Contracts\HttpClient\HttpClientInterface ;
use Symfony\Contracts\HttpClient\ResponseInterface ;
2018-04-21 12:59:48 +05:00
2022-12-11 00:08:59 +00:00
abstract class TestCase extends LumenTestCase
2018-04-21 12:59:48 +05:00
{
2022-12-04 11:38:11 +00:00
use MakesHttpRequestsEx ;
2022-12-11 00:08:59 +00:00
protected Generator $faker ;
2022-12-16 11:00:18 +00:00
protected int $maxResultsPerPage ;
2022-12-04 19:53:27 +00:00
2023-02-05 00:55:11 +00:00
protected function dummyScraperResultDocument ( string $uri , string $mediaType , array $content , ? string $wrapKey = null ) : array
{
return [
" createdAt " => new UTCDateTime (),
" modifiedAt " => new UTCDateTime (),
" request_hash " => " request: $mediaType : " . sha1 ( $uri ),
... ( $wrapKey !== null ? [ $wrapKey => $content ] : $content )
];
}
protected function dummyResultsDocument ( string $uri , string $mediaType , array $resultsContent , $hasNextPage = false , $lastVisiblePage = 1 ) : array
{
return $this -> dummyScraperResultDocument (
$uri ,
$mediaType ,
[
" results " => $resultsContent ,
" has_next_page " => $hasNextPage ,
" last_visible_page " => $lastVisiblePage
]
);
}
protected function givenDummyCharactersStaffData ( $uri , $mediaType )
{
DB :: table ( $mediaType === " anime " ? $mediaType . " _characters_staff " : $mediaType . " _characters " ) -> insert ([
" createdAt " => new UTCDateTime (),
" modifiedAt " => new UTCDateTime (),
" request_hash " => " request: $mediaType : " . sha1 ( $uri ),
" characters " => [
[
" character " => [
" mal_id " => 3 ,
" url " => " https://myanimelist.net/character/3/Jet_Black " ,
" images " => [
" jpg " => [
" image_url " => " https://cdn.myanimelist.net/images/characters/11/253723.jpg?s=6c8a19a79a88c46ae15f30e3ef5fd839 " ,
" small_image_url " => " https://cdn.myanimelist.net/images/characters/11/253723t.jpg?s=6c8a19a79a88c46ae15f30e3ef5fd839 "
],
" webp " => [
" image_url " => " https://cdn.myanimelist.net/images/characters/11/253723.webp?s=6c8a19a79a88c46ae15f30e3ef5fd839 " ,
" small_image_url " => " https://cdn.myanimelist.net/images/characters/11/253723t.webp?s=6c8a19a79a88c46ae15f30e3ef5fd839 "
]
],
" name " => " Black, Jet "
],
" role " => " Main " ,
" favorites " => 1 ,
... ( $mediaType === " anime " ? [
" voice_actors " => [
[
" person " => [
" mal_id " => 357 ,
" url " => " https://myanimelist.net/people/357/Unshou_Ishizuk " ,
" images " => [
" jpg " => [
" image_url " => " https://cdn.myanimelist.net/images/voiceactors/2/17135.jpg?s=5925123b8a7cf9b51a445c225442f0ef "
]
],
" name " => " Ishizuka, Unshou "
],
" language " => " Japanese "
]
]
] : [])
]
],
... (
$mediaType === " anime " ? [
" staff " => [
[
" person " => [
" mal_id " => 40009 ,
" url " => " https://myanimelist.net/people/40009/Yutaka_Maseba " ,
" images " => [
" jpg " => [
" image_url " => " https://cdn.myanimelist.net/images/voiceactors/3/40216.jpg?s=d9fb7a625868ec7d9cd3804fa0da3fd6 "
]
],
" name " => " Maseba, Yutaka "
],
" positions " => [
" Producer "
]
]
]
] : []
)
]);
}
2022-12-04 19:53:27 +00:00
protected function setUp () : void
{
parent :: setUp ();
2022-12-21 11:29:53 +00:00
$this -> faker = FakerFactory :: create ();
2023-01-02 16:29:05 +00:00
$this -> faker -> addProvider ( new FakerEnumProvider ( $this -> faker ));
2022-12-16 11:00:18 +00:00
$this -> maxResultsPerPage = env ( " MAX_RESULTS_PER_PAGE " , 25 );
2022-12-04 19:53:27 +00:00
}
2022-12-04 11:38:11 +00:00
2018-04-21 12:59:48 +05:00
/**
* Creates the application .
*
* @ return \Laravel\Lumen\Application
*/
public function createApplication ()
{
2023-02-05 00:55:11 +00:00
/**
* @ var Application $app
*/
2022-12-04 19:53:27 +00:00
$app = require __DIR__ . '/../bootstrap/app.php' ;
2023-02-05 00:55:11 +00:00
// a http client which fails the tests if requests are leaking to MAL.
$mockHttpClient = \Mockery :: mock ( HttpClientInterface :: class );
/** @noinspection PhpParamsInspection */
$mockHttpClient -> allows ()
-> request ( \Mockery :: any (), \Mockery :: any (), \Mockery :: any ())
-> andThrow ( new CustomTestException ( " Http Request to MAL server was attempted during testing. By default we throw this exception to indicate a buggy test. " ));
$jikan = new \Jikan\MyAnimeList\MalClient ( $mockHttpClient );
$app -> instance ( 'JikanParser' , $jikan );
$app -> singleton ( ExceptionHandler :: class , TestExceptionsHandler :: class );
2022-12-04 19:53:27 +00:00
$database = env ( 'DB_DATABASE' , 'jikan_tests' );
2022-12-10 21:16:05 +00:00
$app [ 'config' ] -> set ( 'database.connections.mongodb.database' , $database === 'jikan' ? 'jikan_tests' : $database );
2023-02-02 23:37:37 +00:00
$app [ 'config' ] -> set ( 'jikan.micro_caching_enabled' , false );
2022-12-21 11:29:53 +00:00
$app -> register ( TestServiceProvider :: class );
2023-02-05 00:55:11 +00:00
Container :: setInstance ( $app );
/** @noinspection PhpParamsInspection */
Facade :: setFacadeApplication ( $app );
2022-12-04 19:53:27 +00:00
return $app ;
}
2022-12-04 20:11:35 +00:00
protected function setUpTraits ()
2022-12-04 19:53:27 +00:00
{
2022-12-04 20:11:35 +00:00
parent :: setUpTraits ();
2022-12-04 19:53:27 +00:00
$uses = array_flip ( class_uses_recursive ( get_class ( $this )));
// we want to empty the search index
if ( isset ( $uses [ ScoutFlush :: class ])) {
/** @noinspection PhpUndefinedMethodInspection */
$this -> runScoutFlush ();
}
2022-12-04 21:43:29 +00:00
if ( isset ( $uses [ SyntheticMongoDbTransaction :: class ])) {
/** @noinspection PhpUndefinedMethodInspection */
$this -> beginDatabaseTransaction ();
}
2018-04-21 12:59:48 +05:00
}
2022-12-16 11:00:18 +00:00
public function assertPaginationData ( int $expectedCount , ? int $expectedTotal = null , ? int $perPage = null ) : TestResponse
{
if ( is_null ( $expectedTotal ))
{
$expectedTotal = $expectedCount ;
}
if ( is_null ( $perPage ))
{
$perPage = $this -> maxResultsPerPage ;
}
$this -> response -> assertJsonPath ( " pagination.items.count " , $expectedCount );
$this -> response -> assertJsonPath ( " pagination.items.total " , $expectedTotal );
return $this -> response -> assertJsonPath ( " pagination.items.per_page " , $perPage );
}
2022-12-21 18:33:19 +00:00
public function assertCollectionsStrictlyEqual ( Collection $expectedItems , Collection $actualItems ) : void
{
$this -> assertEquals ( 0 , $expectedItems -> diff ( $actualItems ) -> count ());
$this -> assertEquals ( $expectedItems -> toArray (), $actualItems -> toArray ());
}
2023-02-02 23:37:37 +00:00
protected function mockJikanParserWith404RespondingUpstream ()
{
$httpClient = \Mockery :: mock ( HttpClientInterface :: class );
$response = \Mockery :: mock ( ResponseInterface :: class );
/** @noinspection PhpParamsInspection */
$httpClient -> allows () -> request ( \Mockery :: any (), \Mockery :: any (), \Mockery :: any ()) -> andReturn ( $response );
$response -> allows ([
" getStatusCode " => 404 ,
" getHeaders " => [],
" getContent " => " "
]);
2023-02-05 00:55:11 +00:00
$this -> app -> instance ( " HttpClient " , $httpClient );
2023-02-02 23:37:37 +00:00
$this -> app -> instance ( " JikanParser " , new MalClient ( $httpClient ));
}
2018-04-21 12:59:48 +05:00
}