Skip to content

Commit

Permalink
Merge pull request #2 from simivar/feature/implement-summoner-endpoints
Browse files Browse the repository at this point in the history
Implement summoner endpoints
  • Loading branch information
simivar authored Oct 21, 2020
2 parents 6242a5f + c929500 commit 7e4d9f8
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 15 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ and uses PSR-17 and PSR-18 abstraction so you are free to choose any HTTP Client
composer require simivar/riot-php symfony/http-client nyholm/psr7
```

## APIs Coverage
| API | Docs | Status |
| -------------------- | ----------------------------------------------------------------- | ------ |
| Account v1 | [docs](https://developer.riotgames.com/apis#account-v1) | - |
| Champion Mastery v4 | [docs](https://developer.riotgames.com/apis#champion-mastery-v4) | - |
| Champion v3 | [docs](https://developer.riotgames.com/apis#champion-v3) | - |
| Clash v1 | [docs](https://developer.riotgames.com/apis#clash-v1) | - |
| League Exp v4 | [docs](https://developer.riotgames.com/apis#league-exp-v4) | - |
| League v4 | [docs](https://developer.riotgames.com/apis#league-v4) | - |
| Lol Status v3 | [docs](https://developer.riotgames.com/apis#lol-status-v3) | - |
| Lor Match v1 | [docs](https://developer.riotgames.com/apis#lor-match-v1) | - |
| Lor Ranked v1 | [docs](https://developer.riotgames.com/apis#lor-ranked-v1) | - |
| Match v4 | [docs](https://developer.riotgames.com/apis#match-v4) | - |
| Spectator v4 | [docs](https://developer.riotgames.com/apis#spectator-v4) | - |
| Summoner v4 | [docs](https://developer.riotgames.com/apis#summoner-v4) | 100% |
| Tft League v1 | [docs](https://developer.riotgames.com/apis#tft-league-v1) | - |
| Tft Match v1 | [docs](https://developer.riotgames.com/apis#tft-match-v1) | - |
| Tft Summoner v1 | [docs](https://developer.riotgames.com/apis#tft-summoner-v1) | - |
| Third Party Code v4 | [docs](https://developer.riotgames.com/apis#third-party-code-v4) | 100% |
| Tournament Stub v4 | [docs](https://developer.riotgames.com/apis#tournament-stub-v4) | - |
| Tournament v4 | [docs](https://developer.riotgames.com/apis#tournament-v4) | - |

# Legal notice
Riot PHP isn't endorsed by Riot Games and doesn't reflect the views or opinions of Riot Games or anyone officially
involved in producing or managing Riot Games properties. Riot Games, and all associated properties are trademarks
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"php": ">=7.4",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0",
"psr/http-factory": "^1.0"
"psr/http-factory": "^1.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
60 changes: 60 additions & 0 deletions src/Riot/API/Version4/Summoner.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,64 @@ public function getByName(string $summonerName, string $region): ?SummonerDTO

return SummonerDTO::createFromArray(json_decode($body, true, 512, JSON_THROW_ON_ERROR));
}

/**
* @throws RateLimitExceededException
* @throws \JsonException
*/
public function getByAccountId(string $encryptedAccountId, string $region): ?SummonerDTO
{
$response = $this->riotConnection->get(
$region,
sprintf('lol/summoner/v4/summoners/by-account/%s', $encryptedAccountId),
);

if (null === $response) {
return null;
}

$body = $response->getBody()->getContents();

return SummonerDTO::createFromArray(json_decode($body, true, 512, JSON_THROW_ON_ERROR));
}

/**
* @throws RateLimitExceededException
* @throws \JsonException
*/
public function getByPuuid(string $encryptedPuuid, string $region): ?SummonerDTO
{
$response = $this->riotConnection->get(
$region,
sprintf('lol/summoner/v4/summoners/by-puuid/%s', $encryptedPuuid),
);

if (null === $response) {
return null;
}

$body = $response->getBody()->getContents();

return SummonerDTO::createFromArray(json_decode($body, true, 512, JSON_THROW_ON_ERROR));
}

/**
* @throws RateLimitExceededException
* @throws \JsonException
*/
public function getById(string $id, string $region): ?SummonerDTO
{
$response = $this->riotConnection->get(
$region,
sprintf('lol/summoner/v4/summoners/%s', $id),
);

if (null === $response) {
return null;
}

$body = $response->getBody()->getContents();

return SummonerDTO::createFromArray(json_decode($body, true, 512, JSON_THROW_ON_ERROR));
}
}
74 changes: 63 additions & 11 deletions tests/Unit/API/Version4/SummonerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Riot\Tests\API\Version4;
namespace Riot\Tests\Unit\API\Version4;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -13,21 +13,19 @@

final class SummonerTest extends TestCase
{
public function testGetByNameReturnsNullOnGetNull(): void
private function setUpNullResponse(string $path): ConnectionInterface
{
$riotConnection = $this->createMock(ConnectionInterface::class);
$riotConnection->expects(self::once())
$nullRiotConnectionResponse = $this->createMock(ConnectionInterface::class);
$nullRiotConnectionResponse->expects(self::once())
->method('get')
->with(self::equalTo('eun1'), self::equalTo('lol/summoner/v4/summoners/by-name/simivar'))
->with(self::equalTo('eun1'), self::equalTo($path))
->willReturn(null)
;

$summoner = new Summoner($riotConnection);
$result = $summoner->getByName('simivar', 'eun1');
self::assertNull($result);
return $nullRiotConnectionResponse;
}

public function testGetByNameReturnsSummonerDTOOnSuccess(): void
private function setUpJsonResponse(string $path): ConnectionInterface
{
$apiResponse = '{"id": "1","accountId": "2","puuid": "3","name": "Simivar","profileIconId": 4,"revisionDate": 5,"summonerLevel": 6}';

Expand All @@ -46,12 +44,66 @@ public function testGetByNameReturnsSummonerDTOOnSuccess(): void
$riotConnection = $this->createMock(ConnectionInterface::class);
$riotConnection->expects(self::once())
->method('get')
->with(self::equalTo('eun1'), self::equalTo('lol/summoner/v4/summoners/by-name/simivar'))
->with(self::equalTo('eun1'), self::equalTo($path))
->willReturn($response)
;

$summoner = new Summoner($riotConnection);
return $riotConnection;
}

public function testGetByNameReturnsNullOnGetNull(): void
{
$summoner = new Summoner($this->setUpNullResponse('lol/summoner/v4/summoners/by-name/simivar'));
$result = $summoner->getByName('simivar', 'eun1');
self::assertNull($result);
}

public function testGetByNameReturnsSummonerDTOOnSuccess(): void
{
$summoner = new Summoner($this->setUpJsonResponse('lol/summoner/v4/summoners/by-name/simivar'));
$result = $summoner->getByName('simivar', 'eun1');
self::assertInstanceOf(SummonerDTO::class, $result);
}

public function testGetByAccountIdReturnsNullOnGetNull(): void
{
$summoner = new Summoner($this->setUpNullResponse('lol/summoner/v4/summoners/by-account/simivar'));
$result = $summoner->getByAccountId('simivar', 'eun1');
self::assertNull($result);
}

public function testGetByAccountIdReturnsSummonerDTOOnSuccess(): void
{
$summoner = new Summoner($this->setUpJsonResponse('lol/summoner/v4/summoners/by-account/simivar'));
$result = $summoner->getByAccountId('simivar', 'eun1');
self::assertInstanceOf(SummonerDTO::class, $result);
}

public function testGetByPuuidReturnsNullOnGetNull(): void
{
$summoner = new Summoner($this->setUpNullResponse('lol/summoner/v4/summoners/by-puuid/simivar'));
$result = $summoner->getByPuuid('simivar', 'eun1');
self::assertNull($result);
}

public function testGetByPuuidReturnsSummonerDTOOnSuccess(): void
{
$summoner = new Summoner($this->setUpJsonResponse('lol/summoner/v4/summoners/by-puuid/simivar'));
$result = $summoner->getByPuuid('simivar', 'eun1');
self::assertInstanceOf(SummonerDTO::class, $result);
}

public function testGetByIdReturnsNullOnGetNull(): void
{
$summoner = new Summoner($this->setUpNullResponse('lol/summoner/v4/summoners/simivar'));
$result = $summoner->getById('simivar', 'eun1');
self::assertNull($result);
}

public function testGetByIdReturnsSummonerDTOOnSuccess(): void
{
$summoner = new Summoner($this->setUpJsonResponse('lol/summoner/v4/summoners/simivar'));
$result = $summoner->getById('simivar', 'eun1');
self::assertInstanceOf(SummonerDTO::class, $result);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/API/Version4/ThirdPartyCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Riot\Tests\API\Version4;
namespace Riot\Tests\Unit\API\Version4;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DTO/SummonerDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Riot\Tests\DTO;
namespace Riot\Tests\Unit\DTO;

use PHPUnit\Framework\TestCase;
use Riot\DTO\SummonerDTO;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Exception/RateLimitExceededExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Riot\Tests\Exception;
namespace Riot\Tests\Unit\Exception;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand Down

0 comments on commit 7e4d9f8

Please sign in to comment.