Passed
Push — master ( 298035...f88e8a )
by Vince
06:31
created

AuthTest::testJWTIsNotValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php declare(strict_types=1);
2
3
use PHPUnit\Framework\TestCase;
4
use responsible\responsible;
5
use responsible\core\auth;
6
use responsible\core\headers;
7
use responsible\core\exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
final class AuthTest extends TestCase
10
{
11
    private $options;
12
13
    private $requestTime;
14
15
    private const SECRET_KEY = 'mock-key';
16
17
    private const TOKEN = 'abc-123';
18
19
    public function setUp()
20
    {
21
        $apiOptions = new options;
22
        $this->options = $apiOptions->getApiOptions();
23
        $this->requestTime = $_SERVER['REQUEST_TIME'];
24
    }
25
26
    /**
27
     * @test Test if JWT is valid
28
     */
29
    public function testJWTCanEncodeAndDecode(): void
30
    {
31
        $jwt = new auth\jwt;
32
33
        $payload = [
34
            'iss' => 'http://localhost',
35
            'iat' => $this->requestTime,
36
            'exp' => $this->requestTime,
37
            'nbf' => $this->requestTime,
38
        ];
39
40
        $encoded = $jwt->key(self::SECRET_KEY)
41
            ->setOptions($this->options)
42
            ->setPayload($payload)
0 ignored issues
show
Bug introduced by
$payload of type array<string,mixed|string> is incompatible with the type string expected by parameter $payload of responsible\core\auth\jwt::setPayload(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            ->setPayload(/** @scrutinizer ignore-type */ $payload)
Loading history...
43
            ->encode($payload)
0 ignored issues
show
Unused Code introduced by
The call to responsible\core\auth\jwt::encode() has too many arguments starting with $payload. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            ->/** @scrutinizer ignore-call */ encode($payload)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
44
        ;
45
46
        $this->assertEquals(true, is_string($encoded));
47
48
        $decoded = $jwt
49
            ->setOptions($this->options)
50
            ->token($encoded)
51
            ->key(self::SECRET_KEY)
52
            ->decode()
53
        ;
54
55
        $this->assertEquals(true, (is_array($decoded)&&!empty($decoded)) );
56
    }
57
58
    /**
59
     * @test Test if JWT is not valid and throws an exception
60
     */
61
    public function testJWTIsNotValid(): void
62
    {
63
        $jwt = new auth\jwt;
64
65
        $this->expectException(exception\errorException::class);
66
67
        $decoded = $jwt
0 ignored issues
show
Unused Code introduced by
The assignment to $decoded is dead and can be removed.
Loading history...
68
                ->setOptions($this->options)
69
                ->token(self::TOKEN)
70
                ->key(self::SECRET_KEY)
71
                ->decode()
72
            ;
73
    }
74
}