1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests\Accessor; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Gandung\JWT\Accessor\PayloadAccessor; |
7
|
|
|
use Gandung\JWT\Token\PayloadBuilderInterface; |
8
|
|
|
use Gandung\JWT\JWTFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class PayloadAccessorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var PayloadBuilderInterface |
17
|
|
|
*/ |
18
|
|
|
private $payload; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
parent::__construct(); |
23
|
|
|
$claim = JWTFactory::getClaimBuilder() |
|
|
|
|
24
|
|
|
->issuedBy('me') |
25
|
|
|
->expireAt(new \DateTimeImmutable('@' . (time() + 3600))); |
26
|
|
|
$this->payload = JWTFactory::getPayloadBuilder() |
27
|
|
|
->claim($claim) |
28
|
|
|
->userData([ |
29
|
|
|
'credentials' => [ |
30
|
|
|
'username' => 'me', |
31
|
|
|
'password' => 'this_is_only_me_will_know' |
32
|
|
|
] |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testCanGetInstance() |
37
|
|
|
{ |
38
|
|
|
$this->assertInstanceOf(PayloadAccessor::class, new PayloadAccessor($this->payload->getValue())); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCanGetIssuedByDirective() |
42
|
|
|
{ |
43
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
44
|
|
|
$this->assertInternalType('string', $accessor->getIssuedBy()); |
45
|
|
|
$this->assertEquals('me', $accessor->getIssuedBy()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException \RuntimeException |
50
|
|
|
*/ |
51
|
|
|
public function testCanGetRelatedToDirective() |
52
|
|
|
{ |
53
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
54
|
|
|
$this->assertInternalType('string', $accessor->getRelatedTo()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @expectedException \RuntimeException |
59
|
|
|
*/ |
60
|
|
|
public function testCanGetIntendedForDirective() |
61
|
|
|
{ |
62
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
63
|
|
|
$this->assertInternalType('string', $accessor->getIntendedFor()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testCanGetExpireAtDirective() |
67
|
|
|
{ |
68
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
69
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $accessor->getExpireAt()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @expectedException \RuntimeException |
74
|
|
|
*/ |
75
|
|
|
public function testCanGetCanOnlyBeUsedAfterDirective() |
76
|
|
|
{ |
77
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
78
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $accessor->getCanOnlyBeUsedAfter()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @expectedException \RuntimeException |
83
|
|
|
*/ |
84
|
|
|
public function testCanGetIssuedAtDirective() |
85
|
|
|
{ |
86
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
87
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $accessor->getIssuedAt()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @expectedException \RuntimeException |
92
|
|
|
*/ |
93
|
|
|
public function testCanGetIdentifiedByDirective() |
94
|
|
|
{ |
95
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
96
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $accessor->getIdentifiedBy()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testCanGetAll() |
100
|
|
|
{ |
101
|
|
|
$accessor = new PayloadAccessor($this->payload->getValue()); |
102
|
|
|
$this->assertInternalType('array', $accessor->get()); |
103
|
|
|
$this->assertNotEmpty($accessor->get()); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.