-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathInMemoryTokenCacheTest.php
55 lines (44 loc) · 1.78 KB
/
InMemoryTokenCacheTest.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
<?php
declare(strict_types=1);
namespace SellingPartnerApi\Tests;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use SellingPartnerApi\Authentication\AccessTokenAuthenticator;
use SellingPartnerApi\Authentication\InMemoryTokenCache;
use SellingPartnerApi\Contracts\TokenCache;
class InMemoryTokenCacheTest extends TestCase
{
private TokenCache $cache;
public function setup(): void
{
$this->cache = new InMemoryTokenCache;
}
public function testStoresToken(): void
{
$token = new AccessTokenAuthenticator('access-token', expiresAt: new DateTimeImmutable('+1 hour'));
$this->cache->set('token-1', $token);
$fetched = $this->cache->get('token-1');
$this->assertSame($token, $fetched);
}
public function testExpiresStoredToken(): void
{
$token = new AccessTokenAuthenticator('access-token', expiresAt: new DateTimeImmutable('-1 hour'));
$this->cache->set('token-1', $token);
$fetched = $this->cache->get('token-1');
$this->assertFalse($fetched);
}
public function testDeletesKey(): void
{
$this->cache->set('token-1', new AccessTokenAuthenticator('access-token', expiresAt: new DateTimeImmutable('+1 hour')));
$this->cache->forget('token-1');
$this->assertFalse($this->cache->get('token-1'));
}
public function testClearsCache(): void
{
$this->cache->set('token-1', new AccessTokenAuthenticator('access-token-1', expiresAt: new DateTimeImmutable('+1 hour')));
$this->cache->set('token-2', new AccessTokenAuthenticator('access-token-2', expiresAt: new DateTimeImmutable('+2 hours')));
$this->cache->clear();
$this->assertFalse($this->cache->get('token-1'));
$this->assertFalse($this->cache->get('token-2'));
}
}