Merge pull request #485 from jikan-me/automated-tests

 Added gh action to run tests and fixed tests
This commit is contained in:
pushrbx 2024-01-29 14:12:16 +00:00 committed by GitHub
commit 194c745eb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 165 additions and 36 deletions

122
.github/workflows/tests.yml vendored Normal file
View File

@ -0,0 +1,122 @@
name: "Tests"
on:
workflow_dispatch: {}
push:
branches:
- master
pull_request: {}
jobs:
tests:
runs-on: ubuntu-latest
name: PHP v8.1 with MongoDB 6.0
concurrency: "ci-${{ github.ref }}"
env:
extensions: curl,intl,mbstring,mongodb-stable,redis,opcache,sockets,pcntl,xdebug
key: jikan-rest-ci-cache-v1
phpversion: 8.1
services:
mongodb:
image: mongo:6
ports:
- 27017:27017
env:
MONGO_INITDB_DATABASE: jikan_test
typesense:
image: typesense/typesense:0.24.1
ports:
- 8108:8108
volumes:
- typesense-data:/data
env:
TYPESENSE_API_KEY: jikan_testing
TYPESENSE_DATA_DIR: /data
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Show MongoDB server status
run: |
docker run --rm --network host mongo:6 mongosh localhost:27017 --eval "db.runCommand({ serverStatus: 1 })"
- name: Show Typesense server status
run: curl --connect-timeout 5 --max-time 10 --retry 5 --retry-delay 1 --retry-max-time 5 -s -f http://localhost:8108/health
- name: Setup extension cache
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ env.phpversion }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}
- name: Cache extensions
uses: actions/cache@v4
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.phpversion }}
extensions: ${{ env.extensions }}
coverage: xdebug
tools: composer
- name: Setup dependency cache
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}"
restore-keys: "${{ runner.os }}-composer-"
- name: Install dependencies
run: composer install --no-interaction --no-progress --no-suggest --no-scripts
- name: Run tests
env:
CI_ENV: true
APP_ENV: testing
APP_DEBUG: "true"
APP_KEY: "base64:em9ycm8="
APP_TIMEZONE: UTC
APP_URL: http://localhost:8080
LOG_CHANNEL: stack
LOG_LEVEL: debug
DB_HOST: localhost
DB_PORT: 27017
MAX_RESULTS_PER_PAGE: 25
TYPESENSE_API_KEY: jikan_testing
TYPESENSE_HOST: localhost
TYPESENSE_PORT: 8108
SCOUT_DRIVER: typesense
SCOUT_QUEUE: "false"
MICROCACHING: "false"
CACHING: "false"
CACHE_DRIVER: array
CACHE_DEFAULT_EXPIRE: 86400
CACHE_META_EXPIRE: 300
CACHE_USER_EXPIRE: 300
CACHE_USERLIST_EXPIRE: 3600
CACHE_404_EXPIRE: 604800
CACHE_SEARCH_EXPIRE: 432000
CACHE_PRODUCERS_EXPIRE: 432000
CACHE_MAGAZINES_EXPIRE: 432000
CACHE_MICROCACHE_EXPIRE: 60
GITHUB_REPORTING: "false"
REPORTING: "false"
run: ./vendor/bin/phpunit --coverage-clover coverage.xml
- name: Upload coverage reports
uses: codecov/codecov-action@v3
with:
files: coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}

View File

@ -1,6 +1,6 @@
<?php
$db_username = env('DB_USERNAME', 'admin');
$db_username = env('DB_USERNAME', env("APP_ENV") === "testing" ? "" : "admin");
$dsn = "mongodb://";
if (empty($db_username)) {
$dsn .= env('DB_HOST', 'localhost').":".env('DB_PORT', 27017)."/".env('DB_ADMIN', 'admin');

View File

@ -215,14 +215,13 @@ class AnimeSearchEndpointTest extends TestCase
/**
* @dataProvider emptyDateRangeProvider
*/
public function testSearchByEmptyDatesShouldRaiseValidationError($params)
public function testSearchByEmptyDatesShouldNotRaiseValidationError($params)
{
$this->generateFiveSpecificAndTenRandomElementsInDb($params);
$content = $this->getJsonResponse($params);
$this->getJsonResponse($params);
$this->seeStatusCode(400);
$this->assertEquals("ValidationException", data_get($content, "type"));
$this->seeStatusCode(200);
}
/**

View File

@ -196,14 +196,13 @@ class MangaSearchEndpointTest extends TestCase
/**
* @dataProvider emptyDateRangeProvider
*/
public function testSearchByEmptyDatesShouldRaiseValidationError($params)
public function testSearchByEmptyDatesShouldNotRaiseValidationError($params)
{
$this->generateFiveSpecificAndTenRandomElementsInDb($params);
$content = $this->getJsonResponse($params);
$this->getJsonResponse($params);
$this->seeStatusCode(400);
$this->assertEquals("ValidationException", data_get($content, "type"));
$this->seeStatusCode(200);
}
/**

View File

@ -29,17 +29,17 @@ class TopAnimeEndpointTest extends TestCase
public function orderByFieldAndParamsData()
{
return [
["rank", false, []],
["rank", false, ["filter" => "airing"]],
["rank", false, ["type" => "tv"]],
["rank", false, ["type" => "movie"]],
["rank", false, ["type" => "ova"]],
["rank", false, ["type" => "ona"]],
["rank", false, ["type" => "special"]],
["rank", false, ["type" => "music"]],
["rank", false, ["filter" => "upcoming"]],
["members", true, ["filter" => "bypopularity"]],
["favorites", true, ["filter" => "favorite"]]
"empty query string" => ["score", true, []],
"query string: ?filter=airing" => ["score", true, ["filter" => "airing"]],
"query string: ?type=tv" => ["score", true, ["type" => "tv"]],
"query string: ?type=movie" => ["score", true, ["type" => "movie"]],
"query string: ?type=ova" => ["score", true, ["type" => "ova"]],
"query string: ?type=ona" => ["score", true, ["type" => "ona"]],
"query string: ?type=special" => ["score", true, ["type" => "special"]],
"query string: ?type=music" => ["score", true, ["type" => "music"]],
"query string: ?filter=upcoming" => ["members", true, ["filter" => "upcoming"]],
"query string: ?filter=bypopularity" => ["members", true, ["filter" => "bypopularity"]],
"query string: ?filter=favorite" => ["favorites", true, ["filter" => "favorite"]]
];
}
@ -57,10 +57,10 @@ class TopAnimeEndpointTest extends TestCase
/*
* Test whether the API orders the items correctly. It has to return items in similar order as MAL would
* their search results.
* No filters / query string parameters -> sorted by rank
* filter = airing -> sorted by rank
* type = tv/movie/ova/ona/special -> sorted by rank
* filter = upcoming -> sorted by "members" attribute
* No filters / query string parameters -> sorted by score
* filter = airing -> sorted by score
* type = tv/movie/ova/ona/special -> sorted by score
* filter = upcoming -> sorted by "popularity" attribute
* filter = favorites -> sorted by "favorites" attribute
* filter = bypopular -> sorted by "members" attribute
*/

View File

@ -13,6 +13,9 @@ use Jikan\Model\User\LastUpdates;
use Jikan\Model\User\Profile as JikanProfile;
use Jikan\Model\User\Reviews\UserReviews;
use Jikan\MyAnimeList\MalClient;
use Jikan\Parser\User\Profile\FavoritesParser;
use Jikan\Parser\User\Profile\LastUpdatesParser;
use Jikan\Parser\User\Profile\MangaStatsParser;
use Jikan\Parser\User\Profile\UserProfileParser;
use Jikan\Request\User\UserRecommendationsRequest;
use Tests\TestCase;
@ -103,8 +106,8 @@ class UserControllerTest extends TestCase
$userProfileParser->allows()
->getJoinDate()
->andReturn(\DateTimeImmutable::createFromFormat(\DateTimeImmutable::RFC3339, "2000-10-01T00:00:00+00:00"));
$animeStats = \Mockery::mock(AnimeStats::class)->makePartial();
$animeStats->allows([
$animeStatsParser = \Mockery::mock(\Jikan\Parser\User\Profile\AnimeStatsParser::class)->makePartial();
$animeStatsParser->allows([
"getDaysWatched" => 0,
"getMeanScore" => 0,
"getWatching" => 0,
@ -116,11 +119,12 @@ class UserControllerTest extends TestCase
"getRewatched" => 0,
"getEpisodesWatched" => 0
]);
$animeStats = AnimeStats::fromParser($animeStatsParser);
$userProfileParser->allows()
->getAnimeStats()
->andReturn($animeStats);
$mangaStats = \Mockery::mock(MangaStats::class)->makePartial();
$mangaStats->allows([
$mangaStatsParser = \Mockery::mock(MangaStatsParser::class)->makePartial();
$mangaStatsParser->allows([
"getDaysRead" => 0,
"getMeanScore" => 0,
"getReading" => 0,
@ -133,16 +137,18 @@ class UserControllerTest extends TestCase
"getChaptersRead" => 0,
"getVolumesRead" => 0
]);
$mangaStats = MangaStats::fromParser($mangaStatsParser);
$userProfileParser->allows()
->getMangaStats()
->andReturn($mangaStats);
$favorites = \Mockery::mock(\Jikan\Model\User\Favorites::class)->makePartial();
$favorites->allows([
$favoritesParser = \Mockery::mock(FavoritesParser::class)->makePartial();
$favoritesParser->allows([
"getAnime" => [],
"getManga" => [],
"getCharacters" => [],
"getPeople" => []
]);
$favorites = \Jikan\Model\User\Favorites::fromParser($favoritesParser);
$userProfileParser->allows()
->getFavorites()
->andReturn($favorites);
@ -155,22 +161,25 @@ class UserControllerTest extends TestCase
$userProfileParser->allows()
->getAbout()
->andReturn(null);
$lastUpdates = \Mockery::mock(LastUpdates::class)->makePartial();
$lastUpdates->allows()
->getAnime()
$lastUpdatesParser = \Mockery::mock(LastUpdatesParser::class)->makePartial();
$lastUpdatesParser->allows()
->getLastAnimeUpdates()
->andReturn([]);
$lastUpdates->allows()
->getManga()
$lastUpdatesParser->allows()
->getLastMangaUpdates()
->andReturn([]);
$lastUpdates = LastUpdates::fromParser($lastUpdatesParser);
$userProfileParser->allows()
->getUserLastUpdates()
->andReturn($lastUpdates);
$resultData = JikanProfile::fromParser($userProfileParser);
/** @noinspection PhpParamsInspection */
$jikanParser->allows()
->getUserProfile(\Mockery::any())
->andReturn(JikanProfile::fromParser($userProfileParser));
->andReturn($resultData);
$this->app->instance('JikanParser', $jikanParser);