CookieServiceTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 139
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testReadSerie() 0 20 1
A testNoSerieRead() 0 9 1
A testInvalidCookieNullifiesCookie() 0 23 1
A testWriteNull() 0 14 1
A testWriteSerie() 0 18 1
A testWithCustomCookieOptions() 0 28 1
1
<?php
2
3
namespace JwPersistentUserTest\Service;
4
5
use JwPersistentUser\Model\SerieToken;
6
use Zend\Http\Header\Cookie;
7
use Zend\Http\Request;
8
use Zend\Http\Response;
9
10
use JwPersistentUser\Test\TestCase;
11
use JwPersistentUser\Model\ModuleOptions;
12
use JwPersistentUser\Service\CookieService;
13
14
class CookieServiceTest extends TestCase
15
{
16
    /**
17
     * @var ModuleOptions
18
     */
19
    private $options;
20
21
    /**
22
     * @var CookieService
23
     */
24
    protected $service;
25
26
    public function setUp()
27
    {
28
        $this->service = new CookieService;
29
30
        $this->options = new ModuleOptions;
31
        $this->options->setSerieTokenEntityClass('JwPersistentUser\Model\SerieToken');
32
        $this->service->setModuleOptions($this->options);
33
    }
34
35
    public function testReadSerie()
36
    {
37
        $request = new Request;
38
        $cookies = new Cookie([
39
            'JwPersistentUser' => '1:abc:def'
40
        ]);
41
        $headers = $request->getHeaders();
42
        $headers->addHeader($cookies);
43
44
        $response = new Response;
45
46
        $result = $this->service->read($request, $response);
47
48
        $this->assertFalse($response->getHeaders()->has('Set-Cookie'));
49
50
        $this->assertNotNull($result);
51
        $this->assertEquals(1, $result->getUserId());
52
        $this->assertEquals('abc', $result->getSerie());
53
        $this->assertEquals('def', $result->getToken());
54
    }
55
56
    public function testNoSerieRead()
57
    {
58
        $request = new Request;
59
        $response = new Response;
60
61
        $result = $this->service->read($request, $response);
62
63
        $this->assertNull($result);
64
    }
65
66
    public function testInvalidCookieNullifiesCookie()
67
    {
68
        $request = new Request;
69
        $cookies = new Cookie([
70
            'JwPersistentUser' => '1:in-valid'
71
        ]);
72
        $headers = $request->getHeaders();
73
        $headers->addHeader($cookies);
74
75
        $response = new Response;
76
77
        $result = $this->service->read($request, $response);
78
79
        $this->assertNull($result);
80
81
        $this->assertTrue($response->getHeaders()->has('Set-Cookie'));
82
        $cookie = $response->getHeaders()->get('Set-Cookie')->current();
0 ignored issues
show
Bug introduced by
The method current does only exist in ArrayIterator, but not in Zend\Http\Header\HeaderInterface.

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...
83
        $this->assertInstanceOf('Zend\Http\Header\SetCookie', $cookie);
84
        $this->assertEquals('JwPersistentUser', $cookie->getName());
85
        $this->assertDateTimeEquals(new \DateTime('-1 hour'), new \DateTime($cookie->getExpires()));
86
        $this->assertEquals('/', $cookie->getPath());
87
        $this->assertTrue($cookie->isHttponly());
88
    }
89
90
    public function testWriteNull()
91
    {
92
        $response = new Response;
93
94
        $this->service->writeNull($response);
95
96
        $this->assertTrue($response->getHeaders()->has('Set-Cookie'));
97
        $cookie = $response->getHeaders()->get('Set-Cookie')->current();
0 ignored issues
show
Bug introduced by
The method current does only exist in ArrayIterator, but not in Zend\Http\Header\HeaderInterface.

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...
98
        $this->assertInstanceOf('Zend\Http\Header\SetCookie', $cookie);
99
        $this->assertEquals('JwPersistentUser', $cookie->getName());
100
        $this->assertDateTimeEquals(new \DateTime('-1 hour'), new \DateTime($cookie->getExpires()));
101
        $this->assertEquals('/', $cookie->getPath());
102
        $this->assertTrue($cookie->isHttponly());
103
    }
104
105
    public function testWriteSerie()
106
    {
107
        $serie = new SerieToken(3, 'abc', 'def');
108
        $serie->setExpiresAt(new \DateTime('+1 year'));
109
        $response = new Response;
110
111
        $this->service->writeSerie($response, $serie);
112
113
        $this->assertTrue($response->getHeaders()->has('Set-Cookie'));
114
        $cookie = $response->getHeaders()->get('Set-Cookie')->current();
0 ignored issues
show
Bug introduced by
The method current does only exist in ArrayIterator, but not in Zend\Http\Header\HeaderInterface.

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...
115
        $this->assertInstanceOf('Zend\Http\Header\SetCookie', $cookie);
116
        $this->assertEquals('JwPersistentUser', $cookie->getName());
117
        $this->assertEquals('3:abc:def', $cookie->getValue());
118
        $this->assertDateTimeEquals(new \DateTime('+1 year'), new \DateTime($cookie->getExpires()));
119
        $this->assertEquals('/', $cookie->getPath());
120
        $this->assertEquals(null, $cookie->getSameSite());
121
        $this->assertNull($cookie->isSecure());
122
    }
123
124
    public function testWithCustomCookieOptions()
125
    {
126
        $this->options->setCookiePath('/foo');
127
        $this->options->setCookieDomain('bla.nl');
128
        $this->options->setCookieHttpOnly(true);
129
        $this->options->setCookieSameSite('none');
130
        $this->options->setCookieSecure(true);
131
        $this->options->setCookieVersion(3);
132
        $this->options->setCookieMaxAge(300);
133
134
        $serie = new SerieToken(3, 'abc', 'def');
135
        $serie->setExpiresAt(new \DateTime('+1 year'));
136
        $response = new Response;
137
138
        $this->service->writeSerie($response, $serie);
139
140
        $this->assertTrue($response->getHeaders()->has('Set-Cookie'));
141
        $cookie = $response->getHeaders()->get('Set-Cookie')->current();
0 ignored issues
show
Bug introduced by
The method current does only exist in ArrayIterator, but not in Zend\Http\Header\HeaderInterface.

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...
142
        $this->assertInstanceOf('Zend\Http\Header\SetCookie', $cookie);
143
144
        $this->assertEquals('/foo', $cookie->getPath());
145
        $this->assertEquals('bla.nl', $cookie->getDomain());
146
        $this->assertTrue($cookie->isHttpOnly());
147
        $this->assertEquals('None', $cookie->getSameSite());
148
        $this->assertEquals(300, $cookie->getMaxAge());
149
        $this->assertEquals(3, $cookie->getVersion());
150
        $this->assertTrue($cookie->isSecure());
151
    }
152
}
153