NonceServiceTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 145
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testCreateNonce() 0 14 1
A testCreateWithDefaultExpirationDate() 0 10 1
A testConsumeNonce() 0 12 1
A testConsumeNonceWithExpirationDate() 0 12 1
A testConsumeExpiredNonceWillThrowException() 0 9 1
A testCreateNonceWithNamespace() 0 7 1
A testFindNonExistentNonceWillReturnNull() 0 4 1
A testFindNonceWithParamsWillInvokeTheMapperFindMethodWithSameParams() 0 15 1
A testIsValid() 0 4 1
A isValidProvider() 0 8 1
A testCannotConsumeNonceTwice() 0 9 1
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\Nonce\Test\Service;
9
10
use DateInterval;
11
use DateTime;
12
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13
use PHPUnit_Framework_TestCase as TestCase;
14
use Ramsey\Uuid\Uuid;
15
use Thorr\Nonce\DataMapper\NonceMapperInterface;
16
use Thorr\Nonce\Entity\Nonce;
17
use Thorr\Nonce\Entity\NonceOwnerInterface;
18
use Thorr\Nonce\Options\ModuleOptions;
19
use Thorr\Nonce\Service\Exception\NonceHasExpiredException;
20
use Thorr\Nonce\Service\NonceService;
21
22
class NonceServiceTest extends TestCase
23
{
24
    /**
25
     * @var NonceMapperInterface|MockObject
26
     */
27
    protected $nonceMapper;
28
29
    /**
30
     * @var ModuleOptions
31
     */
32
    protected $moduleOptions;
33
34
    /**
35
     * @var NonceService
36
     */
37
    protected $nonceService;
38
39
    public function setUp()
40
    {
41
        $this->nonceMapper   = $this->getMock(NonceMapperInterface::class);
42
        $this->moduleOptions = new ModuleOptions();
43
        $this->nonceService  = new NonceService($this->nonceMapper, $this->moduleOptions);
44
    }
45
46
    public function testCreateNonce()
47
    {
48
        $owner          = $this->getMock(NonceOwnerInterface::class);
49
        $expirationDate = new DateTime();
50
51
        $this->nonceMapper->expects($this->atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Thorr\Nonce\DataMapper\NonceMapperInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
52
                          ->method('save');
53
54
        $nonce = $this->nonceService->createNonce($owner, $expirationDate);
55
56
        $this->assertInstanceOf(Nonce::class, $nonce);
57
        $this->assertSame($expirationDate, $nonce->getExpirationDate());
58
        $this->assertSame($owner, $nonce->getOwner());
59
    }
60
61
    public function testCreateWithDefaultExpirationDate()
62
    {
63
        $owner        = $this->getMock(NonceOwnerInterface::class);
64
        $nonce        = $this->nonceService->createNonce($owner);
65
        $expectedDate = (new DateTime())->add(new DateInterval($this->moduleOptions->getDefaultNonceExpirationInterval()));
66
67
        $this->assertInstanceOf(Nonce::class, $nonce);
68
        $this->assertNotNull($nonce->getExpirationDate());
69
        $this->assertTrue($expectedDate >= $nonce->getExpirationDate());
70
    }
71
72
    public function testConsumeNonce()
73
    {
74
        $nonce = new Nonce();
75
76
        $result = $this->nonceService->consumeNonce($nonce);
77
78
        $now   = new DateTime();
79
80
        $this->assertTrue($result);
81
        $this->assertNotNull($nonce->getExpirationDate());
82
        $this->assertTrue($now >= $nonce->getExpirationDate());
83
    }
84
85
    public function testConsumeNonceWithExpirationDate()
86
    {
87
        $owner = $this->getMock(NonceOwnerInterface::class);
88
        $nonce = $this->nonceService->createNonce($owner);
89
90
        $result = $this->nonceService->consumeNonce($nonce);
91
92
        $now   = new DateTime();
93
94
        $this->assertTrue($result);
95
        $this->assertTrue($now >= $nonce->getExpirationDate());
96
    }
97
98
    public function testConsumeExpiredNonceWillThrowException()
99
    {
100
        $expirationDate = (new DateTime())->sub(new DateInterval('PT1S'));
101
        $nonce          = new Nonce(null, $expirationDate);
102
103
        $this->setExpectedException(NonceHasExpiredException::class);
104
105
        $this->nonceService->consumeNonce($nonce);
106
    }
107
108
    public function testCreateNonceWithNamespace()
109
    {
110
        $owner = $this->getMock(NonceOwnerInterface::class);
111
        $nonce = $this->nonceService->createNonce($owner, null, 'foobar');
112
113
        $this->assertSame('foobar', $nonce->getNamespace());
114
    }
115
116
    public function testFindNonExistentNonceWillReturnNull()
117
    {
118
        $this->assertNull($this->nonceService->findNonce('foobar'));
119
    }
120
121
    public function testFindNonceWithParamsWillInvokeTheMapperFindMethodWithSameParams()
122
    {
123
        $uuid      = Uuid::uuid4();
124
        $namespace = 'foobar';
125
        $nonce     = new Nonce();
126
127
        $this->nonceMapper
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Thorr\Nonce\DataMapper\NonceMapperInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
            ->expects($this->atLeastOnce())
129
            ->method('find')
130
            ->with($uuid, $namespace)
131
            ->willReturn($nonce)
132
        ;
133
134
        $this->assertSame($nonce, $this->nonceService->findNonce($uuid, $namespace));
135
    }
136
137
    /**
138
     * @param Nonce $nonce
139
     * @param bool  $result
140
     *
141
     * @dataProvider isValidProvider
142
     */
143
    public function testIsValid(Nonce $nonce, $result)
144
    {
145
        $this->assertSame($result, $this->nonceService->isValid($nonce));
146
    }
147
148
    public function isValidProvider()
149
    {
150
        return [
151
            [ new Nonce(), true ],
152
            [ new Nonce(null, (new DateTime())->sub(new DateInterval('PT1M'))), false ],
153
            [ new Nonce(null, (new DateTime())->add(new DateInterval('PT1M'))), true ],
154
        ];
155
    }
156
157
    public function testCannotConsumeNonceTwice()
158
    {
159
        $nonce = new Nonce();
160
161
        $this->assertTrue($this->nonceService->consumeNonce($nonce));
162
163
        $this->setExpectedException(NonceHasExpiredException::class);
164
        $this->nonceService->consumeNonce($nonce);
165
    }
166
}
167