CacheListenerTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testAttach() 0 17 1
B testCacheAction() 0 31 1
B testNotCacheableAction() 0 30 1
1
<?php
2
3
namespace PamiModuleTest\Listener;
4
5
use PAMI\Message\Response\ResponseMessage;
6
use PamiModule\Listener\CacheListener;
7
use Zend\Cache\Storage\Adapter\Memory;
8
use Zend\EventManager\Event;
9
10
class CacheListenerTest extends \PHPUnit_Framework_TestCase
11
{
12
    public function testAttach()
13
    {
14
        $events = $this->getMockBuilder('Zend\\EventManager\\EventManagerInterface')->getMock();
15
        $cache = $this->getMockBuilder('Zend\\Cache\\Storage\\StorageInterface')->getMock();
16
17
        $events->expects(static::exactly(2))
18
            ->method('attach')
19
            ->withConsecutive(
20
                ['sendAction.pre'],
21
                ['sendAction.post']
22
            );
23
24
        $listener = new CacheListener($cache, ['Foo']);
25
        $listener->attach($events);
26
        static::assertSame($cache, $listener->getCache());
27
        static::assertEquals(['foo'], $listener->getCacheableActions());
28
    }
29
30
    public function testCacheAction()
31
    {
32
        $actionsToCache = ['Foo'];
33
        $client = $this->getMockBuilder('PamiModule\\Service\\Client')
34
            ->setMethods(['getHost'])
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
        $client->expects(static::atLeast(1))->method('getHost')->willReturn('foo.com');
38
39
        $action = $this->getMockBuilder('PAMI\\Message\\OutgoingMessage')
40
            ->disableOriginalConstructor()
41
            ->getMock();
42
        $action->expects(static::any())->method('getKey')->with('Action')->willReturn('Foo');
43
        $action->expects(static::any())->method('getKeys')->willReturn(['foo' => 'bar', 'bar' => 'foo']);
44
45
        $cache = new Memory();
46
47
        $listener = new CacheListener($cache, $actionsToCache);
48
49
        $event = new Event('sendAction.pre', $client, ['action' => $action]);
50
        $ret = $listener->onSendPre($event);
51
        static::assertNull($ret);
52
53
        $response = new ResponseMessage("Event: Response\r\nResponse: Success\r\nFoo: Bar");
54
        $event = new Event('sendAction.pre', $client, ['action' => $action, 'response' => $response]);
55
        $listener->onSendPost($event);
56
57
        // Requesting again, asserting the response is the previous cached
58
        $ret = $listener->onSendPre($event);
59
        static::assertSame($response, $ret);
60
    }
61
62
    public function testNotCacheableAction()
63
    {
64
        $actionsToCache = [];
65
        $client = $this->getMockBuilder('PamiModule\\Service\\Client')
66
            ->disableOriginalConstructor()
67
            ->setMethods(['getHost'])
68
            ->getMock();
69
70
        $action = $this->getMockBuilder('PAMI\\Message\\OutgoingMessage')
71
            ->disableOriginalConstructor()
72
            ->getMock();
73
        $action->expects(static::any())->method('getKey')->with('Action')->willReturn('Bar');
74
        $action->expects(static::any())->method('getKeys')->willReturn(['foo' => 'bar', 'bar' => 'foo']);
75
76
        $cache = new Memory();
77
78
        $listener = new CacheListener($cache, $actionsToCache);
79
80
        $event = new Event('sendAction.pre', $client, ['action' => $action]);
81
        $ret = $listener->onSendPre($event);
82
        static::assertNull($ret);
83
84
        $response = new ResponseMessage("Event: Response\r\nResponse: Success\r\nFoo: Bar");
85
        $event = new Event('sendAction.pre', $client, ['action' => $action, 'response' => $response]);
86
        $listener->onSendPost($event);
87
88
        // Requesting again, asserting the response is the previous cached
89
        $ret = $listener->onSendPre($event);
90
        static::assertNull($ret);
91
    }
92
}
93