|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Rest\Exception; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Shlinkio\Shlink\Common\Util\StringUtilsTrait; |
|
9
|
|
|
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException; |
|
10
|
|
|
use Throwable; |
|
11
|
|
|
use function array_map; |
|
12
|
|
|
use function random_int; |
|
13
|
|
|
use function range; |
|
14
|
|
|
use function sprintf; |
|
15
|
|
|
|
|
16
|
|
|
class VerifyAuthenticationExceptionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
use StringUtilsTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
* @dataProvider provideExceptionData |
|
23
|
|
|
*/ |
|
24
|
|
|
public function withErrorCreatesExpectedException(string $code, string $message, ?Throwable $prev): void |
|
25
|
|
|
{ |
|
26
|
|
|
$e = VerifyAuthenticationException::withError($code, $message, $prev); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
29
|
|
|
$this->assertEquals( |
|
30
|
|
|
sprintf('Authentication verification failed with the public message "%s"', $message), |
|
31
|
|
|
$e->getMessage() |
|
32
|
|
|
); |
|
33
|
|
|
$this->assertEquals($code, $e->getErrorCode()); |
|
34
|
|
|
$this->assertEquals($message, $e->getPublicMessage()); |
|
35
|
|
|
$this->assertEquals($prev, $e->getPrevious()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function provideExceptionData(): iterable |
|
39
|
|
|
{ |
|
40
|
|
|
return array_map(function () { |
|
41
|
|
|
return [ |
|
42
|
|
|
$this->generateRandomString(), |
|
43
|
|
|
$this->generateRandomString(50), |
|
44
|
|
|
random_int(0, 1) === 1 ? new Exception('Prev') : null, |
|
45
|
|
|
]; |
|
46
|
|
|
}, range(1, 10)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
* @dataProvider provideConstructorData |
|
52
|
|
|
*/ |
|
53
|
|
|
public function constructCreatesExpectedException( |
|
54
|
|
|
string $errorCode, |
|
55
|
|
|
string $publicMessage, |
|
56
|
|
|
string $message, |
|
57
|
|
|
int $code, |
|
58
|
|
|
?Throwable $prev |
|
59
|
|
|
): void { |
|
60
|
|
|
$e = new VerifyAuthenticationException($errorCode, $publicMessage, $message, $code, $prev); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals($code, $e->getCode()); |
|
63
|
|
|
$this->assertEquals($message, $e->getMessage()); |
|
64
|
|
|
$this->assertEquals($errorCode, $e->getErrorCode()); |
|
65
|
|
|
$this->assertEquals($publicMessage, $e->getPublicMessage()); |
|
66
|
|
|
$this->assertEquals($prev, $e->getPrevious()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function provideConstructorData(): iterable |
|
70
|
|
|
{ |
|
71
|
|
|
return array_map(function (int $i) { |
|
72
|
|
|
return [ |
|
73
|
|
|
$this->generateRandomString(), |
|
74
|
|
|
$this->generateRandomString(30), |
|
75
|
|
|
$this->generateRandomString(50), |
|
76
|
|
|
$i, |
|
77
|
|
|
random_int(0, 1) === 1 ? new Exception('Prev') : null, |
|
78
|
|
|
]; |
|
79
|
|
|
}, range(10, 20)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @test */ |
|
83
|
|
|
public function defaultConstructorValuesAreKept(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$e = new VerifyAuthenticationException('foo', 'bar'); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals(0, $e->getCode()); |
|
88
|
|
|
$this->assertEquals('', $e->getMessage()); |
|
89
|
|
|
$this->assertEquals('foo', $e->getErrorCode()); |
|
90
|
|
|
$this->assertEquals('bar', $e->getPublicMessage()); |
|
91
|
|
|
$this->assertNull($e->getPrevious()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|