From 5c4fe0ef641917dbc008f6a2bf5ae072cb2c2023 Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Mon, 19 Oct 2026 14:00:09 +0200 Subject: [PATCH] prefix wrapper methods that send a request with fetch --- README.md | 27 ++-- src/Client/Category.php | 8 +- src/Client/File.php | 15 ++- src/Client/Game.php | 27 ++-- src/Client/GameVersionType.php | 10 +- src/Client/Mod.php | 29 +++-- tests/Integration/Client/ClientTest.php | 38 +++--- tests/Unit/Client/CategoryTest.php | 68 +++++++++++ tests/Unit/Client/FileTest.php | 68 +++++++++++ tests/Unit/Client/GameTest.php | 105 ++++++++++++++++ tests/Unit/Client/GameVersionTypeTest.php | 42 +++++++ tests/Unit/Client/ModTest.php | 142 ++++++++++++++++++++++ 12 files changed, 515 insertions(+), 64 deletions(-) create mode 100644 tests/Unit/Client/CategoryTest.php create mode 100644 tests/Unit/Client/FileTest.php create mode 100644 tests/Unit/Client/GameTest.php create mode 100644 tests/Unit/Client/GameVersionTypeTest.php create mode 100644 tests/Unit/Client/ModTest.php diff --git a/README.md b/README.md index 43d8fdc..ce0582d 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,11 @@ $client->setUserAgent("my-app/1.0.0 (contact@example.com)"); **You need an API key to use this client.** If you're a third party you can read more about how to get one [here](https://support.curseforge.com/en/support/solutions/articles/9000208346). +## Wrappers +Most methods return wrappers like `Game`, `Mod` or `File`. They extend the generated models in `src/Model`, +so all getters of the models can be used directly, e.g. `$mod->getName()`. +Methods of the wrappers that send a request to the API start with `fetch`, e.g. `$mod->fetchFiles()`. + ## Paginated Lists Most methods return a paginated list containing a list of results on the current page and methods to navigate to the next and previous page. The paginated list implements `Iterator`, `ArrayAccess` and `Countable` so @@ -60,16 +65,16 @@ A game contains various different version types, you can fetch them like this: ```php $versionTypes = $client->getGameVersionTypes(432); // or -$versionTypes = $game->getVersionTypes(); +$versionTypes = $game->fetchVersionTypes(); ``` Since this endpoint isn't paginated, it returns an array instead of a paginated list. You can also fetch the versions of a specific version type: ```php -$versions = $client->getGameVersionTypeVersions(432, 68441); +$versions = $client->getGameVersionsByType(432, 68441); // or $versionType = $versionTypes[0]; -$versions = $versionType->getVersions(); +$versions = $versionType->fetchVersions(); ``` For example: 68441 is the mod loader selection for Minecraft. @@ -77,7 +82,7 @@ You can also get all versions for all version types of a game: ```php $versions = $client->getGameVersions(432); // or -$versions = $game->getVersions(); +$versions = $game->fetchVersions(); ``` ### Game Categories @@ -87,7 +92,7 @@ You can fetch them like this: ```php $categoryClasses = $client->getCategories(432); // or -$categoryClasses = $game->getCategories(); +$categoryClasses = $game->fetchCategories(); ``` Note that this will return both, category classes AND categories. @@ -95,14 +100,14 @@ If you want to get only the category classes you can set the parameter `onlyClas ```php $categoryClasses = $client->getCategories(432, null, true); // or -$categoryClasses = $game->getCategories(null, true); +$categoryClasses = $game->fetchCategories(null, true); ``` You can fetch categories in a class using the parameter `classId`: ```php $categories = $client->getCategories(432, 6); // or -$categories = $game->getCategories(6); +$categories = $game->fetchCategories(6); ``` ## Mods @@ -118,7 +123,7 @@ You can also get a list of featured mods for a game: ```php $mods = $client->getFeaturedMods(432); // or -$mods = $game->getFeaturedMods(); +$mods = $game->fetchFeaturedMods(); $featured = $mods->getFeatured(); $popular = $mods->getPopular(); @@ -136,13 +141,13 @@ $mods = $client->getMods([420561, 278568, 715033]); You can also fetch the HTML for mod descriptions: ```php -$description = $mod->getDescription(); +$description = $mod->fetchDescription(); ``` ### Mod Files -To fetch mod files use the method `getFiles()`: +To fetch mod files use the method `fetchFiles()`: ```php -$files = $mod->getFiles(); +$files = $mod->fetchFiles(); ``` It optionally takes a ModFilesOptions object as a parameter: ```php diff --git a/src/Client/Category.php b/src/Client/Category.php index 22c852c..860306f 100644 --- a/src/Client/Category.php +++ b/src/Client/Category.php @@ -19,11 +19,12 @@ public function __construct( /** * Search mods in this category + * This sends a request to the CurseForge API every time it is called. * @param ModSearchOptions|null $options * @return PaginatedModList * @throws ApiException */ - public function getMods(?ModSearchOptions $options = null): PaginatedModList + public function fetchMods(?ModSearchOptions $options = null): PaginatedModList { if ($options === null) { $options = new ModSearchOptions($this->getGameId()); @@ -39,11 +40,12 @@ public function getMods(?ModSearchOptions $options = null): PaginatedModList } /** - * Get the game of this category + * Fetch the game of this category + * This sends a request to the CurseForge API every time it is called. * @return Game * @throws ApiException */ - public function getGame(): Game + public function fetchGame(): Game { return $this->client->getGame($this->getGameId()); } diff --git a/src/Client/File.php b/src/Client/File.php index 63a4fca..8462a3d 100644 --- a/src/Client/File.php +++ b/src/Client/File.php @@ -16,11 +16,12 @@ public function __construct( } /** - * Get the server pack file for this version (if it exists) + * Fetch the server pack file for this version (if it exists) + * This sends a request to the CurseForge API every time it is called. * @return File|null * @throws ApiException */ - public function getServerPack(): ?File + public function fetchServerPack(): ?File { if (!$this->getServerPackFileId()) { return null; @@ -30,24 +31,26 @@ public function getServerPack(): ?File } /** - * Get the changelog of this file + * Fetch the changelog of this file + * This sends a request to the CurseForge API every time it is called. * @param bool|null $raw Get raw changelog without things like external link redirects * @param bool|null $stripped Get the changelog with all HTML tags removed * @param bool|null $markup * @return string * @throws ApiException */ - public function getChangelog(?bool $raw = null, ?bool $stripped = null, ?bool $markup = null): string + public function fetchChangelog(?bool $raw = null, ?bool $stripped = null, ?bool $markup = null): string { return $this->client->getModFileChangelog($this->getModId(), $this->getId(), $raw, $stripped, $markup); } /** - * Get the mod of this file + * Fetch the mod of this file + * This sends a request to the CurseForge API every time it is called. * @return Mod * @throws ApiException */ - public function getMod(): Mod + public function fetchMod(): Mod { return $this->client->getMod($this->getModId()); } diff --git a/src/Client/Game.php b/src/Client/Game.php index a6f36a4..66b2806 100644 --- a/src/Client/Game.php +++ b/src/Client/Game.php @@ -24,49 +24,55 @@ public function __construct( } /** - * Get all version types for this game + * Fetch all version types for this game + * This sends a request to the CurseForge API every time it is called. * @return GameVersionType[] * @throws ApiException */ - public function getVersionTypes(): array + public function fetchVersionTypes(): array { return $this->client->getGameVersionTypes($this->getId()); } /** - * Get all versions for each version type of this game + * Fetch all versions for each version type of this game + * This sends a request to the CurseForge API every time it is called. * @return GameVersionsByTypeV2[] * @throws ApiException */ - public function getVersions(): array + public function fetchVersions(): array { return $this->client->getGameVersions($this->getId()); } /** - * Get all versions of a version type of this game + * Fetch all versions of a version type of this game + * This sends a request to the CurseForge API every time it is called. * @param int $versionTypeId * @return GameVersionsByTypeV2[] * @throws ApiException */ - public function getGameVersionsByType(int $versionTypeId): array + public function fetchGameVersionsByType(int $versionTypeId): array { return $this->client->getGameVersionsByType($this->getId(), $versionTypeId); } /** + * Fetch the categories of this game + * This sends a request to the CurseForge API every time it is called. * @param int|null $classId The ID of the category class you want to query categories for (e.g. 5 for plugins or 6 for mods) * @param bool|null $classesOnly only return category classes (e.g. plugins or mods). * @return array * @throws ApiException */ - public function getCategories(?int $classId = null, ?bool $classesOnly = null): array + public function fetchCategories(?int $classId = null, ?bool $classesOnly = null): array { return $this->client->getCategories($this->getId(), $classId, $classesOnly); } /** - * Search for mods + * Search for mods in this game + * This sends a request to the CurseForge API every time it is called. * @param ModSearchOptions|null $options * @return PaginatedModList * @throws ApiException @@ -77,14 +83,15 @@ public function searchMods(?ModSearchOptions $options = null): PaginatedModList } /** - * Get all featured mods for this game + * Fetch all featured mods for this game + * This sends a request to the CurseForge API every time it is called. * @param int[] $excludedModIds * @param int|null $gameVersionTypeId * @param bool|null $clientCompatible * @return FeaturedMods * @throws ApiException */ - public function getFeaturedMods(array $excludedModIds = [], ?int $gameVersionTypeId = null, ?bool $clientCompatible = null): FeaturedMods + public function fetchFeaturedMods(array $excludedModIds = [], ?int $gameVersionTypeId = null, ?bool $clientCompatible = null): FeaturedMods { return $this->client->getFeaturedMods($this->getId(), $excludedModIds, $gameVersionTypeId, $clientCompatible); } diff --git a/src/Client/GameVersionType.php b/src/Client/GameVersionType.php index e6da35d..cce463a 100644 --- a/src/Client/GameVersionType.php +++ b/src/Client/GameVersionType.php @@ -17,21 +17,23 @@ public function __construct( } /** - * Get all versions of this type + * Fetch all versions of this type + * This sends a request to the CurseForge API every time it is called. * @return GameVersionsByTypeV2[] * @throws ApiException */ - public function getVersions(): array + public function fetchVersions(): array { return $this->client->getGameVersionsByType($this->getGameId(), $this->getId()); } /** - * Get the game of this type + * Fetch the game of this type + * This sends a request to the CurseForge API every time it is called. * @return Game * @throws ApiException */ - public function getGame(): Game + public function fetchGame(): Game { return $this->client->getGame($this->getGameId()); } diff --git a/src/Client/Mod.php b/src/Client/Mod.php index c4923ea..5234f8e 100644 --- a/src/Client/Mod.php +++ b/src/Client/Mod.php @@ -29,32 +29,35 @@ public static function fromModels(CurseForgeAPIClient $client, array $models): a /** * Fetch the description of this mod as html + * This sends a request to the CurseForge API every time it is called. * @param string|null $lang * @return string|null * @throws ApiException */ - public function getDescription(?string $lang = null): ?string + public function fetchDescription(?string $lang = null): ?string { return $this->client->getModDescription($this->getId(), $lang); } /** - * Get the files of this mod + * Fetch the files of this mod + * This sends a request to the CurseForge API every time it is called. * @param ModFilesOptions|null $options * @return PaginatedFilesList * @throws ApiException */ - public function getFiles(?ModFilesOptions $options = null): PaginatedFilesList + public function fetchFiles(?ModFilesOptions $options = null): PaginatedFilesList { return $this->client->getModFiles($options ?? new ModFilesOptions($this->getId())); } /** - * Get game this mod belongs to + * Fetch the game this mod belongs to + * This sends a request to the CurseForge API every time it is called. * @return Game * @throws ApiException */ - public function getGame(): Game + public function fetchGame(): Game { return $this->client->getGame($this->getGameId()); } @@ -74,31 +77,35 @@ public function getPrimaryCategory(): ?Category } /** - * Get the category class of this mod + * Fetch the category class of this mod + * This sends a request to the CurseForge API every time it is called. * @return Category * @throws ApiException */ - public function getCategoryClass(): Category + public function fetchCategoryClass(): Category { return $this->client->getCategory($this->getGameId(), $this->getClassId()); } /** - * Get the main file of this mod + * Fetch the main file of this mod + * This sends a request to the CurseForge API every time it is called. * @return File * @throws ApiException */ - public function getMainFile(): File + public function fetchMainFile(): File { - return $this->getFile($this->getMainFileId()); + return $this->fetchFile($this->getMainFileId()); } /** + * Fetch a file of this mod + * This sends a request to the CurseForge API every time it is called. * @param int $fileId * @return File * @throws ApiException */ - public function getFile(int $fileId): File + public function fetchFile(int $fileId): File { return $this->client->getModFile($this->getId(), $fileId); } diff --git a/tests/Integration/Client/ClientTest.php b/tests/Integration/Client/ClientTest.php index 3cf8c2e..61dd75b 100644 --- a/tests/Integration/Client/ClientTest.php +++ b/tests/Integration/Client/ClientTest.php @@ -100,10 +100,10 @@ public function testGetGame() $this->assertEquals(static::MINECRAFT_GAME_ID, $game->getId()); $this->assertEquals("Minecraft", $game->getName()); - $versions = $game->getVersions(); + $versions = $game->fetchVersions(); $this->assertNotEmpty($versions); - $categories = $game->getCategories(); + $categories = $game->fetchCategories(); $this->assertNotEmpty($categories); } @@ -128,12 +128,12 @@ public function testGetGameVersionTypes() $game = $this->apiClient->getGame(static::MINECRAFT_GAME_ID); $this->assertEquals(static::MINECRAFT_GAME_ID, $game->getId()); $this->assertEquals("Minecraft", $game->getName()); - $types = $game->getVersionTypes(); + $types = $game->fetchVersionTypes(); $this->assertNotEmpty($types); foreach ($types as $type) { - $this->assertEquals($game, $type->getGame()); - $gameVersions = $game->getGameVersionsByType($type->getId()); - $this->assertEquals($gameVersions, $type->getVersions()); + $this->assertEquals($game, $type->fetchGame()); + $gameVersions = $game->fetchGameVersionsByType($type->getId()); + $this->assertEquals($gameVersions, $type->fetchVersions()); } } @@ -149,9 +149,9 @@ public function testGetCategories() for ($i = 0; $i < 3; $i++) { $category = $categories[$i]; - $this->assertEquals(static::MINECRAFT_GAME_ID, $category->getGame()->getId()); + $this->assertEquals(static::MINECRAFT_GAME_ID, $category->fetchGame()->getId()); - $mods = $category->getMods(); + $mods = $category->fetchMods(); $this->assertNotEmpty($mods); foreach ($mods->getResults() as $mod) { if ($category->getIsClass()) { @@ -179,9 +179,9 @@ public function testGetCategoriesOnlyClasses() for ($i = 0; $i < 3; $i++) { $category = $categories[$i]; - $this->assertEquals(static::MINECRAFT_GAME_ID, $category->getGame()->getId()); + $this->assertEquals(static::MINECRAFT_GAME_ID, $category->fetchGame()->getId()); - $mods = $category->getMods(); + $mods = $category->fetchMods(); $this->assertNotEmpty($mods); foreach ($mods->getResults() as $mod) { if ($category->getIsClass()) { @@ -308,9 +308,9 @@ public function testGetMod() $mod = $this->apiClient->getMod(static::MCLOGS_MOD_ID); $this->assertEquals(static::MCLOGS_MOD_ID, $mod->getId()); - $this->assertEquals(static::MINECRAFT_GAME_ID, $mod->getGame()->getId()); + $this->assertEquals(static::MINECRAFT_GAME_ID, $mod->fetchGame()->getId()); $this->assertEquals(static::MCLOGS_PRIMARY_CATEGORY_ID, $mod->getPrimaryCategory()->getId()); - $this->assertEquals(static::MODS_CATEGORY_ID, $mod->getCategoryClass()->getId()); + $this->assertEquals(static::MODS_CATEGORY_ID, $mod->fetchCategoryClass()->getId()); } /** @@ -336,7 +336,7 @@ public function testGetMods() public function testGetFeaturedMods() { $game = $this->apiClient->getGame(static::MINECRAFT_GAME_ID); - $featured = $game->getFeaturedMods(); + $featured = $game->fetchFeaturedMods(); $this->assertNotEmpty($featured->getFeatured()); $this->assertNotEmpty($featured->getRecentlyUpdated()); @@ -351,7 +351,7 @@ public function testGetFeaturedMods() public function testGetModDescription() { $mod = $this->apiClient->getMod(static::MCLOGS_MOD_ID); - $description = $mod->getDescription(); + $description = $mod->fetchDescription(); $this->assertNotNull($description); $this->assertNotEmpty($description); } @@ -365,18 +365,18 @@ public function testGetModFile() { $mod = $this->apiClient->getMod(static::MCLOGS_MOD_ID); - $file = $mod->getMainFile(); + $file = $mod->fetchMainFile(); - $this->assertEquals($mod, $file->getMod()); + $this->assertEquals($mod, $file->fetchMod()); $this->assertEquals($mod->getMainFileId(), $file->getId()); - $this->assertNotNull($file->getChangelog()); + $this->assertNotNull($file->fetchChangelog()); $this->assertTrue($mod->getAllowModDistribution()); $this->assertNotEmpty($this->apiClient->getModFileDownloadURL($mod->getId(), $file->getId())); - $this->assertNull($file->getServerPack()); + $this->assertNull($file->fetchServerPack()); } /** @@ -388,7 +388,7 @@ public function testGetModFiles() { $mod = $this->apiClient->getMod(static::MCLOGS_MOD_ID); - $fileIds = array_map(fn($file) => $file->getId(), $mod->getFiles()->getResults()); + $fileIds = array_map(fn($file) => $file->getId(), $mod->fetchFiles()->getResults()); $this->assertNotEmpty($fileIds); $files = $this->apiClient->getFiles($fileIds); $this->assertSameSize($fileIds, $files); diff --git a/tests/Unit/Client/CategoryTest.php b/tests/Unit/Client/CategoryTest.php new file mode 100644 index 0000000..4c07bae --- /dev/null +++ b/tests/Unit/Client/CategoryTest.php @@ -0,0 +1,68 @@ +createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/searchMods.json"))]); + $category = new Category($client, new CategoryModel(["id" => 435, "game_id" => 432, "class_id" => 6])); + + $mods = $category->fetchMods(); + + $this->assertGetRequest("/v1/mods/search", ["gameId" => "432", "categoryIds" => "[435]", "index" => "0", "pageSize" => "50"]); + $this->assertCount(2, $mods); + } + + /** + * @throws ApiException + */ + public function testFetchModsOfCategoryClass(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/searchMods.json"))]); + $category = new Category($client, new CategoryModel(["id" => 6, "game_id" => 432, "is_class" => true])); + + $category->fetchMods(); + + $this->assertGetRequest("/v1/mods/search", ["gameId" => "432", "classId" => "6", "index" => "0", "pageSize" => "50"]); + } + + /** + * @throws ApiException + */ + public function testFetchModsWithOptions(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/searchMods.json"))]); + $category = new Category($client, new CategoryModel(["id" => 435, "game_id" => 432, "class_id" => 6])); + + $category->fetchMods((new ModSearchOptions(432))->setSearchFilter("mclogs")); + + $this->assertGetRequest("/v1/mods/search", ["gameId" => "432", "categoryIds" => "[435]", "searchFilter" => "mclogs", "index" => "0", "pageSize" => "50"]); + } + + /** + * @throws ApiException + */ + public function testFetchGame(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getGame.json"))]); + $category = new Category($client, new CategoryModel(["id" => 435, "game_id" => 432, "class_id" => 6])); + + $game = $category->fetchGame(); + + $this->assertGetRequest("/v1/games/432"); + $this->assertInstanceOf(Game::class, $game); + $this->assertSame(432, $game->getId()); + } +} diff --git a/tests/Unit/Client/FileTest.php b/tests/Unit/Client/FileTest.php new file mode 100644 index 0000000..34ebd3f --- /dev/null +++ b/tests/Unit/Client/FileTest.php @@ -0,0 +1,68 @@ +createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModFile.json"))]); + $file = new File($client, new FileModel(["id" => 8946028, "mod_id" => 420561, "server_pack_file_id" => 8946029])); + + $serverPack = $file->fetchServerPack(); + + $this->assertGetRequest("/v1/mods/420561/files/8946029"); + $this->assertInstanceOf(File::class, $serverPack); + $this->assertSame(8946029, $serverPack->getId()); + } + + /** + * @throws ApiException + */ + public function testFetchServerPackWithoutServerPackReturnsNull(): void + { + $client = $this->createClient(); + $file = new File($client, new FileModel(["id" => 8946029, "mod_id" => 420561])); + + $this->assertNull($file->fetchServerPack()); + $this->assertRequestCount(0); + } + + /** + * @throws ApiException + */ + public function testFetchChangelog(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModFileChangelog.json"))]); + $file = new File($client, new FileModel(["id" => 8946029, "mod_id" => 420561])); + + $changelog = $file->fetchChangelog(true, false, true); + + $this->assertGetRequest("/v1/mods/420561/files/8946029/changelog", ["raw" => "true", "stripped" => "false", "markup" => "true"]); + $this->assertIsString($changelog); + } + + /** + * @throws ApiException + */ + public function testFetchMod(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getMod.json"))]); + $file = new File($client, new FileModel(["id" => 8946029, "mod_id" => 420561])); + + $mod = $file->fetchMod(); + + $this->assertGetRequest("/v1/mods/420561"); + $this->assertInstanceOf(Mod::class, $mod); + $this->assertSame(420561, $mod->getId()); + } +} diff --git a/tests/Unit/Client/GameTest.php b/tests/Unit/Client/GameTest.php new file mode 100644 index 0000000..957592c --- /dev/null +++ b/tests/Unit/Client/GameTest.php @@ -0,0 +1,105 @@ +createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionTypes.json"))]); + $game = new Game($client, new GameModel(["id" => 432])); + + $versionTypes = $game->fetchVersionTypes(); + + $this->assertGetRequest("/v1/games/432/version-types"); + $this->assertCount(3, $versionTypes); + $this->assertContainsOnlyInstancesOf(GameVersionType::class, $versionTypes); + } + + /** + * @throws ApiException + */ + public function testFetchVersions(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionsV2.json"))]); + $game = new Game($client, new GameModel(["id" => 432])); + + $versions = $game->fetchVersions(); + + $this->assertGetRequest("/v2/games/432/versions"); + $this->assertContainsOnlyInstancesOf(GameVersionsByTypeV2::class, $versions); + } + + /** + * @throws ApiException + */ + public function testFetchGameVersionsByType(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionsV2.json"))]); + $game = new Game($client, new GameModel(["id" => 432])); + + $versions = $game->fetchGameVersionsByType(11); + + $this->assertGetRequest("/v2/games/432/versions"); + $this->assertSame(["1.5.3", "1.5.2", "1.5.1"], array_map(fn(GameVersion $version) => $version->getName(), $versions)); + } + + /** + * @throws ApiException + */ + public function testFetchCategories(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getCategories.json"))]); + $game = new Game($client, new GameModel(["id" => 432])); + + $categories = $game->fetchCategories(6, true); + + $this->assertGetRequest("/v1/categories", ["gameId" => "432", "classId" => "6", "classesOnly" => "true"]); + $this->assertContainsOnlyInstancesOf(Category::class, $categories); + } + + /** + * @throws ApiException + */ + public function testSearchMods(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/searchMods.json"))]); + $game = new Game($client, new GameModel(["id" => 432])); + + $mods = $game->searchMods(); + + $this->assertGetRequest("/v1/mods/search", ["gameId" => "432", "index" => "0", "pageSize" => "50"]); + $this->assertCount(2, $mods); + } + + /** + * @throws ApiException + */ + public function testFetchFeaturedMods(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getFeaturedMods.json"))]); + $game = new Game($client, new GameModel(["id" => 432])); + + $featuredMods = $game->fetchFeaturedMods([420561], 68441, true); + + $this->assertPostRequest("/v1/mods/featured", [ + "gameId" => 432, + "excludedModIds" => [420561], + "gameVersionTypeId" => 68441, + "clientCompatible" => true, + ]); + $this->assertCount(2, $featuredMods->getFeatured()); + } +} diff --git a/tests/Unit/Client/GameVersionTypeTest.php b/tests/Unit/Client/GameVersionTypeTest.php new file mode 100644 index 0000000..7d279af --- /dev/null +++ b/tests/Unit/Client/GameVersionTypeTest.php @@ -0,0 +1,42 @@ +createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionsV2.json"))]); + $versionType = new GameVersionType($client, new GameVersionTypeModel(["id" => 11, "game_id" => 432])); + + $versions = $versionType->fetchVersions(); + + $this->assertGetRequest("/v2/games/432/versions"); + $this->assertSame(["1.5.3", "1.5.2", "1.5.1"], array_map(fn(GameVersion $version) => $version->getName(), $versions)); + } + + /** + * @throws ApiException + */ + public function testFetchGame(): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getGame.json"))]); + $versionType = new GameVersionType($client, new GameVersionTypeModel(["id" => 11, "game_id" => 432])); + + $game = $versionType->fetchGame(); + + $this->assertGetRequest("/v1/games/432"); + $this->assertInstanceOf(Game::class, $game); + $this->assertSame(432, $game->getId()); + } +} diff --git a/tests/Unit/Client/ModTest.php b/tests/Unit/Client/ModTest.php new file mode 100644 index 0000000..7c5d7c8 --- /dev/null +++ b/tests/Unit/Client/ModTest.php @@ -0,0 +1,142 @@ +createClient($responses), new ModModel([ + "id" => 420561, + "game_id" => 432, + "class_id" => 6, + "main_file_id" => 8946029, + ])); + } + + /** + * @throws ApiException + */ + public function testFetchDescription(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModDescription.json"))]); + + $description = $mod->fetchDescription("de"); + + $this->assertGetRequest("/v1/mods/420561/description", ["lang" => "de"]); + $this->assertStringContainsString("mclo.gs", $description); + } + + /** + * @throws ApiException + */ + public function testFetchFiles(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModFiles.json"))]); + + $files = $mod->fetchFiles(); + + $this->assertGetRequest("/v1/mods/420561/files", ["index" => "0", "pageSize" => "50"]); + $this->assertContainsOnlyInstancesOf(File::class, $files->getResults()); + } + + /** + * @throws ApiException + */ + public function testFetchFilesWithOptions(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModFiles.json"))]); + + $mod->fetchFiles((new ModFilesOptions(420561))->setGameVersion("1.21.1")); + + $this->assertGetRequest("/v1/mods/420561/files", ["gameVersion" => "1.21.1", "index" => "0", "pageSize" => "50"]); + } + + /** + * @throws ApiException + */ + public function testFetchGame(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getGame.json"))]); + + $game = $mod->fetchGame(); + + $this->assertGetRequest("/v1/games/432"); + $this->assertInstanceOf(Game::class, $game); + } + + /** + * @throws ApiException + */ + public function testFetchCategoryClass(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getCategories.json"))]); + + $categoryClass = $mod->fetchCategoryClass(); + + $this->assertGetRequest("/v1/categories", ["gameId" => "432"]); + $this->assertInstanceOf(Category::class, $categoryClass); + $this->assertSame(6, $categoryClass->getId()); + } + + /** + * @throws ApiException + */ + public function testFetchMainFile(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModFile.json"))]); + + $file = $mod->fetchMainFile(); + + $this->assertGetRequest("/v1/mods/420561/files/8946029"); + $this->assertInstanceOf(File::class, $file); + $this->assertSame(8946029, $file->getId()); + } + + /** + * @throws ApiException + */ + public function testFetchFile(): void + { + $mod = $this->createMod([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getModFile.json"))]); + + $file = $mod->fetchFile(8946029); + + $this->assertGetRequest("/v1/mods/420561/files/8946029"); + $this->assertSame(420561, $file->getModId()); + } + + public function testGetPrimaryCategoryDoesNotSendRequest(): void + { + $client = $this->createClient(); + $mod = new Mod($client, new ModModel([ + "id" => 420561, + "primary_category_id" => 435, + "categories" => [ + new CategoryModel(["id" => 434]), + new CategoryModel(["id" => 435]), + ], + ])); + + $category = $mod->getPrimaryCategory(); + + $this->assertInstanceOf(Category::class, $category); + $this->assertSame(435, $category->getId()); + $this->assertRequestCount(0); + } +}