Completed
Pull Request — master (#14)
by
unknown
07:30
created

testSimpleCacheTokenPersistenceInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Eljam\GuzzleJwt\Tests\Persistence;
4
5
use Eljam\GuzzleJwt\JwtToken;
6
use Eljam\GuzzleJwt\Persistence\NullTokenPersistence;
7
use Eljam\GuzzleJwt\Persistence\SimpleCacheTokenPersistence;
8
use Kodus\Cache\MockCache;
9
use Psr\SimpleCache\CacheInterface;
10
11
/**
12
 * @author Nicolas Reynis (nreynis)
13
 */
14
class TokenPersistenceTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * testNullTokenPersistence.
18
     */
19
    public function testNullTokenPersistence()
20
    {
21
        $tokenPersistence = new NullTokenPersistence();
22
        $token = new JwtToken('foo', new \DateTime('now'));
23
24
        $tokenPersistence->saveToken($token);
0 ignored issues
show
Unused Code introduced by
The call to the method Eljam\GuzzleJwt\Persiste...ersistence::saveToken() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
25
26
        $this->assertFalse($tokenPersistence->hasToken());
27
        $this->assertNull($tokenPersistence->restoreToken());
28
    }
29
30
    /**
31
     * testSimpleCacheTokenPersistenceInterface.
32
     * Makes sure we only use the interface methods.
33
     */
34
    public function testSimpleCacheTokenPersistenceInterface()
35
    {
36
        $simpleCache = $this->getMock(CacheInterface::class);
37
        $tokenPersistence = new SimpleCacheTokenPersistence($simpleCache);
38
        $token = new JwtToken('foo', new \DateTime('now'));
39
40
        $this->assertNull($tokenPersistence->saveToken($token));
41
        $this->assertNull($tokenPersistence->hasToken());
42
        $this->assertNull($tokenPersistence->restoreToken());
43
        $this->assertNull($tokenPersistence->deleteToken());
44
    }
45
46
    /**
47
     * testSimpleCacheTokenPersistence.
48
     */
49
    public function testSimpleCacheTokenPersistence()
50
    {
51
        $simpleCache = new MockCache();
52
        $tokenPersistence = new SimpleCacheTokenPersistence($simpleCache);
53
        $token = new JwtToken('foo', new \DateTime('now'));
54
55
        $tokenPersistence->saveToken($token);
56
57
        $this->assertTrue($tokenPersistence->hasToken());
58
        $this->assertEquals($tokenPersistence->restoreToken()->getToken(), $token->getToken());
59
60
        $tokenPersistence->deleteToken();
61
62
        $this->assertFalse($tokenPersistence->hasToken());
63
        $this->assertNull($tokenPersistence->restoreToken());
64
    }
65
}
66