Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions tests/Unit/Client/CurseForgeAPIClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace Aternos\CurseForgeApi\Tests\Unit\Client;

use Aternos\CurseForgeApi\ApiException;
use Aternos\CurseForgeApi\Client\Category;
use Aternos\CurseForgeApi\Client\Game;
use Aternos\CurseForgeApi\Client\GameVersionType;
use Aternos\CurseForgeApi\Configuration;
use Aternos\CurseForgeApi\Model\GameVersion;
use Aternos\CurseForgeApi\Model\GameVersionsByTypeV2;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand Down Expand Up @@ -96,4 +101,150 @@ public function testBooleanQueryParametersAreSentAsStrings(): void

$this->assertGetRequest("/v1/categories", ["gameId" => "432", "classesOnly" => "true"]);
}

/**
* @throws ApiException
*/
public function testGetGames(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getGames.json"))]);

$games = $client->getGames();

$this->assertGetRequest("/v1/games", ["index" => "0", "pageSize" => "50"]);
$this->assertCount(2, $games);
$this->assertContainsOnlyInstancesOf(Game::class, $games->getResults());
$this->assertSame("final-fantasy-v", $games[0]->getData()->getSlug());
}

/**
* @throws ApiException
*/
public function testGetGamesWithOffsetAndPageSize(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getGames.json"))]);

$client->getGames(10, 2);

$this->assertGetRequest("/v1/games", ["index" => "10", "pageSize" => "2"]);
}

/**
* @throws ApiException
*/
public function testGetGame(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getGame.json"))]);

$game = $client->getGame(432);

$this->assertGetRequest("/v1/games/432");
$this->assertSame(432, $game->getData()->getId());
$this->assertSame("Minecraft", $game->getData()->getName());
}

/**
* @throws ApiException
*/
public function testGetGameVersionTypes(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionTypes.json"))]);

$versionTypes = $client->getGameVersionTypes(432);

$this->assertGetRequest("/v1/games/432/version-types");
$this->assertCount(3, $versionTypes);
$this->assertContainsOnlyInstancesOf(GameVersionType::class, $versionTypes);
$this->assertSame("Minecraft 1.1", $versionTypes[0]->getData()->getName());
}

/**
* @throws ApiException
*/
public function testGetGameVersions(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionsV2.json"))]);

$versions = $client->getGameVersions(432);

$this->assertGetRequest("/v2/games/432/versions");
$this->assertCount(3, $versions);
$this->assertContainsOnlyInstancesOf(GameVersionsByTypeV2::class, $versions);
$this->assertSame(3, $versions[0]->getType());
}

/**
* @throws ApiException
*/
public function testGetGameVersionsByType(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionsV2.json"))]);

$versions = $client->getGameVersionsByType(432, 11);

$this->assertGetRequest("/v2/games/432/versions");
$this->assertContainsOnlyInstancesOf(GameVersion::class, $versions);
$this->assertSame(["1.5.3", "1.5.2", "1.5.1"], array_map(fn(GameVersion $version) => $version->getName(), $versions));
}

/**
* @throws ApiException
*/
public function testGetGameVersionsByUnknownTypeReturnsEmptyArray(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getVersionsV2.json"))]);

$this->assertSame([], $client->getGameVersionsByType(432, 1));
}

/**
* @throws ApiException
*/
public function testGetCategories(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getCategories.json"))]);

$categories = $client->getCategories(432);

$this->assertGetRequest("/v1/categories", ["gameId" => "432"]);
$this->assertArrayNotHasKey("classId", $this->getQuery());
$this->assertArrayNotHasKey("classesOnly", $this->getQuery());
$this->assertCount(5, $categories);
$this->assertContainsOnlyInstancesOf(Category::class, $categories);
}

/**
* @throws ApiException
*/
public function testGetCategoriesWithClassIdAndClassesOnly(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getCategories.json"))]);

$client->getCategories(432, 6, true);

$this->assertGetRequest("/v1/categories", ["gameId" => "432", "classId" => "6", "classesOnly" => "true"]);
}

/**
* @throws ApiException
*/
public function testGetCategory(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getCategories.json"))]);

$category = $client->getCategory(432, 435);

$this->assertGetRequest("/v1/categories", ["gameId" => "432"]);
$this->assertSame("Server Utility", $category->getData()->getName());
}

/**
* @throws ApiException
*/
public function testGetUnknownCategoryReturnsNull(): void
{
$client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getCategories.json"))]);

$this->assertNull($client->getCategory(432, 1));
}
}
63 changes: 63 additions & 0 deletions tests/Unit/Client/Fixtures/getCategories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"data": [
{
"id": 134,
"gameId": 432,
"name": "Teleportation",
"slug": "teleportation",
"url": "https://www.curseforge.com/minecraft/bukkit-plugins/teleportation",
"iconUrl": "https://media.forgecdn.net/avatars/65/463/636162917927634490.png",
"dateModified": "2016-12-02T22:09:52.763Z",
"classId": 5,
"parentCategoryId": 5,
"displayIndex": 0
},
{
"id": 434,
"gameId": 432,
"name": "Armor, Tools, and Weapons",
"slug": "armor-weapons-tools",
"url": "https://www.curseforge.com/minecraft/mc-mods/armor-weapons-tools",
"iconUrl": "https://media.forgecdn.net/avatars/6/47/635351498790409758.png",
"dateModified": "2014-05-08T17:44:39.057Z",
"classId": 6,
"parentCategoryId": 6,
"displayIndex": 0
},
{
"id": 405,
"gameId": 432,
"name": "Miscellaneous",
"slug": "miscellaneous",
"url": "https://www.curseforge.com/minecraft/texture-packs/miscellaneous",
"iconUrl": "https://media.forgecdn.net/avatars/6/59/635351501101869320.png",
"dateModified": "2020-06-30T17:54:39.157Z",
"classId": 12,
"parentCategoryId": 12,
"displayIndex": 20
},
{
"id": 435,
"gameId": 432,
"name": "Server Utility",
"slug": "server-utility",
"url": "https://www.curseforge.com/minecraft/mc-mods/server-utility",
"iconUrl": "https://media.forgecdn.net/avatars/6/48/635351498950580836.png",
"dateModified": "2014-05-08T17:44:55.057Z",
"classId": 6,
"parentCategoryId": 6,
"displayIndex": 0
},
{
"id": 6,
"gameId": 432,
"name": "Mods",
"slug": "mc-mods",
"url": "https://www.curseforge.com/minecraft/mc-mods",
"iconUrl": "https://media.forgecdn.net/avatars/52/101/636111139584399357.png",
"dateModified": "2026-05-03T12:03:33.26Z",
"isClass": true,
"displayIndex": 5
}
]
}
18 changes: 18 additions & 0 deletions tests/Unit/Client/Fixtures/getGame.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"data": {
"id": 432,
"name": "Minecraft",
"slug": "minecraft",
"dateModified": "2025-05-04T08:40:05.227Z",
"assets": {
"iconUrl": "https://media.forgecdn.net/avatars/1070/13/638605220544214742.webp",
"tileUrl": "https://media.forgecdn.net/game-box-art/432_bc6c42ce-5dd9-496a-a5cd-a51cf3584008.jpg",
"coverUrl": "https://media.forgecdn.net/game-covers/432_5076448d-feda-439b-9d63-ce86b70459ef.webp"
},
"status": 6,
"apiStatus": 2,
"supportedFeatures": {
"supportModSubscriptions": false
}
}
}
42 changes: 42 additions & 0 deletions tests/Unit/Client/Fixtures/getGames.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"data": [
{
"id": 5021,
"name": "Final Fantasy V",
"slug": "final-fantasy-v",
"dateModified": "2022-11-06T20:35:12.237Z",
"assets": {
"iconUrl": null,
"tileUrl": "https://media.forgecdn.net/game-box-art/5021_e7882608-f934-457d-8b8f-1b584a9df1c0.png",
"coverUrl": null
},
"status": 6,
"apiStatus": 2,
"supportedFeatures": {
"supportModSubscriptions": false
}
},
{
"id": 423,
"name": "World of Tanks",
"slug": "worldoftanks",
"dateModified": "2022-11-28T12:15:21.007Z",
"assets": {
"iconUrl": "https://media.forgecdn.net/avatars/77/677/636174916166063366.png",
"tileUrl": "https://media.forgecdn.net/game-tiles/World+of+Tanks+Tile.jpg",
"coverUrl": "https://media.forgecdn.net/game-covers/423_67ef25da-ad0a-4191-9b65-d1420caf4b4e.webp"
},
"status": 6,
"apiStatus": 2,
"supportedFeatures": {
"supportModSubscriptions": false
}
}
],
"pagination": {
"index": 0,
"pageSize": 2,
"resultCount": 2,
"totalCount": 38
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Client/Fixtures/getVersionTypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"data": [
{
"id": 15,
"gameId": 432,
"name": "Minecraft 1.1",
"slug": "minecraft-1-1",
"isSyncable": true,
"status": 1
},
{
"id": 68722,
"gameId": 432,
"name": "Minecraft 1.15",
"slug": "minecraft-1-15",
"isSyncable": true,
"status": 1
},
{
"id": 73242,
"gameId": 432,
"name": "Minecraft 1.17",
"slug": "minecraft-1-17",
"isSyncable": true,
"status": 1
}
]
}
64 changes: 64 additions & 0 deletions tests/Unit/Client/Fixtures/getVersionsV2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"data": [
{
"type": 3,
"versions": [
{
"id": 9672,
"slug": "43-2-3",
"name": "43.2.3"
},
{
"id": 9714,
"slug": "44-1-7",
"name": "44.1.7"
},
{
"id": 10090,
"slug": "40-2-10",
"name": "40.2.10"
}
]
},
{
"type": 11,
"versions": [
{
"id": 9791,
"slug": "1-5-3",
"name": "1.5.3"
},
{
"id": 312,
"slug": "1-5-2",
"name": "1.5.2"
},
{
"id": 280,
"slug": "1-5-1",
"name": "1.5.1"
}
]
},
{
"type": 76994,
"versions": [
{
"id": 10420,
"slug": "20-2-12-beta",
"name": "20.2.12-beta"
},
{
"id": 10513,
"slug": "20-4-17-beta",
"name": "20.4.17-beta"
},
{
"id": 10443,
"slug": "20-2-31-beta",
"name": "20.2.31-beta"
}
]
}
]
}
Loading