|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\OpenIDClientTest\Exception; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\OpenIDClient\Exception\ExceptionInterface; |
|
8
|
|
|
use Facile\OpenIDClient\Exception\InvalidArgumentException; |
|
9
|
|
|
use Facile\OpenIDClient\Exception\OAuth2Exception; |
|
10
|
|
|
use Facile\OpenIDClient\Exception\RemoteException; |
|
11
|
|
|
use function json_decode; |
|
12
|
|
|
use function json_encode; |
|
13
|
|
|
use Facile\OpenIDClientTest\TestCase; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\StreamInterface; |
|
16
|
|
|
|
|
17
|
|
|
class OAuth2ExceptionTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testFromResponse(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
|
22
|
|
|
$stream = $this->prophesize(StreamInterface::class); |
|
23
|
|
|
|
|
24
|
|
|
$response->getBody()->willReturn($stream->reveal()); |
|
25
|
|
|
$response->getReasonPhrase()->willReturn('Bad request'); |
|
26
|
|
|
$response->getStatusCode()->willReturn(400); |
|
27
|
|
|
|
|
28
|
|
|
$stream->__toString()->willReturn('{"error": "error_code"}'); |
|
29
|
|
|
|
|
30
|
|
|
$exception = OAuth2Exception::fromResponse($response->reveal()); |
|
31
|
|
|
|
|
32
|
|
|
static::assertInstanceOf(ExceptionInterface::class, $exception); |
|
33
|
|
|
static::assertSame('error_code', $exception->getMessage()); |
|
34
|
|
|
static::assertSame('error_code', $exception->getError()); |
|
35
|
|
|
static::assertNull($exception->getDescription()); |
|
36
|
|
|
static::assertNull($exception->getErrorUri()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testFromResponseComplete(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
|
42
|
|
|
$stream = $this->prophesize(StreamInterface::class); |
|
43
|
|
|
|
|
44
|
|
|
$response->getBody()->willReturn($stream->reveal()); |
|
45
|
|
|
$response->getReasonPhrase()->willReturn('Bad request'); |
|
46
|
|
|
$response->getStatusCode()->willReturn(400); |
|
47
|
|
|
|
|
48
|
|
|
$stream->__toString()->willReturn('{"error": "error_code","error_description":"Error message","error_uri":"uri"}'); |
|
49
|
|
|
|
|
50
|
|
|
$exception = OAuth2Exception::fromResponse($response->reveal()); |
|
51
|
|
|
|
|
52
|
|
|
static::assertInstanceOf(ExceptionInterface::class, $exception); |
|
53
|
|
|
static::assertSame('Error message (error_code)', $exception->getMessage()); |
|
54
|
|
|
static::assertSame('error_code', $exception->getError()); |
|
55
|
|
|
static::assertSame('Error message', $exception->getDescription()); |
|
56
|
|
|
static::assertSame('uri', $exception->getErrorUri()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testFromResponseNoOAuthError(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->expectException(RemoteException::class); |
|
62
|
|
|
|
|
63
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
|
64
|
|
|
$stream = $this->prophesize(StreamInterface::class); |
|
65
|
|
|
|
|
66
|
|
|
$response->getBody()->willReturn($stream->reveal()); |
|
67
|
|
|
$response->getReasonPhrase()->willReturn('Bad request'); |
|
68
|
|
|
$response->getStatusCode()->willReturn(400); |
|
69
|
|
|
|
|
70
|
|
|
$stream->__toString()->willReturn(''); |
|
71
|
|
|
|
|
72
|
|
|
OAuth2Exception::fromResponse($response->reveal()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testFromParameters(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$exception = OAuth2Exception::fromParameters([ |
|
78
|
|
|
'error' => 'error_code', |
|
79
|
|
|
'error_description' => 'Error message', |
|
80
|
|
|
'error_uri' => 'uri', |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
static::assertInstanceOf(ExceptionInterface::class, $exception); |
|
84
|
|
|
static::assertSame('Error message (error_code)', $exception->getMessage()); |
|
85
|
|
|
static::assertSame('error_code', $exception->getError()); |
|
86
|
|
|
static::assertSame('Error message', $exception->getDescription()); |
|
87
|
|
|
static::assertSame('uri', $exception->getErrorUri()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testFromInvalidParameters(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
93
|
|
|
|
|
94
|
|
|
OAuth2Exception::fromParameters([ |
|
95
|
|
|
'error_description' => 'Error message', |
|
96
|
|
|
'error_uri' => 'uri', |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testJsonSerializer(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$params = [ |
|
103
|
|
|
'error' => 'error_code', |
|
104
|
|
|
'error_description' => 'Error message', |
|
105
|
|
|
'error_uri' => 'uri', |
|
106
|
|
|
]; |
|
107
|
|
|
$exception = OAuth2Exception::fromParameters($params); |
|
108
|
|
|
|
|
109
|
|
|
static::assertInstanceOf(ExceptionInterface::class, $exception); |
|
110
|
|
|
static::assertSame($params, json_decode(json_encode($exception), true)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|