testEventStatusLinkResponseListener()   C
last analyzed

Complexity

Conditions 10
Paths 1

Size

Total Lines 154

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 154
rs 6.1333
c 0
b 0
f 0
cc 10
nc 1
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * functional test suite for the EventStatusLinkResponseListener
4
 */
5
6
namespace Graviton\RabbitMqBundle\Tests\Listener;
7
8
use Graviton\DocumentBundle\Entity\ExtReference;
9
use Graviton\RabbitMqBundle\Listener\EventStatusLinkResponseListener;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Class EventStatusLinkResponseListenerTest
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  https://opensource.org/licenses/MIT MIT License
17
 * @link     http://swisscom.ch
18
 */
19
class EventStatusLinkResponseListenerTest extends \PHPUnit\Framework\TestCase
20
{
21
    /**
22
     * Verifies the correct workflow of the ResponseListener
23
     *
24
     * @return void
25
     */
26
    public function testEventStatusLinkResponseListener()
27
    {
28
        $producerMock = $this->getMockBuilder(
29
            '\OldSound\RabbitMqBundle\RabbitMq\ProducerInterface'
30
        )->disableOriginalConstructor()->setMethods(['publish', 'getChannel'])->getMockForAbstractClass();
31
32
        $producerMock->expects($this->once())->method('publish')
33
            ->will(
34
                $this->returnCallback(
35
                    function ($message, $routingKey) {
36
                        $this->assertSame(
37
                            '{"event":"document.core.product.create","coreUserId":"",'.
38
                            '"document":{"$ref":"graviton-api-test\/core\/product'.
39
                            '"},"status":{"$ref":"http:\/\/graviton-test.lo\/worker\/123jkl890yui567mkl"}}',
40
                            $message
41
                        );
42
                        $this->assertSame(
43
                            'someWorkerId',
44
                            $routingKey
45
                        );
46
                    }
47
                )
48
            );
49
50
        $channelMock = $this->getMockBuilder(
51
            '\PhpAmqpLib\Channel\AMQPChannel'
52
        )->disableOriginalConstructor()->getMock();
53
54
        $producerMock->expects($this->once())->method('getChannel')
55
            ->willReturn($channelMock);
56
57
        $routerMock = $this->getMockBuilder('\Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor(
58
        )->setMethods(['generate'])->getMockForAbstractClass();
59
        $routerMock->expects($this->once())->method('generate')->willReturn(
60
            'http://graviton-test.lo/worker/123jkl890yui567mkl'
61
        );
62
63
        $securityMock = $this
64
            ->getMockBuilder('Graviton\SecurityBundle\Service\SecurityUtils')
65
            ->disableOriginalConstructor()
66
            ->setMethods(['isSecurityUser'])->getMockForAbstractClass();
67
68
        $requestMock = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor(
69
        )->setMethods(['get'])->getMock();
70
        $requestMock->expects($this->atLeastOnce())->method('get')->will(
71
            $this->returnCallback(
72
                function () {
73
                    switch (func_get_arg(0)) {
74
                        case '_route':
75
                            return 'graviton.core.rest.product.post';
76
                            break;
77
                        case 'selfLink':
78
                            return 'graviton-api-test/core/product';
79
                            break;
80
                    }
81
                }
82
            )
83
        );
84
85
        $requestStackMock = $this->getMockBuilder(
86
            '\Symfony\Component\HttpFoundation\RequestStack'
87
        )->disableOriginalConstructor()->setMethods(['getCurrentRequest'])->getMock();
88
        $requestStackMock->expects($this->once())->method('getCurrentRequest')->willReturn($requestMock);
89
90
        $cursorMock = $this->getMockBuilder('\Doctrine\MongoDB\CursorInterface')->disableOriginalConstructor(
91
        )->getMockForAbstractClass();
92
        $cursorMock->expects($this->any())->method('toArray')->willReturn(['someWorkerId' => 'some content']);
93
94
        $queryMock = $this->getMockBuilder('\Doctrine\MongoDB\Query\Query')->disableOriginalConstructor()->getMock();
95
        $queryMock->expects($this->any())->method('execute')->willReturn($cursorMock);
96
97
        $queryBuilderMock = $this->getMockBuilder('\Doctrine\ODM\MongoDB\Query\Builder')->disableOriginalConstructor(
98
        )->getMock();
99
        $queryBuilderMock->expects($this->any())->method('select')->willReturnSelf();
100
        $queryBuilderMock->expects($this->any())->method('field')->willReturnSelf();
101
        $queryBuilderMock->expects($this->any())->method('equals')->willReturnSelf();
102
        $queryBuilderMock->expects($this->any())->method('getQuery')->willReturn($queryMock);
103
104
        $documentManagerMock = $this->getMockBuilder(
105
            '\Doctrine\ODM\MongoDB\DocumentManager'
106
        )->disableOriginalConstructor()->setMethods(['createQueryBuilder', 'persist', 'flush'])->getMock();
107
        $documentManagerMock->expects($this->any())->method('createQueryBuilder')->willReturn($queryBuilderMock);
108
        $documentManagerMock->expects($this->any())->method('persist')->with(
109
            $this->callback(
110
                function ($obj) {
111
                    return
112
                        get_class($obj) == 'GravitonDyn\EventStatusBundle\Document\EventStatus' &&
113
                        $obj->getCreatedate() instanceof \DateTime &&
114
                        get_class($obj->getEventresource()) == 'GravitonDyn\EventStatusBundle\Document\\'.
115
                        'EventStatusEventResourceEmbedded' &&
116
                        get_class($obj->getEventresource()->getRef()) == 'Graviton\DocumentBundle\Entity\\'.
117
                        'ExtReference' &&
118
                        $obj->getEventresource()->getRef()->jsonSerialize() == ['$ref' => 'App', '$id' => 7] &&
119
                        $obj->getEventname() == 'document.dude.config.create' &&
120
                        count($obj->getStatus()) === 1 &&
121
                        count($obj->getInformation()) === 0;
122
                }
123
            )
124
        );
125
        $documentManagerMock->expects($this->any())->method('flush');
126
127
        $extrefConverterMock = $this->getMockBuilder(
128
            '\Graviton\DocumentBundle\Service\ExtReferenceConverter'
129
        )->disableOriginalConstructor()->setMethods(['getExtReference'])->getMock();
130
        $extrefConverterMock->expects($this->exactly(1))->method('getExtReference')
131
            ->willReturn(ExtReference::create('App', 7));
132
133
        $queueEventMock = $this->getMockBuilder(
134
            '\Graviton\RabbitMqBundle\Document\QueueEvent'
135
        )->setMethods(['getEvent', 'getDocumenturl'])->getMock();
136
        $queueEventMock->expects($this->exactly(5))->method('getEvent')->willReturn('document.dude.config.create');
137
        $queueEventMock->expects($this->exactly(2))->method('getDocumenturl')->willReturn('http://localhost/dude/4');
138
139
        $filterResponseEventMock = $this->getMockBuilder(
140
            '\Symfony\Component\HttpKernel\Event\FilterResponseEvent'
141
        )->disableOriginalConstructor()->setMethods(['isMasterRequest', 'getResponse'])->getMock();
142
        $filterResponseEventMock->expects($this->once())->method('isMasterRequest')->willReturn(true);
143
144
        $response = new Response();
145
        $filterResponseEventMock->expects($this->once())->method('getResponse')->willReturn($response);
146
147
        $listener = new EventStatusLinkResponseListener(
148
            $producerMock,
149
            $routerMock,
150
            $requestStackMock,
151
            $documentManagerMock,
152
            $extrefConverterMock,
153
            $queueEventMock,
154
            [
155
                'Testing' => [
156
                    'baseRoute' => 'graviton.core.rest.product',
157
                    'events' => [
158
                        'post' => 'document.core.product.create',
159
                        'put' => 'document.core.product.update',
160
                        'delete' => 'document.core.product.delete'
161
                    ]
162
                ]
163
            ],
164
            '\GravitonDyn\EventWorkerBundle\Document\EventWorker',
165
            '\GravitonDyn\EventStatusBundle\Document\EventStatus',
166
            '\GravitonDyn\EventStatusBundle\Document\EventStatusStatus',
167
            '\GravitonDyn\EventStatusBundle\Document\EventStatusEventResourceEmbedded',
168
            'gravitondyn.eventstatus.rest.eventstatus.get',
169
            $securityMock,
170
            null
171
        );
172
173
        $listener->onKernelResponse($filterResponseEventMock);
174
175
        $this->assertEquals(
176
            '<http://graviton-test.lo/worker/123jkl890yui567mkl>; rel="eventStatus"',
177
            $response->headers->get('Link')
178
        );
179
    }
180
}
181