-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathLicenseManagerTest.php
More file actions
198 lines (165 loc) · 6.71 KB
/
LicenseManagerTest.php
File metadata and controls
198 lines (165 loc) · 6.71 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Tests\Licensing;
use Illuminate\Contracts\Support\MessageBag;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Licensing\AddonLicense;
use Statamic\Licensing\LicenseManager;
use Statamic\Licensing\Outpost;
use Statamic\Licensing\SiteLicense;
use Statamic\Licensing\StatamicLicense;
use Tests\TestCase;
class LicenseManagerTest extends TestCase
{
#[Test]
public function it_gets_the_outpost_response()
{
$manager = $this->managerWithResponse(['the' => 'response']);
$this->assertEquals(['the' => 'response'], $manager->response());
}
#[Test]
public function it_clears_the_outpost_response()
{
$outpost = $this->mock(Outpost::class);
$outpost->shouldReceive('clearCachedResponse')->once();
(new LicenseManager($outpost))->refresh();
}
#[Test]
public function it_checks_for_public_domains()
{
$this->assertTrue($this->managerWithResponse(['public' => true])->isOnPublicDomain());
$this->assertFalse($this->managerWithResponse(['public' => false])->isOnPublicDomain());
}
#[Test]
public function it_checks_for_test_domains()
{
$this->assertFalse($this->managerWithResponse(['public' => true])->isOnTestDomain());
$this->assertTrue($this->managerWithResponse(['public' => false])->isOnTestDomain());
}
#[Test]
public function licenses_are_valid_if_statamic_and_all_addons_are_valid()
{
$licenses = $this->managerWithResponse([
'statamic' => ['valid' => true],
'packages' => [
'foo/bar' => ['valid' => true],
'baz/qux' => ['valid' => true],
],
]);
$this->assertTrue($licenses->valid());
$this->assertFalse($licenses->invalid());
}
#[Test]
public function licenses_are_invalid_if_statamic_is_invalid_but_addons_are_valid()
{
$licenses = $this->managerWithResponse([
'statamic' => ['valid' => false],
'packages' => [
'foo/bar' => ['valid' => true],
'baz/qux' => ['valid' => true],
],
]);
$this->assertFalse($licenses->valid());
$this->assertTrue($licenses->invalid());
}
#[Test]
public function licenses_are_invalid_if_statamic_is_valid_but_any_addons_are_invalid()
{
$licenses = $this->managerWithResponse([
'statamic' => ['valid' => true],
'packages' => [
'foo/bar' => ['valid' => true],
'baz/qux' => ['valid' => false],
],
]);
$this->assertFalse($licenses->valid());
$this->assertTrue($licenses->invalid());
}
#[Test]
public function it_gets_the_site_license()
{
$licenses = $this->managerWithResponse(['site' => 'test-response']);
$site = $licenses->site();
$this->assertInstanceOf(SiteLicense::class, $site);
$this->assertEquals('test-response', $site->response());
}
#[Test]
public function it_gets_the_statamic_license()
{
$licenses = $this->managerWithResponse(['statamic' => 'test-response']);
$statamic = $licenses->statamic();
$this->assertInstanceOf(StatamicLicense::class, $statamic);
$this->assertEquals('test-response', $statamic->response());
}
#[Test]
public function it_gets_the_addon_licenses()
{
$licenses = $this->managerWithResponse([
'packages' => [
'foo/bar' => 'the foo/bar response',
'baz/qux' => 'the baz/qux response',
],
]);
$addons = $licenses->addons();
$this->assertInstanceOf(Collection::class, $addons);
$this->assertEveryItemIsInstanceOf(AddonLicense::class, $addons);
$this->assertEquals(['foo/bar', 'baz/qux'], $addons->keys()->all());
$this->assertEquals('the foo/bar response', $addons['foo/bar']->response());
$this->assertEquals('the baz/qux response', $addons['baz/qux']->response());
}
#[Test]
public function it_checks_if_statamic_license_needs_renewal()
{
$this->assertFalse($this->managerWithResponse([
'statamic' => ['valid' => true],
])->statamicNeedsRenewal());
$this->assertFalse($this->managerWithResponse([
'statamic' => ['valid' => false, 'reason' => 'unlicensed'],
])->statamicNeedsRenewal());
$this->assertTrue($this->managerWithResponse([
'statamic' => ['valid' => false, 'reason' => 'outside_license_range'],
])->statamicNeedsRenewal());
}
#[Test]
public function it_checks_for_request_failures()
{
Carbon::setTestNow(now()->startOfMinute());
tap($this->managerWithResponse(['error' => 500]), function ($licenses) {
$this->assertTrue($licenses->requestFailed());
$this->assertEquals(500, $licenses->requestErrorCode());
$this->assertFalse($licenses->requestRateLimited());
$this->assertNull($licenses->failedRequestRetrySeconds());
$this->assertInstanceOf(MessageBag::class, $licenses->requestValidationErrors());
$this->assertEquals([], $licenses->requestValidationErrors()->all());
});
tap($this->managerWithResponse([
'error' => 422,
'errors' => ['foo' => ['one'], 'bar' => ['two']],
]), function ($licenses) {
$this->assertTrue($licenses->requestFailed());
$this->assertEquals(422, $licenses->requestErrorCode());
$this->assertFalse($licenses->requestRateLimited());
$this->assertNull($licenses->failedRequestRetrySeconds());
$this->assertInstanceOf(MessageBag::class, $licenses->requestValidationErrors());
$this->assertEquals(['one', 'two'], $licenses->requestValidationErrors()->all());
});
tap($this->managerWithResponse([
'error' => 429,
'expiry' => now()->addSeconds(10)->timestamp,
]), function ($licenses) {
$this->assertTrue($licenses->requestFailed());
$this->assertEquals(429, $licenses->requestErrorCode());
$this->assertTrue($licenses->requestRateLimited());
$this->assertEquals(10, $licenses->failedRequestRetrySeconds());
$this->assertInstanceOf(MessageBag::class, $licenses->requestValidationErrors());
$this->assertEquals([], $licenses->requestValidationErrors()->all());
});
}
private function managerWithResponse(array $response)
{
$outpost = $this->mock(Outpost::class);
$outpost->shouldReceive('response')->andReturn($response);
return new LicenseManager($outpost);
}
}