From a10682fc421027018b563342921dadb29cecc145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 21 May 2024 16:51:27 +0100 Subject: [PATCH] feat: added league resource --- docs/03-supported-endpoints.md | 62 ++--- src/Endpoint/LeagueEndpoint.php | 231 ------------------- src/Resource/LeagueResource.php | 133 +++++++++++ src/SportMonksFootball.php | 11 +- tests/Integration/LeagueResourceTest.php | 91 ++++++++ tests/Integration/SportMonksFootballTest.php | 2 + tests/LeagueEndpointTest_.php | 104 --------- tests/Unit/LeagueTest.php | 40 ++++ 8 files changed, 286 insertions(+), 388 deletions(-) delete mode 100644 src/Endpoint/LeagueEndpoint.php create mode 100644 src/Resource/LeagueResource.php create mode 100644 tests/Integration/LeagueResourceTest.php delete mode 100644 tests/LeagueEndpointTest_.php create mode 100644 tests/Unit/LeagueTest.php diff --git a/docs/03-supported-endpoints.md b/docs/03-supported-endpoints.md index ba6707e..6a075f5 100644 --- a/docs/03-supported-endpoints.md +++ b/docs/03-supported-endpoints.md @@ -365,23 +365,17 @@ $response = $api->fixtures()->getAllLastUpdated(); ### Leagues - [Official documentation](https://docs.sportmonks.com/football/endpoints-and-entities/endpoints/leagues) -- Cache default max age: `1 hour` - - `getAllLive` cache max age: `1 minute` #### `getAll` ```php -getAll(int $page = 1, int $perPage = 25, string $order = 'asc'): LeagueCollection +getAll(): LeagueCollection ``` Get all leagues: ```php -$leagues = $sportMonksFootball->leagues()->getAll(); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAll(); ``` #### `getById` @@ -393,8 +387,7 @@ getById(int $id): LeagueItem Get league by id: ```php -$league = $sportMonksFootball->leagues()->getById(1); -echo $league->getData()->getName(); +$response = $api->leagues()->getById(1); ``` #### `getAllLive` @@ -406,94 +399,67 @@ getAllLive(): LeagueCollection Get all leagues currently live: ```php -$leagues = $sportMonksFootball->leagues()->getAllLive(); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAllLive(); ``` -> **Note** -> Cache max age is forced to `1 minute` on this endpoint. - #### `getAllByFixtureDate` ```php -getAllByFixtureDate(\DateTimeInterface $date, int $page = 1, int $perPage = 25, string $order = 'asc'): LeagueCollection +getAllByFixtureDate(\DateTimeInterface $date): LeagueCollection ``` Get all leagues with fixtures by date: ```php -$leagues = $sportMonksFootball->leagues()->getAllByFixtureDate(new DateTime('today')); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAllByFixtureDate(new DateTime('today')); ``` #### `getAllByCountryId` ```php -getAllByCountryId(int $countryId, int $page = 1, int $perPage = 25, string $order = 'asc'): LeagueCollection +getAllByCountryId(int $countryId): LeagueCollection ``` Get all leagues by country id: ```php -$leagues = $sportMonksFootball->leagues()->getAllByCountryId(1); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAllByCountryId(1); ``` #### `getAllBySearchQuery` ```php -getAllBySearchQuery(string $query, int $page = 1, int $perPage = 25, string $order = 'asc'): LeagueCollection +getAllBySearchQuery(string $query): LeagueCollection ``` Get all leagues by search query: ```php -$leagues = $sportMonksFootball->leagues()->getAllBySearchQuery('premiership'); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAllBySearchQuery('premiership'); ``` #### `getAllByTeamId` ```php -getAllByTeamId(int $teamId, int $page = 1, int $perPage = 25, string $order = 'asc'): LeagueCollection +getAllByTeamId(int $teamId): LeagueCollection ``` Get all current and historical leagues by team id: ```php -$leagues = $sportMonksFootball->leagues()->getAllByTeamId(1); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAllByTeamId(1); ``` #### `getAllCurrentByTeamId` ```php -getAllCurrentByTeamId(int $teamId, int $page = 1, int $perPage = 25, string $order = 'asc'): LeagueCollection +getAllCurrentByTeamId(int $teamId): LeagueCollection ``` Get all current leagues by team id: ```php -$leagues = $sportMonksFootball->leagues()->getAllCurrentByTeamId(1); - -foreach ($leagues->getData() as $league) { - echo $league->getName(); -} +$response = $api->leagues()->getAllCurrentByTeamId(1); ``` ### Livescores diff --git a/src/Endpoint/LeagueEndpoint.php b/src/Endpoint/LeagueEndpoint.php deleted file mode 100644 index 012d4f9..0000000 --- a/src/Endpoint/LeagueEndpoint.php +++ /dev/null @@ -1,231 +0,0 @@ -validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: '/v3/football/leagues', - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new LeagueCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - */ - public function getById(int $id): LeagueItem - { - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/leagues/{id}', [ - 'id' => $id - ]) - ); - - return new LeagueItem($response); - } - - /** - * @throws ApiErrorException - * @throws Exception - */ - public function getAllLive(): LeagueCollection - { - // Force cache max age - $this->cacheTtl = 60; // 1 minute - - $response = $this->sendRequest( - method: 'GET', - path: '/v3/football/leagues/live' - ); - - return new LeagueCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - * @throws ValidationException - */ - public function getAllByFixtureDate( - \DateTimeInterface $date, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): LeagueCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/leagues/date/{date}', [ - 'date' => $date->format('Y-m-d') - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new LeagueCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - * @throws ValidationException - */ - public function getAllByCountryId( - int $countryId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): LeagueCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/leagues/countries/{countryId}', [ - 'countryId' => $countryId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new LeagueCollection($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllBySearchQuery( - string $query, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): LeagueCollection - { - $this->validateSearchQuery($query); - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/leagues/search/{query}', [ - 'query' => $query - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new LeagueCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - * @throws ValidationException - */ - public function getAllByTeamId( - int $teamId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): LeagueCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/leagues/teams/{teamId}', [ - 'teamId' => $teamId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new LeagueCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - * @throws ValidationException - */ - public function getAllCurrentByTeamId( - int $teamId, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): LeagueCollection - { - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/football/leagues/teams/{teamId}/current', [ - 'teamId' => $teamId - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new LeagueCollection($response); - } -} \ No newline at end of file diff --git a/src/Resource/LeagueResource.php b/src/Resource/LeagueResource.php new file mode 100644 index 0000000..2a38484 --- /dev/null +++ b/src/Resource/LeagueResource.php @@ -0,0 +1,133 @@ +api->request( + method: 'GET', + path: '/v3/football/leagues' + ); + + return new LeagueCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getById(int $id): LeagueItem + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/leagues/{id}', [ + 'id' => $id + ]) + ); + + return new LeagueItem($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllLive(): LeagueCollection + { + $data = $this->api->request( + method: 'GET', + path: '/v3/football/leagues/live' + ); + + return new LeagueCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByFixtureDate(\DateTimeInterface $date): LeagueCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/leagues/date/{date}', [ + 'date' => $date->format('Y-m-d') + ]) + ); + + return new LeagueCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByCountryId(int $countryId): LeagueCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/leagues/countries/{countryId}', [ + 'countryId' => $countryId + ]) + ); + + return new LeagueCollection($data); + } + + /** + * @throws ValidationException + * @throws ClientExceptionInterface + */ + public function getAllBySearchQuery(string $query): LeagueCollection + { + $this->validateQuery($query, 'query'); + + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/leagues/search/{query}', [ + 'query' => $query + ]) + ); + + return new LeagueCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByTeamId(int $teamId): LeagueCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/leagues/teams/{teamId}', [ + 'teamId' => $teamId + ]) + ); + + return new LeagueCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllCurrentByTeamId(int $teamId): LeagueCollection + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/football/leagues/teams/{teamId}/current', [ + 'teamId' => $teamId + ]) + ); + + return new LeagueCollection($data); + } +} \ No newline at end of file diff --git a/src/SportMonksFootball.php b/src/SportMonksFootball.php index 244bb38..3507de0 100644 --- a/src/SportMonksFootball.php +++ b/src/SportMonksFootball.php @@ -35,6 +35,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\CountryResource; use ProgrammatorDev\SportMonksFootball\Resource\FilterResource; use ProgrammatorDev\SportMonksFootball\Resource\FixtureResource; +use ProgrammatorDev\SportMonksFootball\Resource\LeagueResource; class SportMonksFootball extends Api { @@ -91,11 +92,11 @@ public function fixtures(): FixtureResource return new FixtureResource($this); } -// public function leagues(): LeagueEndpoint -// { -// return new LeagueEndpoint($this); -// } -// + public function leagues(): LeagueResource + { + return new LeagueResource($this); + } + // public function livescores(): LivescoreEndpoint // { // return new LivescoreEndpoint($this); diff --git a/tests/Integration/LeagueResourceTest.php b/tests/Integration/LeagueResourceTest.php new file mode 100644 index 0000000..7732be0 --- /dev/null +++ b/tests/Integration/LeagueResourceTest.php @@ -0,0 +1,91 @@ + [ + LeagueItem::class, + MockResponse::LEAGUE_ITEM_DATA, + 'leagues', + 'getById', + [1] + ]; + } + + public static function provideCollectionResponseData(): \Generator + { + yield 'get all' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAll' + ]; + yield 'get all live' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAllLive' + ]; + yield 'get all by fixture date' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAllByFixtureDate', + [new \DateTime('today')] + ]; + yield 'get all by country id' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAllByCountryId', + [1] + ]; + yield 'get all by search query' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAllBySearchQuery', + ['test'] + ]; + yield 'get all by team id' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAllByTeamId', + [1] + ]; + yield 'get all current by team id' => [ + LeagueCollection::class, + MockResponse::LEAGUE_COLLECTION_DATA, + 'leagues', + 'getAllCurrentByTeamId', + [1] + ]; + } + + public static function provideValidationExceptionData(): \Generator + { + yield 'get all by search query, blank query' => [ + 'leagues', + 'getAllBySearchQuery', + [''] + ]; + } +} \ No newline at end of file diff --git a/tests/Integration/SportMonksFootballTest.php b/tests/Integration/SportMonksFootballTest.php index b08db55..bd8cac9 100644 --- a/tests/Integration/SportMonksFootballTest.php +++ b/tests/Integration/SportMonksFootballTest.php @@ -10,6 +10,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\CountryResource; use ProgrammatorDev\SportMonksFootball\Resource\FilterResource; use ProgrammatorDev\SportMonksFootball\Resource\FixtureResource; +use ProgrammatorDev\SportMonksFootball\Resource\LeagueResource; use ProgrammatorDev\SportMonksFootball\Test\AbstractTest; class SportMonksFootballTest extends AbstractTest @@ -24,5 +25,6 @@ public function testMethods() $this->assertInstanceOf(CountryResource::class, $this->api->countries()); $this->assertInstanceOf(FilterResource::class, $this->api->filters()); $this->assertInstanceOf(FixtureResource::class, $this->api->fixtures()); + $this->assertInstanceOf(LeagueResource::class, $this->api->leagues()); } } \ No newline at end of file diff --git a/tests/LeagueEndpointTest_.php b/tests/LeagueEndpointTest_.php deleted file mode 100644 index 5952600..0000000 --- a/tests/LeagueEndpointTest_.php +++ /dev/null @@ -1,104 +0,0 @@ - [ - MockResponse::LEAGUE_ITEM_DATA, - 'leagues', - 'getById', - [1] - ]; - } - - public static function provideEndpointCollectionResponseData(): \Generator - { - yield 'get all' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAll', - [] - ]; - yield 'get all live' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAllLive', - [] - ]; - yield 'get all by fixture date' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAllByFixtureDate', - [new \DateTime('today')] - ]; - yield 'get all by country id' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAllByCountryId', - [1] - ]; - yield 'get all by search query' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAllBySearchQuery', - ['test'] - ]; - yield 'get all by team id' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAllByTeamId', - [1] - ]; - yield 'get all current by team id' => [ - MockResponse::LEAGUE_COLLECTION_DATA, - 'leagues', - 'getAllCurrentByTeamId', - [1] - ]; - } - - public static function provideEndpointInvalidPaginationData(): \Generator - { - yield 'get all' => ['leagues', 'getAll', []]; - yield 'get all by fixture date' => ['leagues', 'getAllByFixtureDate', [new \DateTime('today')]]; - yield 'get all by country id' => ['leagues', 'getAllByCountryId', [1]]; - yield 'get all by search query' => ['leagues', 'getAllBySearchQuery', ['test']]; - yield 'get all by team id' => ['leagues', 'getAllByTeamId', [1]]; - yield 'get all current by team id' => ['leagues', 'getAllCurrentByTeamId', [1]]; - } - - public static function provideEndpointInvalidSearchQueryData(): \Generator - { - yield 'get all by search query' => ['leagues', 'getAllBySearchQuery']; - } - - private function assertResponse(League $league): void - { - $this->assertSame(271, $league->getId()); - $this->assertSame(1, $league->getSportId()); - $this->assertSame(320, $league->getCountryId()); - $this->assertSame('Superliga', $league->getName()); - $this->assertSame(true, $league->isActive()); - $this->assertSame('DNK SL', $league->getShortCode()); - $this->assertSame('https://cdn.sportmonks.com/images/soccer/leagues/271.png', $league->getImagePath()); - $this->assertSame('league', $league->getType()); - $this->assertSame('domestic', $league->getSubType()); - $this->assertSame('2023-09-25 17:00:00', $league->getLastPlayedAt()->format('Y-m-d H:i:s')); - $this->assertSame(2, $league->getCategory()); - $this->assertSame(false, $league->hasJerseys()); - } -} \ No newline at end of file diff --git a/tests/Unit/LeagueTest.php b/tests/Unit/LeagueTest.php new file mode 100644 index 0000000..04b11c2 --- /dev/null +++ b/tests/Unit/LeagueTest.php @@ -0,0 +1,40 @@ + 1, + 'sport_id' => 1, + 'country_id' => 1, + 'name' => 'name', + 'active' => true, + 'short_code' => 'short code', + 'image_path' => 'https://image.path', + 'type' => 'league', + 'sub_type' => 'domestic', + 'last_played_at' => '2024-01-01 16:00:00', + 'category' => 1, + 'has_jerseys' => false + ], 'UTC'); + + $this->assertSame(1, $entity->getId()); + $this->assertSame(1, $entity->getSportId()); + $this->assertSame(1, $entity->getCountryId()); + $this->assertSame('name', $entity->getName()); + $this->assertSame(true, $entity->isActive()); + $this->assertSame('short code', $entity->getShortCode()); + $this->assertSame('https://image.path', $entity->getImagePath()); + $this->assertSame('league', $entity->getType()); + $this->assertSame('domestic', $entity->getSubType()); + $this->assertInstanceOf(\DateTimeImmutable::class, $entity->getLastPlayedAt()); + $this->assertSame(1, $entity->getCategory()); + $this->assertSame(false, $entity->hasJerseys()); + } +} \ No newline at end of file