From 0179624d0a70544e61fc2b2240f2c11d9645cd78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 22 May 2024 16:17:11 +0100 Subject: [PATCH] feat: added statistic resource --- docs/03-supported-endpoints.md | 57 ++---- src/Endpoint/StatisticEndpoint.php | 203 ------------------- src/Entity/Statistic.php | 2 +- src/Resource/StatisticResource.php | 106 ++++++++++ src/SportMonksFootball.php | 11 +- tests/Integration/SportMonksFootballTest.php | 2 + tests/Integration/StatisticResourceTest.php | 63 ++++++ tests/StatisticEndpointTest_.php | 152 -------------- tests/Unit/CoachStatisticDetailTest.php | 24 +++ tests/Unit/CoachStatisticTest.php | 24 +++ tests/Unit/StatisticTest.php | 26 +++ tests/Unit/TeamStatisticDetailTest.php | 24 +++ tests/Unit/TeamStatisticTest.php | 24 +++ 13 files changed, 312 insertions(+), 406 deletions(-) delete mode 100644 src/Endpoint/StatisticEndpoint.php create mode 100644 src/Resource/StatisticResource.php create mode 100644 tests/Integration/StatisticResourceTest.php delete mode 100644 tests/StatisticEndpointTest_.php create mode 100644 tests/Unit/CoachStatisticDetailTest.php create mode 100644 tests/Unit/CoachStatisticTest.php create mode 100644 tests/Unit/StatisticTest.php create mode 100644 tests/Unit/TeamStatisticDetailTest.php create mode 100644 tests/Unit/TeamStatisticTest.php diff --git a/docs/03-supported-endpoints.md b/docs/03-supported-endpoints.md index 072b56f..7af1c2e 100644 --- a/docs/03-supported-endpoints.md +++ b/docs/03-supported-endpoints.md @@ -949,110 +949,77 @@ $response = $api->states()->getById(1); ### Statistics - [Official documentation](https://docs.sportmonks.com/football/endpoints-and-entities/endpoints/statistics) -- Cache default max age: `1 day` #### `getAllByPlayerId` ```php -getAllByPlayerId(int $playerId, int $page = 1, int $perPage = 25, string $order = 'asc'): PlayerStatisticCollection +getAllByPlayerId(int $playerId): PlayerStatisticCollection ``` Get all statistics by player id per season: ```php -$statistics = $sportMonksFootball->statistics()->getAllByPlayerId(1); - -foreach ($statistics->getData() as $statistic) { - foreach ($statistic->getDetails() as $detail) { - print_r($detail->getValue()); - } -} +$response = $api->statistics()->getAllByPlayerId(1); ``` #### `getAllByTeamId` ```php -getAllByTeamId(int $teamId, int $page = 1, int $perPage = 25, string $order = 'asc'): TeamStatisticCollection +getAllByTeamId(int $teamId): TeamStatisticCollection ``` Get all statistics by team id per season: ```php -$statistics = $sportMonksFootball->statistics()->getAllByTeamId(1); - -foreach ($statistics->getData() as $statistic) { - foreach ($statistic->getDetails() as $detail) { - print_r($detail->getValue()); - } -} +$response = $api->statistics()->getAllByTeamId(1); ``` #### `getAllByCoachId` ```php -getAllByCoachId(int $coachId, int $page = 1, int $perPage = 25, string $order = 'asc'): CoachStatisticCollection +getAllByCoachId(int $coachId): CoachStatisticCollection ``` Get all statistics by coach id per season: ```php -$statistics = $sportMonksFootball->statistics()->getAllByCoachId(1); - -foreach ($statistics->getData() as $statistic) { - foreach ($statistic->getDetails() as $detail) { - print_r($detail->getValue()); - } -} +$response = $api->statistics()->getAllByCoachId(1); ``` #### `getAllByRefereeId` ```php -getAllByRefereeId(int $refereeId, int $page = 1, int $perPage = 25, string $order = 'asc'): RefereeStatisticCollection +getAllByRefereeId(int $refereeId): RefereeStatisticCollection ``` Get all statistics by referee id per season: ```php -$statistics = $sportMonksFootball->statistics()->getAllByRefereeId(1); - -foreach ($statistics->getData() as $statistic) { - foreach ($statistic->getDetails() as $detail) { - print_r($detail->getValue()); - } -} +$response = $api->statistics()->getAllByRefereeId(1); ``` #### `getAllByStageId` ```php -getAllByStageId(int $stageId, int $page = 1, int $perPage = 25, string $order = 'asc'): StatisticCollection +getAllByStageId(int $stageId): StatisticCollection ``` Get all statistics by stage id: ```php -$statistics = $sportMonksFootball->statistics()->getAllByStageId(1); - -foreach ($statistics->getData() as $statistic) { - print_r($statistic->getValue()); -} +$response = $api->statistics()->getAllByStageId(1); ``` #### `getAllByRoundId` ```php -getAllByRoundId(int $roundId, int $page = 1, int $perPage = 25, string $order = 'asc'): StatisticCollection +getAllByRoundId(int $roundId): StatisticCollection ``` Get all statistics by round id: ```php -$statistics = $sportMonksFootball->statistics()->getAllByRoundId(1); - -foreach ($statistics->getData() as $statistic) { - print_r($statistic->getValue()); -} +$response = $api->statistics()->getAllByRoundId(1); ``` ### Teams diff --git a/src/Endpoint/StatisticEndpoint.php b/src/Endpoint/StatisticEndpoint.php deleted file mode 100644 index 169248a..0000000 --- a/src/Endpoint/StatisticEndpoint.php +++ /dev/null @@ -1,203 +0,0 @@ -validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/statistics/seasons/players/{playerId}', [ - 'playerId' => $playerId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new PlayerStatisticCollection($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllByTeamId( - int $teamId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): TeamStatisticCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/statistics/seasons/teams/{teamId}', [ - 'teamId' => $teamId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new TeamStatisticCollection($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllByCoachId( - int $coachId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): CoachStatisticCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/statistics/seasons/coaches/{coachId}', [ - 'coachId' => $coachId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new CoachStatisticCollection($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllByRefereeId( - int $refereeId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): RefereeStatisticCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/statistics/seasons/referees/{refereeId}', [ - 'refereeId' => $refereeId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new RefereeStatisticCollection($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllByStageId( - int $stageId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): StatisticCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/statistics/stages/{stageId}', [ - 'stageId' => $stageId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new StatisticCollection($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllByRoundId( - int $roundId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): StatisticCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/statistics/rounds/{roundId}', [ - 'roundId' => $roundId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new StatisticCollection($response); - } -} \ No newline at end of file diff --git a/src/Entity/Statistic.php b/src/Entity/Statistic.php index d3e8b7b..ae03ad1 100644 --- a/src/Entity/Statistic.php +++ b/src/Entity/Statistic.php @@ -27,7 +27,7 @@ public function __construct(array $data, string $timezone) $this->value = $data['value']; // include - // Season uses "statistic_type" instead of "type" + // season uses "statistic_type" instead of "type" $this->type = (isset($data['type']) && \is_array($data['type'])) || isset($data['statistic_type']) ? new Type($data['statistic_type'] ?? $data['type']) : null; diff --git a/src/Resource/StatisticResource.php b/src/Resource/StatisticResource.php new file mode 100644 index 0000000..f975edd --- /dev/null +++ b/src/Resource/StatisticResource.php @@ -0,0 +1,106 @@ +api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/statistics/seasons/players/{playerId}', [ + 'playerId' => $playerId + ]) + ); + + return new PlayerStatisticCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByTeamId(int $teamId): TeamStatisticCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/statistics/seasons/teams/{teamId}', [ + 'teamId' => $teamId + ]) + ); + + return new TeamStatisticCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByCoachId(int $coachId): CoachStatisticCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/statistics/seasons/coaches/{coachId}', [ + 'coachId' => $coachId + ]) + ); + + return new CoachStatisticCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByRefereeId(int $refereeId): RefereeStatisticCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/statistics/seasons/referees/{refereeId}', [ + 'refereeId' => $refereeId + ]) + ); + + return new RefereeStatisticCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByStageId(int $stageId): StatisticCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/statistics/stages/{stageId}', [ + 'stageId' => $stageId + ]) + ); + + return new StatisticCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByRoundId(int $roundId): StatisticCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/statistics/rounds/{roundId}', [ + 'roundId' => $roundId + ]) + ); + + return new StatisticCollection($data); + } +} \ No newline at end of file diff --git a/src/SportMonksFootball.php b/src/SportMonksFootball.php index 73dfd6f..ae976d2 100644 --- a/src/SportMonksFootball.php +++ b/src/SportMonksFootball.php @@ -48,6 +48,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\StageResource; use ProgrammatorDev\SportMonksFootball\Resource\StandingResource; use ProgrammatorDev\SportMonksFootball\Resource\StateResource; +use ProgrammatorDev\SportMonksFootball\Resource\StatisticResource; class SportMonksFootball extends Api { @@ -174,11 +175,11 @@ public function states(): StateResource return new StateResource($this); } -// public function statistics(): StatisticEndpoint -// { -// return new StatisticEndpoint($this); -// } -// + public function statistics(): StatisticResource + { + return new StatisticResource($this); + } + // public function teams(): TeamEndpoint // { // return new TeamEndpoint($this); diff --git a/tests/Integration/SportMonksFootballTest.php b/tests/Integration/SportMonksFootballTest.php index 46cb5c7..085f2ff 100644 --- a/tests/Integration/SportMonksFootballTest.php +++ b/tests/Integration/SportMonksFootballTest.php @@ -24,6 +24,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\StageResource; use ProgrammatorDev\SportMonksFootball\Resource\StandingResource; use ProgrammatorDev\SportMonksFootball\Resource\StateResource; +use ProgrammatorDev\SportMonksFootball\Resource\StatisticResource; use ProgrammatorDev\SportMonksFootball\Test\AbstractTest; class SportMonksFootballTest extends AbstractTest @@ -52,5 +53,6 @@ public function testMethods() $this->assertInstanceOf(StageResource::class, $this->api->stages()); $this->assertInstanceOf(StandingResource::class, $this->api->standings()); $this->assertInstanceOf(StateResource::class, $this->api->states()); + $this->assertInstanceOf(StatisticResource::class, $this->api->statistics()); } } \ No newline at end of file diff --git a/tests/Integration/StatisticResourceTest.php b/tests/Integration/StatisticResourceTest.php new file mode 100644 index 0000000..5cb4ea9 --- /dev/null +++ b/tests/Integration/StatisticResourceTest.php @@ -0,0 +1,63 @@ + [ + PlayerStatisticCollection::class, + MockResponse::PLAYER_STATISTIC_COLLECTION_DATA, + 'statistics', + 'getAllByPlayerId', + [1] + ]; + yield 'get all by team id' => [ + TeamStatisticCollection::class, + MockResponse::TEAM_STATISTIC_COLLECTION_DATA, + 'statistics', + 'getAllByTeamId', + [1] + ]; + yield 'get all by coach id' => [ + CoachStatisticCollection::class, + MockResponse::COACH_STATISTIC_COLLECTION_DATA, + 'statistics', + 'getAllByCoachId', + [1] + ]; + yield 'get all by referee id' => [ + RefereeStatisticCollection::class, + MockResponse::REFEREE_STATISTIC_COLLECTION_DATA, + 'statistics', + 'getAllByRefereeId', + [1] + ]; + yield 'get all by stage id' => [ + StatisticCollection::class, + MockResponse::STATISTIC_COLLECTION_DATA, + 'statistics', + 'getAllByStageId', + [1] + ]; + yield 'get all by round id' => [ + StatisticCollection::class, + MockResponse::STATISTIC_COLLECTION_DATA, + 'statistics', + 'getAllByRoundId', + [1] + ]; + } +} \ No newline at end of file diff --git a/tests/StatisticEndpointTest_.php b/tests/StatisticEndpointTest_.php deleted file mode 100644 index b5a201d..0000000 --- a/tests/StatisticEndpointTest_.php +++ /dev/null @@ -1,152 +0,0 @@ - [ - MockResponse::PLAYER_STATISTIC_COLLECTION_DATA, - 'statistics', - 'getAllByPlayerId', - [1], - 'assertPlayerStatisticResponse' - ]; - yield 'get all by team id' => [ - MockResponse::TEAM_STATISTIC_COLLECTION_DATA, - 'statistics', - 'getAllByTeamId', - [1], - 'assertTeamStatisticResponse' - ]; - yield 'get all by coach id' => [ - MockResponse::COACH_STATISTIC_COLLECTION_DATA, - 'statistics', - 'getAllByCoachId', - [1], - 'assertCoachStatisticResponse' - ]; - yield 'get all by referee id' => [ - MockResponse::REFEREE_STATISTIC_COLLECTION_DATA, - 'statistics', - 'getAllByRefereeId', - [1], - 'assertRefereeStatisticResponse' - ]; - yield 'get all by stage id' => [ - MockResponse::STATISTIC_COLLECTION_DATA, - 'statistics', - 'getAllByStageId', - [1], - 'assertStatisticResponse' - ]; - yield 'get all by round id' => [ - MockResponse::STATISTIC_COLLECTION_DATA, - 'statistics', - 'getAllByRoundId', - [1], - 'assertStatisticResponse' - ]; - } - - public static function provideEndpointInvalidPaginationData(): \Generator - { - yield 'get all by player id' => ['statistics', 'getAllByPlayerId', [1]]; - yield 'get all by team id' => ['statistics', 'getAllByTeamId', [1]]; - yield 'get all by coach id' => ['statistics', 'getAllByCoachId', [1]]; - yield 'get all by referee id' => ['statistics', 'getAllByRefereeId', [1]]; - yield 'get all by stage id' => ['statistics', 'getAllByStageId', [1]]; - yield 'get all by round id' => ['statistics', 'getAllByRoundId', [1]]; - } - - private function assertPlayerStatisticResponse(PlayerStatistic $playerStatistic): void - { - $this->assertSame(27356197, $playerStatistic->getId()); - $this->assertSame(14, $playerStatistic->getPlayerId()); - $this->assertSame(293, $playerStatistic->getTeamId()); - $this->assertSame(1282, $playerStatistic->getSeasonId()); - $this->assertSame(true, $playerStatistic->hasValues()); - $this->assertSame(25, $playerStatistic->getPositionId()); - $this->assertSame(22, $playerStatistic->getJerseyNumber()); - - $details = $playerStatistic->getDetails(); - $this->assertContainsOnlyInstancesOf(PlayerStatisticDetail::class, $details); - $this->assertSame(26922109, $details[0]->getId()); - $this->assertSame(27356197, $details[0]->getPlayerStatisticId()); - $this->assertSame(52, $details[0]->getTypeId()); - $this->assertSame(['total' => 1, 'goals' => 1, 'penalties' => 0], $details[0]->getValue()); - } - - private function assertTeamStatisticResponse(TeamStatistic $teamStatistic): void - { - $this->assertSame(38080, $teamStatistic->getId()); - $this->assertSame(53, $teamStatistic->getTeamId()); - $this->assertSame(718, $teamStatistic->getSeasonId()); - $this->assertSame(true, $teamStatistic->hasValues()); - - $details = $teamStatistic->getDetails(); - $this->assertContainsOnlyInstancesOf(TeamStatisticDetail::class, $details); - $this->assertSame(345190, $details[0]->getId()); - $this->assertSame(38080, $details[0]->getTeamStatisticId()); - $this->assertSame(194, $details[0]->getTypeId()); - $this->assertSame([ - 'all' => ['count' => 1, 'percentage' => 8.33], - 'home' => ['count' => 1, 'percentage' => 100], - 'away' => ['count' => 0, 'percentage' => 0] - ], $details[0]->getValue()); - } - - private function assertCoachStatisticResponse(CoachStatistic $coachStatistic): void - { - $this->assertSame(29782, $coachStatistic->getId()); - $this->assertSame(50, $coachStatistic->getCoachId()); - $this->assertSame(62, $coachStatistic->getTeamId()); - $this->assertSame(12963, $coachStatistic->getSeasonId()); - - $details = $coachStatistic->getDetails(); - $this->assertContainsOnlyInstancesOf(CoachStatisticDetail::class, $details); - $this->assertSame(237659, $details[0]->getId()); - $this->assertSame(29782, $details[0]->getCoachStatisticId()); - $this->assertSame(215, $details[0]->getTypeId()); - $this->assertSame(['count' => 9], $details[0]->getValue()); - } - - private function assertRefereeStatisticResponse(RefereeStatistic $refereeStatistic): void - { - $this->assertSame(52911, $refereeStatistic->getId()); - $this->assertSame(37, $refereeStatistic->getRefereeId()); - $this->assertSame(6018, $refereeStatistic->getSeasonId()); - - $details = $refereeStatistic->getDetails(); - $this->assertContainsOnlyInstancesOf(RefereeStatisticDetail::class, $details); - $this->assertSame(370211, $details[0]->getId()); - $this->assertSame(52911, $details[0]->getRefereeStatisticId()); - $this->assertSame(188, $details[0]->getTypeId()); - $this->assertSame(['count' => 1], $details[0]->getValue()); - } - - private function assertStatisticResponse(Statistic $statistic): void - { - $this->assertSame(647677, $statistic->getId()); - $this->assertSame(77463996, $statistic->getModelId()); - $this->assertSame(209, $statistic->getTypeId()); - $this->assertSame(21413511, $statistic->getRelationId()); - $this->assertSame(['count' => 5, 'player_id' => 21413511, 'player_name' => 'Elias Achouri'], $statistic->getValue()); - } -} \ No newline at end of file diff --git a/tests/Unit/CoachStatisticDetailTest.php b/tests/Unit/CoachStatisticDetailTest.php new file mode 100644 index 0000000..0cdd076 --- /dev/null +++ b/tests/Unit/CoachStatisticDetailTest.php @@ -0,0 +1,24 @@ + 1, + 'coach_statistic_id' => 1, + 'type_id' => 1, + 'value' => ['data' => 'test'] + ]); + + $this->assertSame(1, $entity->getId()); + $this->assertSame(1, $entity->getCoachStatisticId()); + $this->assertSame(1, $entity->getTypeId()); + $this->assertSame(['data' => 'test'], $entity->getValue()); + } +} \ No newline at end of file diff --git a/tests/Unit/CoachStatisticTest.php b/tests/Unit/CoachStatisticTest.php new file mode 100644 index 0000000..9783d0c --- /dev/null +++ b/tests/Unit/CoachStatisticTest.php @@ -0,0 +1,24 @@ + 1, + 'season_id' => 1, + 'coach_id' => 1, + 'team_id' => 1 + ], 'UTC'); + + $this->assertSame(1, $entity->getId()); + $this->assertSame(1, $entity->getSeasonId()); + $this->assertSame(1, $entity->getCoachId()); + $this->assertSame(1, $entity->getTeamId()); + } +} \ No newline at end of file diff --git a/tests/Unit/StatisticTest.php b/tests/Unit/StatisticTest.php new file mode 100644 index 0000000..51c7905 --- /dev/null +++ b/tests/Unit/StatisticTest.php @@ -0,0 +1,26 @@ + 1, + 'model_id' => 1, + 'type_id' => 1, + 'relation_id' => 1, + 'value' => ['data' => 'test'] + ], 'UTC'); + + $this->assertSame(1, $entity->getId()); + $this->assertSame(1, $entity->getModelId()); + $this->assertSame(1, $entity->getTypeId()); + $this->assertSame(1, $entity->getRelationId()); + $this->assertSame(['data' => 'test'], $entity->getValue()); + } +} \ No newline at end of file diff --git a/tests/Unit/TeamStatisticDetailTest.php b/tests/Unit/TeamStatisticDetailTest.php new file mode 100644 index 0000000..c0dc26f --- /dev/null +++ b/tests/Unit/TeamStatisticDetailTest.php @@ -0,0 +1,24 @@ + 1, + 'team_statistic_id' => 1, + 'type_id' => 1, + 'value' => ['data' => 'test'] + ]); + + $this->assertSame(1, $entity->getId()); + $this->assertSame(1, $entity->getTeamStatisticId()); + $this->assertSame(1, $entity->getTypeId()); + $this->assertSame(['data' => 'test'], $entity->getValue()); + } +} \ No newline at end of file diff --git a/tests/Unit/TeamStatisticTest.php b/tests/Unit/TeamStatisticTest.php new file mode 100644 index 0000000..80ecf80 --- /dev/null +++ b/tests/Unit/TeamStatisticTest.php @@ -0,0 +1,24 @@ + 1, + 'season_id' => 1, + 'team_id' => 1, + 'has_values' => true + ], 'UTC'); + + $this->assertSame(1, $entity->getId()); + $this->assertSame(1, $entity->getSeasonId()); + $this->assertSame(1, $entity->getTeamId()); + $this->assertSame(true, $entity->hasValues()); + } +} \ No newline at end of file