Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  throw specific exceptions for 401 and 403 http codes
  fix search test
  • Loading branch information
Baptouuuu committed Jun 13, 2020
2 parents f9e2a7d + 9677ffb commit 63717b9
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/Exception/InvalidToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types = 1);

namespace MusicCompanion\AppleMusic\Exception;

final class InvalidToken extends RuntimeException
{
}
8 changes: 8 additions & 0 deletions src/Exception/InvalidUserToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types = 1);

namespace MusicCompanion\AppleMusic\Exception;

final class InvalidUserToken extends RuntimeException
{
}
8 changes: 8 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types = 1);

namespace MusicCompanion\AppleMusic\Exception;

class RuntimeException extends \RuntimeException implements Exception
{
}
38 changes: 28 additions & 10 deletions src/SDK/HttpTransport/AppleMusic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

namespace MusicCompanion\AppleMusic\SDK\HttpTransport;

use MusicCompanion\AppleMusic\Exception\{
InvalidToken,
InvalidUserToken,
};
use Innmind\HttpTransport\{
Transport,
ThrowOnErrorTransport,
Exception\ClientError,
};
use Innmind\Http\Message\{
Request,
Response,
StatusCode,
};
use Innmind\Url\Url;

Expand All @@ -26,15 +32,27 @@ public function __construct(Transport $fulfill)

public function __invoke(Request $request): Response
{
return ($this->fulfill)(new Request\Request(
$this
->url
->withPath($request->url()->path())
->withQuery($request->url()->query()),
$request->method(),
$request->protocolVersion(),
$request->headers(),
$request->body(),
));
try {
return ($this->fulfill)(new Request\Request(
$this
->url
->withPath($request->url()->path())
->withQuery($request->url()->query()),
$request->method(),
$request->protocolVersion(),
$request->headers(),
$request->body(),
));
} catch (ClientError $e) {
if ($e->response()->statusCode()->equals(StatusCode::of('UNAUTHORIZED'))) {
throw new InvalidToken('', 0, $e);
}

if ($e->response()->statusCode()->equals(StatusCode::of('FORBIDDEN'))) {
throw new InvalidUserToken('', 0, $e);
}

throw $e;
}
}
}
3 changes: 0 additions & 3 deletions tests/SDK/Catalog/CatalogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,9 +1074,6 @@ public function testSearch()
)
->take(100)
->then(function($storefront, $term) {
$term = json_encode($term);
$term = substr($term, 1, -1);

$catalog = new Catalog(
$this->createMock(Clock::class),
$fulfill = $this->createMock(Transport::class),
Expand Down
64 changes: 62 additions & 2 deletions tests/SDK/HttpTransport/AppleMusicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

namespace Tests\MusicCompanion\AppleMusic\SDK\HttpTransport;

use MusicCompanion\AppleMusic\SDK\HttpTransport\AppleMusic;
use MusicCompanion\AppleMusic\{
SDK\HttpTransport\AppleMusic,
Exception\InvalidToken,
Exception\InvalidUserToken,
};
use Innmind\HttpTransport\{
Transport,
Exception\ClientError,
Expand Down Expand Up @@ -71,7 +75,7 @@ public function testThrowOnClientError()
$this
->forAll(
Url::any(),
Set\Integers::between(400, 418)
Set\Elements::of(400, 402, ...\range(404, 418)),
)
->then(function($url, $statusCode) {
$fulfill = new AppleMusic(
Expand All @@ -97,6 +101,62 @@ public function testThrowOnClientError()
});
}

public function testThrowWhenInvalidToken()
{
$this
->forAll(Url::any())
->then(function($url) {
$fulfill = new AppleMusic(
$inner = $this->createMock(Transport::class)
);
$initial = new Request(
$url,
Method::get(),
new ProtocolVersion(2, 0)
);
$inner
->expects($this->once())
->method('__invoke')
->willReturn($response = $this->createMock(Response::class));
$response
->expects($this->any())
->method('statusCode')
->willReturn(new StatusCode(401));

$this->expectException(InvalidToken::class);

$fulfill($initial);
});
}

public function testThrowWhenInvalidUserToken()
{
$this
->forAll(Url::any())
->then(function($url) {
$fulfill = new AppleMusic(
$inner = $this->createMock(Transport::class)
);
$initial = new Request(
$url,
Method::get(),
new ProtocolVersion(2, 0)
);
$inner
->expects($this->once())
->method('__invoke')
->willReturn($response = $this->createMock(Response::class));
$response
->expects($this->any())
->method('statusCode')
->willReturn(new StatusCode(403));

$this->expectException(InvalidUserToken::class);

$fulfill($initial);
});
}

public function testThrowOnServerError()
{
$this
Expand Down

0 comments on commit 63717b9

Please sign in to comment.