This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ResponseTraitTest.php
125 lines (112 loc) · 3.45 KB
/
ResponseTraitTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Firehed\U2F;
/**
* @coversDefaultClass Firehed\U2F\ResponseTrait
* @covers ::<protected>
* @covers ::<private>
*/
class ResponseTraitTest extends \PHPUnit\Framework\TestCase
{
private $trait;
public function setUp(): void
{
$this->trait = new class {
use ResponseTrait;
protected function parseResponse(array $response): self
{
$this->setSignature($response['signature']);
return $this;
}
};
}
/**
* @covers ::fromJson
* @covers ::getSignature
* @covers ::getChallengeProvider
* @covers ::getClientData
*/
public function testValidJson()
{
$signature = __METHOD__;
$json = json_encode([
'clientData' => 'eyJ0eXAiOiJuYXZpZ2F0b3IuaWQuZ2V0QXNzZXJ0aW9uIiwi'.
'Y2hhbGxlbmdlIjoid3QyemU4SXNrY1RPM25Jc08yRDJoRmpFNXRWRDA0MU5w'.
'blllc0xwSndlZyIsIm9yaWdpbiI6Imh0dHBzOi8vdTJmLmVyaWNzdGVybi5j'.
'b20iLCJjaWRfcHVia2V5IjoiIn0',
'signature' => $signature,
]);
$response = $this->trait::fromJson($json);
// This is a little goofy because it's an anonymous class, but seems
// preferable to declaring a one-off class in the test to implement the
// trait instead.
$this->assertInstanceOf(
get_class($this->trait),
$response,
'Parsed response was the wrong type'
);
$this->assertInstanceOf(
ClientData::class,
$response->getClientData(),
'ClientData was not parsed correctly'
);
$this->assertInstanceOf(
ChallengeProvider::class,
$response->getChallengeProvider(),
'Did not get a challenge provider'
);
$this->assertSame(
__METHOD__,
$response->getSignature(),
'Signature was not parsed correctly'
);
}
/**
* @covers ::fromJson
*/
public function testFromJsonWithNonJson()
{
$this->expectException(InvalidDataException::class);
$this->expectExceptionCode(InvalidDataException::MALFORMED_DATA);
$this->trait::fromJson('this is not json');
}
/**
* @covers ::fromJson
* @dataProvider clientErrors
*/
public function testErrorResponse(int $code)
{
$json = sprintf('{"errorCode":%d}', $code);
$this->expectException(ClientErrorException::class);
$this->expectExceptionCode($code);
$this->trait::fromJson($json);
}
/**
* @covers ::fromJson
* @dataProvider badClientData
*/
public function testClientDataValidation(string $json, int $code)
{
$this->expectException(InvalidDataException::class);
$this->expectExceptionCode($code);
$this->trait::fromJson($json);
}
// -( DataProviders )------------------------------------------------------
public function clientErrors()
{
return [
[ClientError::OTHER_ERROR],
[ClientError::BAD_REQUEST],
[ClientError::CONFIGURATION_UNSUPPORTED],
[ClientError::DEVICE_INELIGIBLE],
[ClientError::TIMEOUT],
];
}
public function badClientData()
{
return [
['{}', InvalidDataException::MISSING_KEY],
['{"clientData":25}', InvalidDataException::MALFORMED_DATA],
];
}
}