Completed
Push — master ( c25dd9...751246 )
by Adrian
09:09
created

testEventStatusLinkResponseListener()   D

Complexity

Conditions 10
Paths 1

Size

Total Lines 139
Code Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 139
rs 4.8196
cc 10
eloc 103
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  http://opensource.org/licenses/gpl-license.php GNU Public 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'])->getMockForAbstractClass();
31
        $producerMock->expects($this->once())->method('publish')
32
        ->will(
33
            $this->returnCallback(
34
                function ($message, $routingKey) {
35
                    \PHPUnit_Framework_Assert::assertSame(
36
                        '{"event":"document.core.product.create","document":{"$ref":"graviton-api-test\/core\/product'.
37
                        '"},"status":{"$ref":"http:\/\/graviton-test.lo\/worker\/123jkl890yui567mkl"}}',
38
                        $message
39
                    );
40
41
                    \PHPUnit_Framework_Assert::assertSame(
42
                        'document.dude.config.create',
43
                        $routingKey
44
                    );
45
                }
46
            )
47
        );
48
49
        $routerMock = $this->getMockBuilder('\Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor(
50
        )->setMethods(['generate'])->getMockForAbstractClass();
51
        $routerMock->expects($this->once())->method('generate')->willReturn(
52
            'http://graviton-test.lo/worker/123jkl890yui567mkl'
53
        );
54
55
        $requestMock = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor(
56
        )->setMethods(['get'])->getMock();
57
        $requestMock->expects($this->atLeastOnce())->method('get')->will(
58
            $this->returnCallback(
59
                function () {
60
                    switch (func_get_arg(0)) {
61
                        case '_route':
62
                            return 'graviton.core.rest.product.post';
63
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64
                        case 'selfLink':
65
                            return 'graviton-api-test/core/product';
66
                            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
67
                    }
68
                }
69
            )
70
        );
71
72
        $requestStackMock = $this->getMockBuilder(
73
            '\Symfony\Component\HttpFoundation\RequestStack'
74
        )->disableOriginalConstructor()->setMethods(['getCurrentRequest'])->getMock();
75
        $requestStackMock->expects($this->once())->method('getCurrentRequest')->willReturn($requestMock);
76
77
        $cursorMock = $this->getMockBuilder('\Doctrine\MongoDB\CursorInterface')->disableOriginalConstructor(
78
        )->getMockForAbstractClass();
79
        $cursorMock->expects($this->once())->method('toArray')->willReturn(['someWorkerId' => 'some content']);
80
81
        $queryMock = $this->getMockBuilder('\Doctrine\MongoDB\Query\Query')->disableOriginalConstructor()->getMock();
82
        $queryMock->expects($this->once())->method('execute')->willReturn($cursorMock);
83
84
        $queryBuilderMock = $this->getMockBuilder('\Doctrine\ODM\MongoDB\Query\Builder')->disableOriginalConstructor(
85
        )->getMock();
86
        $queryBuilderMock->expects($this->once())->method('select')->willReturnSelf();
87
        $queryBuilderMock->expects($this->once())->method('field')->willReturnSelf();
88
        $queryBuilderMock->expects($this->once())->method('equals')->willReturnSelf();
89
        $queryBuilderMock->expects($this->once())->method('getQuery')->willReturn($queryMock);
90
91
        $documentManagerMock = $this->getMockBuilder(
92
            '\Doctrine\ODM\MongoDB\DocumentManager'
93
        )->disableOriginalConstructor()->setMethods(['createQueryBuilder', 'persist', 'flush'])->getMock();
94
        $documentManagerMock->expects($this->once())->method('createQueryBuilder')->willReturn($queryBuilderMock);
95
        $documentManagerMock->expects($this->once())->method('persist')->with(
96
            $this->callback(
97
                function ($obj) {
98
                    return
99
                        get_class($obj) == 'GravitonDyn\EventStatusBundle\Document\EventStatus' &&
100
                        $obj->getCreatedate() instanceof \DateTime &&
101
                        get_class($obj->getEventresource()) == 'GravitonDyn\EventStatusBundle\Document\\'.
102
                        'EventStatusEventResourceEmbedded' &&
103
                        get_class($obj->getEventresource()->getRef()) == 'Graviton\DocumentBundle\Entity\\'.
104
                        'ExtReference' &&
105
                        $obj->getEventresource()->getRef()->jsonSerialize() == ['$ref' => 'App', '$id' => 7] &&
106
                        $obj->getEventname() == 'document.dude.config.create' &&
107
                        count($obj->getStatus()) === 1 &&
108
                        count($obj->getInformation()) === 0;
109
                }
110
            )
111
        );
112
        $documentManagerMock->expects($this->once())->method('flush');
113
114
        $extrefConverterMock = $this->getMockBuilder(
115
            '\Graviton\DocumentBundle\Service\ExtReferenceConverter'
116
        )->disableOriginalConstructor()->setMethods(['getExtReference'])->getMock();
117
        $extrefConverterMock->expects($this->exactly(1))->method('getExtReference')
118
            ->willReturn(ExtReference::create('App', 7));
119
120
        $queueEventMock = $this->getMockBuilder(
121
            '\Graviton\RabbitMqBundle\Document\QueueEvent'
122
        )->setMethods(['getEvent', 'getDocumenturl'])->getMock();
123
        $queueEventMock->expects($this->exactly(5))->method('getEvent')->willReturn('document.dude.config.create');
124
        $queueEventMock->expects($this->exactly(2))->method('getDocumenturl')->willReturn('http://localhost/dude/4');
125
126
        $filterResponseEventMock = $this->getMockBuilder(
127
            '\Symfony\Component\HttpKernel\Event\FilterResponseEvent'
128
        )->disableOriginalConstructor()->setMethods(['isMasterRequest', 'getResponse'])->getMock();
129
        $filterResponseEventMock->expects($this->once())->method('isMasterRequest')->willReturn(true);
130
131
        $response = new Response();
132
        $filterResponseEventMock->expects($this->once())->method('getResponse')->willReturn($response);
133
134
        $listener = new EventStatusLinkResponseListener(
135
            $producerMock,
136
            $routerMock,
137
            $requestStackMock,
138
            $documentManagerMock,
139
            $extrefConverterMock,
140
            $queueEventMock,
141
            [
142
              'Testing' => [
143
                  'baseRoute' => 'graviton.core.rest.product',
144
                  'events' => [
145
                      'post' => 'document.core.product.create',
146
                      'put' => 'document.core.product.update',
147
                      'delete' => 'document.core.product.delete'
148
                  ]
149
              ]
150
            ],
151
            '\GravitonDyn\EventWorkerBundle\Document\EventWorker',
152
            '\GravitonDyn\EventStatusBundle\Document\EventStatus',
153
            '\GravitonDyn\EventStatusBundle\Document\EventStatusStatus',
154
            '\GravitonDyn\EventStatusBundle\Document\EventStatusEventResourceEmbedded',
155
            'gravitondyn.eventstatus.rest.eventstatus.get'
156
        );
157
158
        $listener->onKernelResponse($filterResponseEventMock);
159
160
        $this->assertEquals(
161
            '<http://graviton-test.lo/worker/123jkl890yui567mkl>; rel="eventStatus"',
162
            $response->headers->get('Link')
163
        );
164
    }
165
}
166