WriteToCookieTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 1
A testDoesNotActOnNonHttpRequest() 0 9 1
A testDoesNotActOnNonHttpResponse() 0 9 1
A testGeneratesSerieToken() 0 19 1
A testLogout() 0 17 1
A methods() 0 7 1
1
<?php
2
3
namespace JwPersistentUserTest\Listener;
4
5
use JwPersistentUser\Test\TestCase;
6
use JwPersistentUser\Model\SerieToken;
7
use JwPersistentUser\Service\CookieService;
8
use JwPersistentUser\Service\RememberMeService;
9
use JwPersistentUser\Listener\WriteTokenToCookie;
10
11
use Zend\EventManager\Event;
12
use Zend\Http\Request;
13
use Zend\Http\Response;
14
15
use Zend\ServiceManager\ServiceManager;
16
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
17
18
class WriteToCookieTest extends TestCase
19
{
20
    /**
21
     * @var WriteTokenToCookie
22
     */
23
    protected $listener;
24
25
    /**
26
     * @var ServiceManager
27
     */
28
    protected $serviceManager;
29
30
    /**
31
     * @var RememberMeService
32
     */
33
    protected $rememberMeService;
34
35
    /**
36
     * @var CookieService
37
     */
38
    protected $cookieService;
39
40
    /**
41
     * @var Request
42
     */
43
    protected $request;
44
45
    /**
46
     * @var Response
47
     */
48
    protected $response;
49
50
    public function setUp()
51
    {
52
        parent::setUp();
53
54
        $this->request = new Request;
55
        $this->response = new Response;
56
        $this->rememberMeService = $this->getMock('JwPersistentUser\Service\RememberMeService');
57
        $this->cookieService = $this->getMock('JwPersistentUser\Service\CookieService');
58
59
        $this->serviceManager = new ServiceManager([
60
            'services' => [
61
                'JwPersistentUser\Service\RememberMe' => $this->rememberMeService,
62
                'Request' => $this->request,
63
                'Response' => $this->response,
64
                'JwPersistentUser\Service\Cookie' => $this->cookieService,
65
            ]
66
        ]);
67
        $this->serviceManager->setAllowOverride(true);
68
69
        $this->listener = new WriteTokenToCookie($this->serviceManager);
70
    }
71
72
    /**
73
     * @dataProvider methods
74
     */
75
    public function testDoesNotActOnNonHttpRequest($method)
76
    {
77
        $this->serviceManager->setService('Request', $this->getMock('Zend\StdLib\RequestInterface'));
78
79
        $this->rememberMeService->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\...vice\RememberMeService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            ->method('createNew');
81
82
        $this->listener->{$method}(new AdapterChainEvent);
83
    }
84
85
    /**
86
     * @dataProvider methods
87
     */
88
    public function testDoesNotActOnNonHttpResponse($method)
89
    {
90
        $this->serviceManager->setService('Response', $this->getMock('Zend\StdLib\ResponseInterface'));
91
92
        $this->rememberMeService->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\...vice\RememberMeService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
            ->method('createNew');
94
95
        $this->listener->{$method}(new AdapterChainEvent);
96
    }
97
98
    public function testGeneratesSerieToken()
99
    {
100
        $event = new AdapterChainEvent;
101
        $event->setIdentity(3);
102
103
        $returnToken = new SerieToken(3, 'abc', 'def');
104
        $returnToken->setExpiresAt(new \DateTime('+3 days'));
105
106
        $this->rememberMeService->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\...vice\RememberMeService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            ->method('createNew')
108
            ->with(3)
109
            ->will($this->returnValue($returnToken));
110
111
        $this->cookieService->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\Service\CookieService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
            ->method('writeSerie')
113
            ->with($this->response, $returnToken);
114
115
        $this->listener->authenticate($event);
116
    }
117
118
    public function testLogout()
119
    {
120
        $this->cookieService->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\Service\CookieService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
            ->method('read')
122
            ->with($this->request, $this->response)
123
            ->will($this->returnValue(new SerieToken(1, 'abc', 'def')));
124
125
        $this->rememberMeService->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\...vice\RememberMeService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
            ->method('removeSerie')
127
            ->with(1, 'abc');
128
129
        $this->cookieService->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<JwPersistentUser\Service\CookieService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            ->method('writeNull')
131
            ->with($this->response);
132
133
        $this->listener->logout(new AdapterChainEvent);
134
    }
135
136
    public function methods()
137
    {
138
        return [
139
            ['authenticate'],
140
            ['logout'],
141
        ];
142
    }
143
}
144