1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests\Token; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Gandung\JWT\Token\ClaimBuilder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class ClaimBuilderTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private function callCollectClaimDirectly($name, $value) |
14
|
|
|
{ |
15
|
|
|
$refl = new \ReflectionClass(ClaimBuilder::class); |
16
|
|
|
$collector = $refl->getMethod('collectClaim'); |
17
|
|
|
$collector->setAccessible(true); |
18
|
|
|
$instance = $collector->invokeArgs($refl->newInstanceWithoutConstructor(), [$name, $value]); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testCanGetInstance() |
22
|
|
|
{ |
23
|
|
|
$this->assertInstanceOf(ClaimBuilder::class, new ClaimBuilder); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testCanBuildJWTClaimAndGetItAfterwards() |
27
|
|
|
{ |
28
|
|
|
$builder = (new ClaimBuilder) |
29
|
|
|
->issuedBy('Paulus Gandung Prakosa') |
30
|
|
|
->relatedTo('something') |
31
|
|
|
->intendedFor('everybody') |
32
|
|
|
->expireAt(new \DateTimeImmutable('@' . (time() + (3600 * 3)))) |
33
|
|
|
->canOnlyBeUsedAfter(new \DateTimeImmutable('@' . (time() - (3600 * 3)))) |
34
|
|
|
->issuedAt(new \DateTimeImmutable('@' . (time() - (3600 * 24)))) |
35
|
|
|
->identifiedBy(md5(uniqid())); |
36
|
|
|
$aggregated = $builder->getValue(); |
37
|
|
|
$this->assertInternalType('array', $aggregated); |
38
|
|
|
$this->assertNotEmpty($aggregated); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @expectedException \Gandung\JWT\Exception\ClaimTokenMismatchException |
43
|
|
|
*/ |
44
|
|
|
public function testCanRaiseExceptionWhileStoreValueInWrongClaimName() |
45
|
|
|
{ |
46
|
|
|
$this->callCollectClaimDirectly('shit', 'this_is_truly_a_shit'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.