diff --git a/README.md b/README.md index 470fbe2..87fc98a 100644 --- a/README.md +++ b/README.md @@ -84,14 +84,14 @@ use Aternos\SpigotApi\Client\SortDirection; $projects = $spigotClient->listProjects(1); foreach ($projects as $project) { - // wrappers expose underlying model via getData() - echo $project->getData()->getTitle() . PHP_EOL; + // wrappers extend the generated models, so all getters can be used directly + echo $project->getTitle() . PHP_EOL; } $projects = $projects->getNextPage(); foreach ($projects as $project) { - echo $project->getData()->getTitle() . PHP_EOL; + echo $project->getTitle() . PHP_EOL; } // list the most downloaded projects @@ -118,14 +118,14 @@ use Aternos\SpigotApi\Client\SortDirection; $projects = $spigotClient->listProjectsForCategory(Category::SPIGOT, 1); foreach ($projects as $project) { - // wrappers expose underlying model via getData() - echo $project->getData()->getTitle() . PHP_EOL; + // wrappers extend the generated models, so all getters can be used directly + echo $project->getTitle() . PHP_EOL; } $projects = $projects->getNextPage(); foreach ($projects as $project) { - echo $project->getData()->getTitle() . PHP_EOL; + echo $project->getTitle() . PHP_EOL; } // list the most recently updated projects in a category @@ -145,10 +145,13 @@ To get a single project (resource) by its numeric id, use `getProject()`. // get a specific project (by numeric id) $project = $spigotClient->getProject(2); -// get data from the wrapper -$id = $project->getData()->getId(); -$title = $project->getData()->getTitle(); -$description = $project->getData()->getDescription(); +// the wrapper extends the generated model, so all getters of the model can be used directly +$id = $project->getId(); +$title = $project->getTitle(); +$description = $project->getDescription(); + +// the category as an Aternos\SpigotApi\Client\Category enum +$category = $project->getCategoryType(); ``` ### More information @@ -167,10 +170,13 @@ $versions = $project->fetchVersions(); // fetch the newest versions first $versions = $project->fetchVersions(1, VersionOrder::POST_DATE, SortDirection::DESC); -// get the author -$author = $project->getAuthor(); +// fetch the full author from the API +$author = $project->fetchAuthor(); ``` +`$project->getAuthor()` and `$project->getCategory()` are the getters of the generated model. +They only contain the id and username of the author and the id, title and description of the category, and do not send a request. + ## Authors ### getAuthor() @@ -185,12 +191,12 @@ To get a single author by its numeric id, use `getAuthor()`. $author = $spigotClient->getAuthor(1); // get data from the wrapper -$id = $author->getData()->getId(); -$username = $author->getData()->getUsername(); -$resources = $author->getData()->getResourceCount(); -$identities = $author->getData()->getIdentities(); -$avatarUrl = $author->getData()->getAvatar(); -$activity = $author->getData()->getLastActivity(); +$id = $author->getId(); +$username = $author->getUsername(); +$resources = $author->getResourceCount(); +$identities = $author->getIdentities(); +$avatarUrl = $author->getAvatar(); +$activity = $author->getLastActivity(); ``` ### findAuthor() @@ -205,12 +211,12 @@ To find an author by username, use `findAuthor()`. $author = $spigotClient->findAuthor("md_5"); // get data from the wrapper -$id = $author->getData()->getId(); -$username = $author->getData()->getUsername(); -$resources = $author->getData()->getResourceCount(); -$identities = $author->getData()->getIdentities(); -$avatarUrl = $author->getData()->getAvatar(); -$activity = $author->getData()->getLastActivity(); +$id = $author->getId(); +$username = $author->getUsername(); +$resources = $author->getResourceCount(); +$identities = $author->getIdentities(); +$avatarUrl = $author->getAvatar(); +$activity = $author->getLastActivity(); ``` ### More information @@ -237,7 +243,7 @@ To list all projects for an author (paginated), use `getAuthorProjects()`. // list all projects from Luck $projects = $spigotClient->getAuthorProjects(100356); foreach ($projects as $project) { - echo $project->getData()->getTitle() . PHP_EOL; + echo $project->getTitle() . PHP_EOL; } ``` @@ -260,7 +266,7 @@ use Aternos\SpigotApi\Client\VersionOrder; $versions = $spigotClient->getProjectVersions(2); foreach ($versions as $version) { - echo $version->getData()->getTitle() . PHP_EOL; + echo $version->getTitle() . PHP_EOL; } // list the newest versions first @@ -277,8 +283,8 @@ To fetch a specific update by its id, use `getVersion()`. ```php $version = $spigotClient->getVersion(352711); -echo $version->getData()->getTitle() . PHP_EOL; -echo $version->getData()->getMessage() . PHP_EOL; +echo $version->getTitle() . PHP_EOL; +echo $version->getMessage() . PHP_EOL; ``` ### More information diff --git a/src/Client/Author.php b/src/Client/Author.php index bafea80..96d8bbd 100644 --- a/src/Client/Author.php +++ b/src/Client/Author.php @@ -4,21 +4,17 @@ use Aternos\SpigotApi\ApiException; use Aternos\SpigotApi\Client\List\PaginatedAuthorProjectList; -use Aternos\SpigotApi\Model\Author as SpigotAuthor; +use Aternos\SpigotApi\Model\Author as AuthorModel; -class Author +class Author extends AuthorModel { public function __construct( protected SpigotAPIClient $client, - protected SpigotAuthor $data + AuthorModel $data ) { - } - - public function getData(): SpigotAuthor - { - return $this->data; + parent::__construct($data->container); } /** @@ -28,7 +24,7 @@ public function getData(): SpigotAuthor */ public function getId(): int { - return intval($this->data->getId()); + return intval(parent::getId()); } /** diff --git a/src/Client/Project.php b/src/Client/Project.php index fe58976..4e77abb 100644 --- a/src/Client/Project.php +++ b/src/Client/Project.php @@ -6,19 +6,15 @@ use Aternos\SpigotApi\Client\List\PaginatedVersionList; use Aternos\SpigotApi\Model\Resource; -class Project +class Project extends Resource { public function __construct( protected SpigotAPIClient $client, - protected Resource $data + Resource $data ) { - } - - public function getData(): Resource - { - return $this->data; + parent::__construct($data->container); } /** @@ -28,7 +24,7 @@ public function getData(): Resource */ public function getId(): int { - return intval($this->data->getId()); + return intval(parent::getId()); } /** @@ -36,19 +32,22 @@ public function getId(): int * * @return Category */ - public function getCategory(): Category + public function getCategoryType(): Category { - return Category::from($this->getData()->getCategory()->getId()); + return Category::from($this->getCategory()->getId()); } /** - * Get the author of the project. + * Fetch the author of this project from the API. + * + * This sends a request to the SpigotMC API every time it is called. * + * @return Author * @throws ApiException */ - public function getAuthor(): Author + public function fetchAuthor(): Author { - return $this->client->getAuthor($this->data->getAuthor()->getId()); + return $this->client->getAuthor($this->getAuthor()->getId()); } /** diff --git a/src/Client/Version.php b/src/Client/Version.php index e291688..3a49e89 100644 --- a/src/Client/Version.php +++ b/src/Client/Version.php @@ -5,19 +5,15 @@ use Aternos\SpigotApi\ApiException; use Aternos\SpigotApi\Model\ResourceUpdate; -class Version +class Version extends ResourceUpdate { public function __construct( protected SpigotAPIClient $client, - protected ResourceUpdate $data + ResourceUpdate $data ) { - } - - public function getData(): ResourceUpdate - { - return $this->data; + parent::__construct($data->container); } /** @@ -27,7 +23,7 @@ public function getData(): ResourceUpdate */ public function getId(): int { - return intval($this->data->getId()); + return intval(parent::getId()); } /** @@ -37,7 +33,7 @@ public function getId(): int */ public function getProjectId(): int { - return intval($this->getData()->getResourceId()); + return intval($this->getResourceId()); } /** diff --git a/tests/Integration/Client/ClientTest.php b/tests/Integration/Client/ClientTest.php index a29465f..4e6fe66 100644 --- a/tests/Integration/Client/ClientTest.php +++ b/tests/Integration/Client/ClientTest.php @@ -26,10 +26,10 @@ public function testListProjectsOrderedByDownloadCount(): void // collect the download counts of all projects from both pages $downloadCounts = []; foreach ($firstPage as $project) { - $downloadCounts[] = $project->getData()->getStats()->getDownloads(); + $downloadCounts[] = $project->getStats()->getDownloads(); } foreach ($secondPage as $project) { - $downloadCounts[] = $project->getData()->getStats()->getDownloads(); + $downloadCounts[] = $project->getStats()->getDownloads(); } $this->assertNotEmpty($downloadCounts); @@ -47,10 +47,10 @@ public function testGetProjectVersionsOrderedByPostDate(): void // collect the post dates of all versions from both pages $postDates = []; foreach ($firstPage as $version) { - $postDates[] = $version->getData()->getPostDate(); + $postDates[] = $version->getPostDate(); } foreach ($secondPage as $version) { - $postDates[] = $version->getData()->getPostDate(); + $postDates[] = $version->getPostDate(); } $this->assertNotEmpty($postDates); @@ -267,9 +267,9 @@ public function testGetProject(): void { $project = $this->apiClient->getProject(2); $this->assertNotNull($project); - $this->assertEquals("HubKick", $project->getData()->getTitle()); - $this->assertEquals("Send players to lobby on kick. /lobby / hub", $project->getData()->getTag()); - $this->assertEquals(1364368470, $project->getData()->getFirstRelease()); + $this->assertEquals("HubKick", $project->getTitle()); + $this->assertEquals("Send players to lobby on kick. /lobby / hub", $project->getTag()); + $this->assertEquals(1364368470, $project->getFirstRelease()); } /** @@ -297,9 +297,9 @@ public function testGetVersion(): void // PlaceholderAPI by HelpChat $version = $this->apiClient->getVersion(352711); $this->assertNotNull($version); - $this->assertEquals("2.10.9 back to normal", $version->getData()->getTitle()); - $this->assertEquals(352711, $version->getData()->getId()); - $this->assertEquals(6245, $version->getData()->getResourceId()); + $this->assertEquals("2.10.9 back to normal", $version->getTitle()); + $this->assertEquals(352711, $version->getId()); + $this->assertEquals(6245, $version->getResourceId()); } /** @@ -309,8 +309,7 @@ public function testGetAuthor(): void { $author = $this->apiClient->getAuthor(100356); $this->assertNotNull($author); - $this->assertNotNull($author->getData()); - $this->assertEquals("Luck", $author->getData()->getUsername()); + $this->assertEquals("Luck", $author->getUsername()); } /** @@ -320,9 +319,8 @@ public function testFindAuthor(): void { $author = $this->apiClient->findAuthor("Luck"); $this->assertNotNull($author); - $this->assertNotNull($author->getData()); - $this->assertEquals("Luck", $author->getData()->getUsername()); - $this->assertEquals(100356, $author->getData()->getId()); + $this->assertEquals("Luck", $author->getUsername()); + $this->assertEquals(100356, $author->getId()); } } \ No newline at end of file diff --git a/tests/Integration/Client/ProjectTest.php b/tests/Integration/Client/ProjectTest.php index d2c28c2..a018524 100644 --- a/tests/Integration/Client/ProjectTest.php +++ b/tests/Integration/Client/ProjectTest.php @@ -21,33 +21,33 @@ public function setUp(): void $this->project = $this->apiClient->getProject(57242); } - public function testGetCategory(): void + public function testGetCategoryType(): void { - $resourceCategory = $this->project->getData()->getCategory(); + $resourceCategory = $this->project->getCategory(); $this->assertEquals(15, $resourceCategory->getId()); $this->assertEquals("Tools and Utilities", $resourceCategory->getTitle()); $this->assertEquals("", $resourceCategory->getDescription()); - $category = $this->project->getCategory(); + $category = $this->project->getCategoryType(); $this->assertEquals(Category::SPIGOT_TOOLS_AND_UTILITIES, $category); } /** * @throws ApiException */ - public function testGetAuthor(): void + public function testFetchAuthor(): void { // Luck - $author = $this->project->getAuthor(); + $author = $this->project->fetchAuthor(); $this->assertNotNull($author); - $this->assertValidAuthor($author->getData()); + $this->assertValidAuthor($author); - $this->assertEquals(100356, $author->getData()->getId()); - $this->assertEquals("Luck", $author->getData()->getUsername()); - $this->assertGreaterThan(1, $author->getData()->getResourceCount()); - $this->assertStringStartsWith("https://www.spigotmc.org/data/avatars", $author->getData()->getAvatar()); + $this->assertEquals(100356, $author->getId()); + $this->assertEquals("Luck", $author->getUsername()); + $this->assertGreaterThan(1, $author->getResourceCount()); + $this->assertStringStartsWith("https://www.spigotmc.org/data/avatars", $author->getAvatar()); // 1759349337 -> Wed Oct 01 2025 - $this->assertGreaterThanOrEqual(1759349337, $author->getData()->getLastActivity()); + $this->assertGreaterThanOrEqual(1759349337, $author->getLastActivity()); } /** diff --git a/tests/TestCase.php b/tests/TestCase.php index 6a55b58..bd3a2df 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -59,39 +59,37 @@ protected function assertValidCategory(ResourceCategory $category): void protected function assertValidProject(Project $project): void { - $this->assertNotNull($project->getData()); - - $this->assertNotNull($project->getData()->getStats()); - $this->assertValidStats($project->getData()->getStats()); - - $this->assertNotNull($project->getData()->getAuthor()); - $this->assertValidResourceAuthor($project->getData()->getAuthor()); - - $this->assertNotNull($project->getData()->getId()); - $this->assertNotNull($project->getData()->getTitle()); - $this->assertNotNull($project->getData()->getTag()); - $this->assertNotNull($project->getData()->getCurrentVersion()); - // $project->getData()->getNativeMinecraftVersion() can be null - // $this->assertNotNull($project->getData()->getSupportedMinecraftVersions()) can be null or empty - $this->assertNotNull($project->getData()->getIconLink()); - $this->assertNotNull($project->getData()->getExternalDownloadUrl()); - $this->assertNotNull($project->getData()->getDescription()); - $this->assertNotNull($project->getData()->getFirstRelease()); - $this->assertNotNull($project->getData()->getLastUpdate()); - $this->assertNotNull($project->getData()->getSourceCodeUrl()); - $this->assertNotNull($project->getData()->getDonateUrl()); + $this->assertNotNull($project->getStats()); + $this->assertValidStats($project->getStats()); + + $this->assertNotNull($project->getAuthor()); + $this->assertValidResourceAuthor($project->getAuthor()); + + $this->assertNotNull($project->getId()); + $this->assertNotNull($project->getTitle()); + $this->assertNotNull($project->getTag()); + $this->assertNotNull($project->getCurrentVersion()); + // $project->getNativeMinecraftVersion() can be null + // $this->assertNotNull($project->getSupportedMinecraftVersions()) can be null or empty + $this->assertNotNull($project->getIconLink()); + $this->assertNotNull($project->getExternalDownloadUrl()); + $this->assertNotNull($project->getDescription()); + $this->assertNotNull($project->getFirstRelease()); + $this->assertNotNull($project->getLastUpdate()); + $this->assertNotNull($project->getSourceCodeUrl()); + $this->assertNotNull($project->getDonateUrl()); } protected function assertValidVersion(Version $version): void { $this->assertNotNull($version->getId()); $this->assertNotNull($version->getProjectId()); - $this->assertNotNull($version->getData()->getId()); - $this->assertNotNull($version->getData()->getResourceId()); - $this->assertNotNull($version->getData()->getDownloadCount()); - $this->assertNotNull($version->getData()->getTitle()); - $this->assertNotNull($version->getData()->getMessage()); - $this->assertNotNull($version->getData()->getPostDate()); + $this->assertNotNull($version->getId()); + $this->assertNotNull($version->getResourceId()); + $this->assertNotNull($version->getDownloadCount()); + $this->assertNotNull($version->getTitle()); + $this->assertNotNull($version->getMessage()); + $this->assertNotNull($version->getPostDate()); } protected function assertValidPaginatedList(PaginatedList $list): void diff --git a/tests/Unit/Client/AuthorTest.php b/tests/Unit/Client/AuthorTest.php index 9b11486..8954ad3 100644 --- a/tests/Unit/Client/AuthorTest.php +++ b/tests/Unit/Client/AuthorTest.php @@ -4,6 +4,7 @@ use Aternos\SpigotApi\ApiException; use Aternos\SpigotApi\Client\Author; +use Aternos\SpigotApi\Model\Author as AuthorModel; use Aternos\SpigotApi\Tests\TestCase; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; @@ -12,9 +13,9 @@ class AuthorTest extends TestCase { - protected function getAuthorModel(): \Aternos\SpigotApi\Model\Author + protected function getAuthorModel(): AuthorModel { - return new \Aternos\SpigotApi\Model\Author([ + return new AuthorModel([ "id" => 100356, "username" => "Luck", "resource_count" => 4, @@ -24,6 +25,15 @@ protected function getAuthorModel(): \Aternos\SpigotApi\Model\Author ]); } + public function testExtendsModel(): void + { + $author = new Author($this->apiClient, $this->getAuthorModel()); + + $this->assertInstanceOf(AuthorModel::class, $author); + $this->assertSame($this->getAuthorModel()->getUsername(), $author->getUsername()); + $this->assertSame($this->getAuthorModel()->getId(), $author->getId()); + } + /** * @throws ApiException */ @@ -41,7 +51,7 @@ public function testFetchProjects(): void foreach ($projects as $project) { $this->assertValidProject($project); - $this->assertEquals($author->getId(), $project->getData()->getAuthor()->getId()); + $this->assertEquals($author->getId(), $project->getAuthor()->getId()); } } } \ No newline at end of file diff --git a/tests/Unit/Client/ClientTest.php b/tests/Unit/Client/ClientTest.php index 227fbaa..ee92141 100644 --- a/tests/Unit/Client/ClientTest.php +++ b/tests/Unit/Client/ClientTest.php @@ -50,7 +50,7 @@ public function testGetProjectsInCategory(): void foreach ($projectList as $project) { $this->assertValidProject($project); - $this->assertEquals(Category::SPIGOT->value, $project->getData()->getCategory()->getId()); + $this->assertEquals(Category::SPIGOT->value, $project->getCategory()->getId()); } } @@ -67,7 +67,7 @@ public function testGetProject(): void $project = $this->apiClient->getProject(2); $this->assertNotNull($project); $this->assertValidProject($project); - $this->assertEquals("spark", $project->getData()->getTitle()); + $this->assertEquals("spark", $project->getTitle()); } /** @@ -83,9 +83,9 @@ public function testGetVersion(): void $version = $this->apiClient->getVersion(352711); $this->assertNotNull($version); $this->assertValidVersion($version); - $this->assertEquals("spark", $version->getData()->getTitle()); - $this->assertEquals(227294, $version->getData()->getId()); - $this->assertEquals(57242, $version->getData()->getResourceId()); + $this->assertEquals("spark", $version->getTitle()); + $this->assertEquals(227294, $version->getId()); + $this->assertEquals(57242, $version->getResourceId()); } /** @@ -116,8 +116,7 @@ public function testGetAuthor(): void $user = $this->apiClient->getAuthor(1); $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertEquals("Luck", $user->getData()->getUsername()); + $this->assertEquals("Luck", $user->getUsername()); } /** @@ -132,9 +131,8 @@ public function testFindAuthor(): void $user = $this->apiClient->findAuthor("Luck"); $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertEquals("Luck", $user->getData()->getUsername()); - $this->assertEquals(100356, $user->getData()->getId()); + $this->assertEquals("Luck", $user->getUsername()); + $this->assertEquals(100356, $user->getId()); } /** @@ -154,7 +152,7 @@ public function testGetAuthorProjects(): void foreach ($projectList as $project) { $this->assertValidProject($project); - $this->assertEquals(100356, $project->getData()->getAuthor()->getId()); + $this->assertEquals(100356, $project->getAuthor()->getId()); } } diff --git a/tests/Unit/Client/ProjectTest.php b/tests/Unit/Client/ProjectTest.php index be67210..ba853f2 100644 --- a/tests/Unit/Client/ProjectTest.php +++ b/tests/Unit/Client/ProjectTest.php @@ -91,17 +91,39 @@ protected function getResourceModel(): Resource ]); } - public function testGetCategory(): void + public function testExtendsModel(): void { $project = new Project($this->apiClient, $this->getResourceModel()); - $this->assertSame(Category::SPIGOT_TOOLS_AND_UTILITIES, $project->getCategory()); + $this->assertInstanceOf(Resource::class, $project); + $this->assertSame(57242, $project->getId()); + $this->assertSame("spark", $project->getTitle()); + $this->assertSame("1.10.119", $project->getCurrentVersion()); + } + + public function testModelGettersDoNotSendRequests(): void + { + // the mock handler has no responses, so every request would fail + $this->apiClient->setHttpClient(new Client(['handler' => HandlerStack::create(new MockHandler())])); + $project = new Project($this->apiClient, $this->getResourceModel()); + + $this->assertInstanceOf(ResourceAuthor::class, $project->getAuthor()); + $this->assertSame("Luck", $project->getAuthor()->getUsername()); + $this->assertInstanceOf(ResourceCategory::class, $project->getCategory()); + $this->assertSame("Tools and Utilities", $project->getCategory()->getTitle()); + } + + public function testGetCategoryType(): void + { + $project = new Project($this->apiClient, $this->getResourceModel()); + + $this->assertSame(Category::SPIGOT_TOOLS_AND_UTILITIES, $project->getCategoryType()); } /** * @throws ApiException */ - public function testGetAuthor(): void + public function testFetchAuthor(): void { $handler = new MockHandler([ new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getAuthor.json")), @@ -109,14 +131,14 @@ public function testGetAuthor(): void $this->apiClient->setHttpClient(new Client(['handler' => HandlerStack::create($handler)])); $project = new Project($this->apiClient, $this->getResourceModel()); - $author = $project->getAuthor(); + $author = $project->fetchAuthor(); $this->assertNotNull($author); - $this->assertEquals(100356, $author->getData()->getId()); - $this->assertEquals("Luck", $author->getData()->getUsername()); - $this->assertGreaterThan(1, $author->getData()->getResourceCount()); - $this->assertStringStartsWith("https://www.spigotmc.org/data/avatars", $author->getData()->getAvatar()); + $this->assertEquals(100356, $author->getId()); + $this->assertEquals("Luck", $author->getUsername()); + $this->assertGreaterThan(1, $author->getResourceCount()); + $this->assertStringStartsWith("https://www.spigotmc.org/data/avatars", $author->getAvatar()); // 1759349337 -> Wed Oct 01 2025 - $this->assertGreaterThanOrEqual(1759349337, $author->getData()->getLastActivity()); + $this->assertGreaterThanOrEqual(1759349337, $author->getLastActivity()); } diff --git a/tests/Unit/Client/VersionTest.php b/tests/Unit/Client/VersionTest.php index e7615c3..ee51a12 100644 --- a/tests/Unit/Client/VersionTest.php +++ b/tests/Unit/Client/VersionTest.php @@ -43,6 +43,15 @@ protected function getResourceUpdateModel(): ResourceUpdate ]); } + public function testExtendsModel(): void + { + $version = new Version($this->apiClient, $this->getResourceUpdateModel()); + + $this->assertInstanceOf(ResourceUpdate::class, $version); + $this->assertSame($this->getResourceUpdateModel()->getTitle(), $version->getTitle()); + $this->assertSame($this->getResourceUpdateModel()->getId(), $version->getId()); + } + public function testGetProjectId(): void { $version = new Version($this->apiClient, $this->getResourceUpdateModel()); @@ -62,7 +71,7 @@ public function testFetchProject(): void $project = $version->fetchProject(); $this->assertNotNull($project); - $this->assertEquals(57242, $project->getData()->getId()); - $this->assertEquals("spark", $project->getData()->getTitle()); + $this->assertEquals(57242, $project->getId()); + $this->assertEquals("spark", $project->getTitle()); } -} \ No newline at end of file +}