diff --git a/README.md b/README.md index b3dc351..1acae39 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,9 @@ This client is a combination of code generated by [OpenAPI Generator](https://op from the Hangar OpenAPI specification and some wrappers around it to improve the usability. The generated code can be found in `src/Api` and `src/Model`. It is recommended to use the wrappers in -`src/Client` instead. The wrappers provide methods to fetch related data from the API, the data returned -by the API is available through `getData()`. +`src/Client` instead. The wrappers extend the generated models, so the data returned by the API is available +through the getters of the wrapper, e.g. `$project->getName()`. They also provide methods to fetch related data +from the API. ## Requirements - PHP 8.4 or newer @@ -88,7 +89,7 @@ you can use it like an array. It also has a `getResults()` method which returns $projects = $hangarClient->getProjects(); foreach ($projects as $project) { - echo $project->getData()->getName() . PHP_EOL; + echo $project->getName() . PHP_EOL; } if ($projects->hasNextPage()) { @@ -126,7 +127,7 @@ $projects = $hangarClient->getProjects($options); ```php // get a project by its slug or id $project = $hangarClient->getProject("mclogs"); -echo $project->getData()->getName() . ": " . $project->getData()->getStats()->getDownloads() . " downloads" . PHP_EOL; +echo $project->getName() . ": " . $project->getStats()->getDownloads() . " downloads" . PHP_EOL; // get the project a file belongs to by the SHA-256 hash of the file $project = $hangarClient->getProjectByVersionHash(hash_file("sha256", "/path/to/plugin.jar")); @@ -218,7 +219,7 @@ use Aternos\HangarApi\Client\Options\UserSearch\UserSortField; // get a user by name $user = $hangarClient->getUser("Aternos"); -echo $user->getData()->getName() . ": " . $user->getData()->getProjectCount() . " projects" . PHP_EOL; +echo $user->getName() . ": " . $user->getProjectCount() . " projects" . PHP_EOL; // search for users (paginated) $users = $hangarClient->getUsers(new UserSearchOptions()->setQuery("aternos")->setSort(UserSortField::NAME)); diff --git a/src/Client/CompactProject.php b/src/Client/CompactProject.php index 99be82b..a94d122 100644 --- a/src/Client/CompactProject.php +++ b/src/Client/CompactProject.php @@ -18,39 +18,27 @@ * @package Aternos\HangarApi\Client * @description This class wraps a compact project (a project with fewer fields) and allows you to fetch additional data. */ -class CompactProject +class CompactProject extends ProjectCompact { + /** + * @param HangarAPIClient $client + * @param ProjectCompact $data + */ public function __construct( protected HangarAPIClient $client, - protected ProjectCompact $project, + ProjectCompact $data, ) { + parent::__construct($data->container); } /** - * @return ProjectCompact - */ - public function getData(): ProjectCompact - { - return $this->project; - } - - /** - * Get the slug of this project (shorthand for getData()->getNamespace()->getSlug()) + * Get the slug of this project (shorthand for getNamespace()->getSlug()) * @return string */ public function getSlug(): string { - return $this->project->getNamespace()->getSlug(); - } - - /** - * Get the id of this project (shorthand for getData()->getId()) - * @return int - */ - public function getId(): int - { - return $this->project->getId(); + return $this->getNamespace()->getSlug(); } /** @@ -111,7 +99,7 @@ public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): arr { return $this->client->getDailyProjectStats( $this->getId(), - $from ?? $this->getData()->getCreatedAt(), + $from ?? $this->getCreatedAt(), $to ); } @@ -143,7 +131,7 @@ public function getMembers(): ProjectMemberList */ public function getOwner(): User { - return $this->client->getUser($this->project->getNamespace()->getOwner()); + return $this->client->getUser($this->getNamespace()->getOwner()); } /** diff --git a/src/Client/Project.php b/src/Client/Project.php index ac60ea8..f0abfda 100644 --- a/src/Client/Project.php +++ b/src/Client/Project.php @@ -9,6 +9,7 @@ use Aternos\HangarApi\Client\Options\VersionSearch\VersionSearchOptions; use Aternos\HangarApi\Model\DayProjectStats; use Aternos\HangarApi\Model\Platform; +use Aternos\HangarApi\Model\Project as ProjectModel; use DateTime; /** @@ -17,39 +18,27 @@ * @package Aternos\HangarApi\Client * @description This class wraps a full project on hangar and allows you to fetch additional data. */ -class Project +class Project extends ProjectModel { - public function __construct( - protected HangarAPIClient $client, - protected \Aternos\HangarApi\Model\Project $project, - ) - { - } - /** - * @return \Aternos\HangarApi\Model\Project + * @param HangarAPIClient $client + * @param ProjectModel $data */ - public function getData(): \Aternos\HangarApi\Model\Project + public function __construct( + protected HangarAPIClient $client, + ProjectModel $data, + ) { - return $this->project; + parent::__construct($data->container); } /** - * Get the slug of this project (shorthand for getData()->getNamespace()->getSlug()) + * Get the slug of this project (shorthand for getNamespace()->getSlug()) * @return string */ public function getSlug(): string { - return $this->project->getNamespace()->getSlug(); - } - - /** - * Get the id of this project (shorthand for getData()->getId()) - * @return int - */ - public function getId(): int - { - return $this->project->getId(); + return $this->getNamespace()->getSlug(); } /** @@ -101,7 +90,7 @@ public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): arr { return $this->client->getDailyProjectStats( $this->getId(), - $from ?? $this->getData()->getCreatedAt(), + $from ?? $this->getCreatedAt(), $to ); } @@ -133,7 +122,7 @@ public function getMembers(): ProjectMemberList */ public function getOwner(): User { - return $this->client->getUser($this->project->getNamespace()->getOwner()); + return $this->client->getUser($this->getNamespace()->getOwner()); } /** diff --git a/src/Client/User.php b/src/Client/User.php index 1cd4ca2..3dd2e60 100644 --- a/src/Client/User.php +++ b/src/Client/User.php @@ -7,6 +7,7 @@ use Aternos\HangarApi\Client\List\CompactProject\WatchedProjectList; use Aternos\HangarApi\Client\List\ProjectList; use Aternos\HangarApi\Client\Options\ProjectSearch\ProjectSearchOptions; +use Aternos\HangarApi\Model\User as UserModel; /** * Class User @@ -14,22 +15,18 @@ * @package Aternos\HangarApi\Client * @description This class wraps a hangar user and allows you to fetch additional data. */ -class User +class User extends UserModel { + /** + * @param HangarAPIClient $client + * @param UserModel $data + */ public function __construct( protected HangarAPIClient $client, - protected \Aternos\HangarApi\Model\User $user, + UserModel $data, ) { - } - - /** - * Get the user data - * @return \Aternos\HangarApi\Model\User - */ - public function getData(): \Aternos\HangarApi\Model\User - { - return $this->user; + parent::__construct($data->container); } /** @@ -39,7 +36,7 @@ public function getData(): \Aternos\HangarApi\Model\User */ public function getWatchedProjects(): WatchedProjectList { - return $this->client->getProjectsWatchedByUser($this->user->getId()); + return $this->client->getProjectsWatchedByUser($this->getId()); } /** @@ -49,7 +46,7 @@ public function getWatchedProjects(): WatchedProjectList */ public function getStarredProjects(): StarredProjectList { - return $this->client->getProjectsStarredByUser($this->user->getId()); + return $this->client->getProjectsStarredByUser($this->getId()); } /** @@ -59,7 +56,7 @@ public function getStarredProjects(): StarredProjectList */ public function getPinnedProjects(): array { - return $this->client->getProjectsPinnedByUser($this->user->getId()); + return $this->client->getProjectsPinnedByUser($this->getId()); } /** @@ -71,7 +68,7 @@ public function getPinnedProjects(): array public function getProjects(): ProjectList { $options = new ProjectSearchOptions(); - $options->setOwner($this->user->getName()); + $options->setOwner($this->getName()); return $this->client->getProjects($options); } } diff --git a/src/Client/Version.php b/src/Client/Version.php index 294f62a..3db4a42 100644 --- a/src/Client/Version.php +++ b/src/Client/Version.php @@ -3,6 +3,7 @@ namespace Aternos\HangarApi\Client; use Aternos\HangarApi\ApiException; +use Aternos\HangarApi\Model\Version as VersionModel; use Aternos\HangarApi\Model\VersionStats; use DateTime; @@ -12,15 +13,22 @@ * @package Aternos\HangarApi\Client * @description This class wraps a version of a project on hangar and allows you to fetch additional data. */ -class Version +class Version extends VersionModel { + /** + * @param HangarAPIClient $client + * @param VersionModel $data + * @param string $projectSlugOrId slug or id of the project this version belongs to + * @param Project|null $project the project this version belongs to, if it is already known + */ public function __construct( - protected HangarAPIClient $client, - protected \Aternos\HangarApi\Model\Version $version, - protected string $projectSlugOrId, - protected ?Project $project = null, + protected HangarAPIClient $client, + VersionModel $data, + protected string $projectSlugOrId, + protected ?Project $project = null, ) { + parent::__construct($data->container); } /** @@ -47,14 +55,6 @@ public function getProject(): Project return $this->project; } - /** - * @return \Aternos\HangarApi\Model\Version - */ - public function getData(): \Aternos\HangarApi\Model\Version - { - return $this->version; - } - /** * Fetch a daily list of statistics for this version * Days without downloads/views are not be included @@ -66,8 +66,8 @@ public function getData(): \Aternos\HangarApi\Model\Version public function getDailyStats(?DateTime $from = null, ?DateTime $to = null): array { return $this->client->getDailyVersionStatsById( - $this->getData()->getId(), - $from ?? $this->getData()->getCreatedAt(), + $this->getId(), + $from ?? $this->getCreatedAt(), $to ); } diff --git a/tests/Integration/Client/ClientTest.php b/tests/Integration/Client/ClientTest.php index 38d05b8..80960cf 100644 --- a/tests/Integration/Client/ClientTest.php +++ b/tests/Integration/Client/ClientTest.php @@ -62,41 +62,39 @@ protected function assertValidNamespace($namespace): void protected function assertValidProject($project): void { $this->assertNotNull($project); - $this->assertNotNull($project->getData()); - $this->assertValidNamespace($project->getData()->getNamespace()); - $this->assertNotNull($project->getData()->getName()); - $this->assertNotNull($project->getData()->getStats()); - $this->assertNotNull($project->getData()->getStats()->getViews()); - $this->assertNotNull($project->getData()->getStats()->getDownloads()); - $this->assertNotNull($project->getData()->getStats()->getRecentViews()); - $this->assertNotNull($project->getData()->getStats()->getRecentDownloads()); - $this->assertNotNull($project->getData()->getStats()->getStars()); - $this->assertNotNull($project->getData()->getStats()->getWatchers()); - $this->assertNotNull($project->getData()->getCategory()); - $this->assertNotSame(Category::UNKNOWN_DEFAULT_OPEN_API, $project->getData()->getCategory()); - $this->assertNotNull($project->getData()->getLastUpdated()); - $this->assertNotNull($project->getData()->getVisibility()); - $this->assertNotNull($project->getData()->getDescription()); - $this->assertNotNull($project->getData()->getSettings()); + $this->assertValidNamespace($project->getNamespace()); + $this->assertNotNull($project->getName()); + $this->assertNotNull($project->getStats()); + $this->assertNotNull($project->getStats()->getViews()); + $this->assertNotNull($project->getStats()->getDownloads()); + $this->assertNotNull($project->getStats()->getRecentViews()); + $this->assertNotNull($project->getStats()->getRecentDownloads()); + $this->assertNotNull($project->getStats()->getStars()); + $this->assertNotNull($project->getStats()->getWatchers()); + $this->assertNotNull($project->getCategory()); + $this->assertNotSame(Category::UNKNOWN_DEFAULT_OPEN_API, $project->getCategory()); + $this->assertNotNull($project->getLastUpdated()); + $this->assertNotNull($project->getVisibility()); + $this->assertNotNull($project->getDescription()); + $this->assertNotNull($project->getSettings()); } protected function assertValidCompactProject($project): void { $this->assertNotNull($project); - $this->assertNotNull($project->getData()); - $this->assertValidNamespace($project->getData()->getNamespace()); - $this->assertNotNull($project->getData()->getName()); - $this->assertNotNull($project->getData()->getStats()); - $this->assertNotNull($project->getData()->getStats()->getViews()); - $this->assertNotNull($project->getData()->getStats()->getDownloads()); - $this->assertNotNull($project->getData()->getStats()->getRecentViews()); - $this->assertNotNull($project->getData()->getStats()->getRecentDownloads()); - $this->assertNotNull($project->getData()->getStats()->getStars()); - $this->assertNotNull($project->getData()->getStats()->getWatchers()); - $this->assertNotNull($project->getData()->getCategory()); - $this->assertNotSame(Category::UNKNOWN_DEFAULT_OPEN_API, $project->getData()->getCategory()); - $this->assertNotNull($project->getData()->getLastUpdated()); - $this->assertNotNull($project->getData()->getVisibility()); + $this->assertValidNamespace($project->getNamespace()); + $this->assertNotNull($project->getName()); + $this->assertNotNull($project->getStats()); + $this->assertNotNull($project->getStats()->getViews()); + $this->assertNotNull($project->getStats()->getDownloads()); + $this->assertNotNull($project->getStats()->getRecentViews()); + $this->assertNotNull($project->getStats()->getRecentDownloads()); + $this->assertNotNull($project->getStats()->getStars()); + $this->assertNotNull($project->getStats()->getWatchers()); + $this->assertNotNull($project->getCategory()); + $this->assertNotSame(Category::UNKNOWN_DEFAULT_OPEN_API, $project->getCategory()); + $this->assertNotNull($project->getLastUpdated()); + $this->assertNotNull($project->getVisibility()); } protected function assertValidResultList($list): void @@ -110,19 +108,19 @@ protected function assertValidResultList($list): void protected function isSameProject($a, $b): bool { - if (!$a?->getData()?->getNamespace()?->getOwner() || !$b?->getData()?->getNamespace()?->getOwner()) { + if (!$a?->getNamespace()?->getOwner() || !$b?->getNamespace()?->getOwner()) { return false; } - if (!$a?->getData()?->getNamespace()?->getSlug() || !$b?->getData()?->getNamespace()?->getSlug()) { + if (!$a?->getNamespace()?->getSlug() || !$b?->getNamespace()?->getSlug()) { return false; } - if ($a->getData()->getNamespace()->getOwner() !== $b->getData()->getNamespace()->getOwner()) { + if ($a->getNamespace()->getOwner() !== $b->getNamespace()->getOwner()) { return false; } - if ($a->getData()->getNamespace()->getSlug() !== $b->getData()->getNamespace()->getSlug()) { + if ($a->getNamespace()->getSlug() !== $b->getNamespace()->getSlug()) { return false; } @@ -186,7 +184,7 @@ public function testGetProjectsInCategory(): void foreach ($projectList as $project) { $this->assertValidProject($project); - $this->assertSame(Category::ADMIN_TOOLS, $project->getData()->getCategory()); + $this->assertSame(Category::ADMIN_TOOLS, $project->getCategory()); } $this->assertTrue($projectList->hasNextPage()); @@ -202,7 +200,7 @@ public function testGetProjectsInCategory(): void foreach ($projectList as $project) { $this->assertValidProject($project); - $this->assertSame(Category::ADMIN_TOOLS, $project->getData()->getCategory()); + $this->assertSame(Category::ADMIN_TOOLS, $project->getCategory()); } $this->assertTrue($projectList->hasNextPage()); } @@ -223,7 +221,7 @@ public function testGetProjectsByOwner(): void foreach ($projectList as $project) { $this->assertValidProject($project); - $this->assertEquals("Aternos", $project->getData()->getNamespace()->getOwner()); + $this->assertEquals("Aternos", $project->getNamespace()->getOwner()); } $this->assertEquals(2, $projectList->getPagination()->getCount()); @@ -248,7 +246,7 @@ public function testGetRecentlyUpdatedProjects(): void $lastUpdated = time(); foreach ($projectList as $project) { $this->assertValidProject($project); - $updated = $project->getData()->getLastUpdated()->getTimestamp(); + $updated = $project->getLastUpdated()->getTimestamp(); $this->assertLessThanOrEqual($lastUpdated, $updated); $lastUpdated = $updated; } @@ -270,7 +268,7 @@ public function testGetNotRecentlyUpdatedProjects(): void $lastUpdated = 0; foreach ($projectList as $project) { $this->assertValidProject($project); - $updated = $project->getData()->getLastUpdated()->getTimestamp(); + $updated = $project->getLastUpdated()->getTimestamp(); $this->assertGreaterThanOrEqual($lastUpdated, $updated); $lastUpdated = $updated; } @@ -339,15 +337,15 @@ public function testGetProject(): void $project = $this->apiClient->getProject("mclogs"); $this->assertNotNull($project); $this->assertValidProject($project); - $this->assertEquals("Aternos", $project->getData()->getNamespace()->getOwner()); + $this->assertEquals("Aternos", $project->getNamespace()->getOwner()); $versions = $project->getVersions(); $this->assertNotNull($versions); $this->assertNotEmpty($versions); foreach ($versions as $version) { $this->assertEquals($project, $version->getProject()); - $fetched = $project->getVersion($version->getData()->getName()); - $this->assertEquals($version->getData()->getName(), $fetched->getData()->getName()); + $fetched = $project->getVersion($version->getName()); + $this->assertEquals($version->getName(), $fetched->getName()); } } @@ -360,7 +358,7 @@ public function testGetProjectMembers(): void $project = $this->apiClient->getProject("motdgg"); $this->assertNotNull($project); $this->assertValidProject($project); - $this->assertEquals("Aternos", $project->getData()->getNamespace()->getOwner()); + $this->assertEquals("Aternos", $project->getNamespace()->getOwner()); $members = $project->getMembers(); $this->assertValidResultList($members); @@ -378,12 +376,12 @@ public function testGetProjectWatchers(): void $project = $this->apiClient->getProject("motdgg"); $this->assertNotNull($project); $this->assertValidProject($project); - $this->assertEquals("Aternos", $project->getData()->getNamespace()->getOwner()); + $this->assertEquals("Aternos", $project->getNamespace()->getOwner()); $watchers = $project->getWatchers(); $this->assertValidResultList($watchers); $this->assertNotEmpty(array_filter($watchers->getResults(), function ($watcher) { - return $watcher->getData()->getName() === "Julian"; + return $watcher->getName() === "Julian"; })); foreach ($watchers as $watcher) { @@ -447,7 +445,7 @@ public function testGetDailyProjectVersionStats(): void $versions = $project->getVersions(); $this->assertValidResultList($versions); foreach ($versions as $version) { - foreach ($version->getData()->getStats()->getPlatformDownloads() as $downloads) { + foreach ($version->getStats()->getPlatformDownloads() as $downloads) { if ($downloads > 0) { $stats = $version->getDailyStats(); $this->assertNotNull($stats); @@ -481,8 +479,7 @@ public function testGetUsers(): void foreach ($users as $user) { $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertNotNull($user->getData()->getName()); + $this->assertNotNull($user->getName()); } $this->assertTrue($users->hasNextPage()); @@ -494,12 +491,11 @@ public function testGetUsers(): void $users = $users->getPreviousPage(); $this->assertValidResultList($users); - $this->assertEquals($firstUserOfPage[$i]->getData()->getName(), $users[0]->getData()->getName()); + $this->assertEquals($firstUserOfPage[$i]->getName(), $users[0]->getName()); foreach ($users as $user) { $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertNotNull($user->getData()->getName()); + $this->assertNotNull($user->getName()); } $this->assertTrue($users->hasNextPage()); } @@ -514,8 +510,7 @@ public function testGetUser(): void { $user = $this->apiClient->getUser("Aternos"); $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertEquals("Aternos", $user->getData()->getName()); + $this->assertEquals("Aternos", $user->getName()); } /** @@ -526,8 +521,7 @@ public function testGetProjectsWatchedByUser(): void { $user = $this->apiClient->getUser("Julian"); $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertEquals("Julian", $user->getData()->getName()); + $this->assertEquals("Julian", $user->getName()); $watched = $user->getWatchedProjects(); $this->assertValidResultList($watched); @@ -545,8 +539,7 @@ public function testGetProjectsStarredByUser(): void { $user = $this->apiClient->getUser("Julian"); $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertEquals("Julian", $user->getData()->getName()); + $this->assertEquals("Julian", $user->getName()); $starred = $user->getStarredProjects(); $this->assertValidResultList($starred); @@ -568,7 +561,7 @@ public function testGetProjectsStarredByUserSortedByStarsKeepsSortOnNextPage(): $this->assertValidResultList($starred); foreach ($starred as $project) { $this->assertValidCompactProject($project); - $stars[] = $project->getData()->getStats()->getStars(); + $stars[] = $project->getStats()->getStars(); } $starred = $starred->getNextPage(); } @@ -616,15 +609,14 @@ public function testGetProjectsOfUser(): void { $user = $this->apiClient->getUser("Aternos"); $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertEquals("Aternos", $user->getData()->getName()); + $this->assertEquals("Aternos", $user->getName()); $projects = $user->getProjects(); $this->assertValidResultList($projects); foreach ($projects as $project) { $this->assertValidProject($project); - $this->assertEquals("Aternos", $project->getData()->getNamespace()->getOwner()); + $this->assertEquals("Aternos", $project->getNamespace()->getOwner()); } $this->assertGreaterThanOrEqual(2, $projects->getPagination()->getCount()); @@ -646,8 +638,7 @@ public function testGetStaff(): void foreach ($users as $user) { $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertNotNull($user->getData()->getName()); + $this->assertNotNull($user->getName()); } $this->assertTrue($users->hasNextPage()); @@ -659,12 +650,11 @@ public function testGetStaff(): void $users = $users->getPreviousPage(); $this->assertValidResultList($users); - $this->assertEquals($firstUserOfPage[$i]->getData()->getName(), $users[0]->getData()->getName()); + $this->assertEquals($firstUserOfPage[$i]->getName(), $users[0]->getName()); foreach ($users as $user) { $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertNotNull($user->getData()->getName()); + $this->assertNotNull($user->getName()); } $this->assertTrue($users->hasNextPage()); } @@ -687,8 +677,7 @@ public function testGetAuthors(): void foreach ($users as $user) { $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertNotNull($user->getData()->getName()); + $this->assertNotNull($user->getName()); } $this->assertTrue($users->hasNextPage()); @@ -700,12 +689,11 @@ public function testGetAuthors(): void $users = $users->getPreviousPage(); $this->assertValidResultList($users); - $this->assertEquals($firstUserOfPage[$i]->getData()->getName(), $users[0]->getData()->getName()); + $this->assertEquals($firstUserOfPage[$i]->getName(), $users[0]->getName()); foreach ($users as $user) { $this->assertNotNull($user); - $this->assertNotNull($user->getData()); - $this->assertNotNull($user->getData()->getName()); + $this->assertNotNull($user->getName()); } $this->assertTrue($users->hasNextPage()); } @@ -771,7 +759,7 @@ public function testGetAuthorsSortedByNameKeepsSortOnNextPage(): void for ($i = 0; $i < 2; $i++) { $this->assertValidResultList($users); foreach ($users as $user) { - $names[] = $user->getData()->getName(); + $names[] = $user->getName(); } $users = $users->getNextPage(); } diff --git a/tests/Unit/Client/HangarAPIClientTest.php b/tests/Unit/Client/HangarAPIClientTest.php index 8c51a0c..570e377 100644 --- a/tests/Unit/Client/HangarAPIClientTest.php +++ b/tests/Unit/Client/HangarAPIClientTest.php @@ -297,7 +297,7 @@ public function testGetVersion(): void $version = $client->getVersion("mclogs", "3.3.4"); $this->assertGetRequest("/projects/mclogs/versions/3.3.4"); - $this->assertSame(30673, $version->getData()->getId()); + $this->assertSame(30673, $version->getId()); $this->assertSame("mclogs", $version->getProjectSlugOrId()); } @@ -329,7 +329,7 @@ public function testGetVersionById(): void $version = $client->getVersionById(30673); $this->assertGetRequest("/versions/30673"); - $this->assertSame("3.3.4", $version->getData()->getName()); + $this->assertSame("3.3.4", $version->getName()); } /** @@ -374,8 +374,8 @@ public function testGetUser(): void $user = $client->getUser("Aternos"); $this->assertGetRequest("/users/Aternos"); - $this->assertSame(196, $user->getData()->getId()); - $this->assertSame("Aternos", $user->getData()->getName()); + $this->assertSame(196, $user->getId()); + $this->assertSame("Aternos", $user->getName()); } /** diff --git a/tests/Unit/Client/ProjectTest.php b/tests/Unit/Client/ProjectTest.php index 8bc5060..3416fbe 100644 --- a/tests/Unit/Client/ProjectTest.php +++ b/tests/Unit/Client/ProjectTest.php @@ -39,7 +39,7 @@ public function testGetVersion(): void $version = $project->getVersion("3.3.4"); $this->assertGetRequest("/projects/246/versions/3.3.4", index: 1); - $this->assertSame("3.3.4", $version->getData()->getName()); + $this->assertSame("3.3.4", $version->getName()); } /** @@ -98,7 +98,7 @@ public function testGetOwner(): void ]); $project = $client->getProject("mclogs"); - $this->assertSame("Aternos", $project->getOwner()->getData()->getName()); + $this->assertSame("Aternos", $project->getOwner()->getName()); $this->assertGetRequest("/users/Aternos", index: 1); } diff --git a/tests/Unit/Client/WrapperTest.php b/tests/Unit/Client/WrapperTest.php new file mode 100644 index 0000000..648d00f --- /dev/null +++ b/tests/Unit/Client/WrapperTest.php @@ -0,0 +1,102 @@ + + */ + public static function wrapperProvider(): array + { + return [ + "Project" => [ + Project::class, + ProjectModel::class, + "getProject.json", + fn(HangarAPIClient $client) => $client->getProject("mclogs"), + ], + "CompactProject" => [ + CompactProject::class, + ProjectCompact::class, + "getUserPinnedProjects.json", + fn(HangarAPIClient $client) => $client->getProjectsPinnedByUser("Aternos")[0], + ], + "User" => [ + User::class, + UserModel::class, + "getUser.json", + fn(HangarAPIClient $client) => $client->getUser("Aternos"), + ], + "Version" => [ + Version::class, + VersionModel::class, + "showVersion.json", + fn(HangarAPIClient $client) => $client->getVersion("mclogs", "3.3.4"), + ], + ]; + } + + /** + * Create the generated model from the first entry of a fixture, the way the generated API does + * @param string $fixture + * @param class-string $modelClass + * @return object + */ + protected function createModel(string $fixture, string $modelClass): object + { + $data = json_decode(file_get_contents(__DIR__ . "/Fixtures/" . $fixture)); + return ObjectSerializer::deserialize(is_array($data) ? $data[0] : $data, $modelClass); + } + + /** + * @throws ApiException + */ + #[DataProvider("wrapperProvider")] + public function testWrapperExtendsModel(string $wrapperClass, string $modelClass, string $fixture, callable $fetch): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/" . $fixture))]); + + $wrapper = $fetch($client); + + $this->assertInstanceOf($wrapperClass, $wrapper); + $this->assertInstanceOf($modelClass, $wrapper); + $this->assertFalse(method_exists($wrapper, "getData")); + $this->assertEquals($this->createModel($fixture, $modelClass)->getId(), $wrapper->getId()); + } + + /** + * @throws ApiException + */ + #[DataProvider("wrapperProvider")] + public function testWrapperIsSerializedLikeModel(string $wrapperClass, string $modelClass, string $fixture, callable $fetch): void + { + $client = $this->createClient([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/" . $fixture))]); + + $wrapper = $fetch($client); + + $this->assertJsonStringEqualsJsonString( + json_encode($this->createModel($fixture, $modelClass)), + json_encode($wrapper), + ); + $this->assertStringNotContainsString("client", json_encode($wrapper)); + } +}