Conditions | 10 |
Paths | 1 |
Total Lines | 148 |
Code Lines | 109 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 4 | Features | 3 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
28 | public function testEventStatusLinkResponseListener() |
||
29 | { |
||
30 | $producerMock = $this->getMockBuilder( |
||
31 | '\OldSound\RabbitMqBundle\RabbitMq\ProducerInterface' |
||
32 | )->disableOriginalConstructor()->setMethods(['publish', 'getChannel'])->getMockForAbstractClass(); |
||
33 | |||
34 | $producerMock->expects($this->once())->method('publish') |
||
35 | ->will( |
||
36 | $this->returnCallback( |
||
37 | function ($message, $routingKey) { |
||
38 | \PHPUnit_Framework_Assert::assertSame( |
||
39 | '{"event":"document.core.product.create","coreUserId":"",'. |
||
40 | '"document":{"$ref":"graviton-api-test\/core\/product'. |
||
41 | '"},"status":{"$ref":"http:\/\/graviton-test.lo\/worker\/123jkl890yui567mkl"}}', |
||
42 | $message |
||
43 | ); |
||
44 | |||
45 | \PHPUnit_Framework_Assert::assertSame( |
||
46 | 'someWorkerId', |
||
47 | $routingKey |
||
48 | ); |
||
49 | } |
||
50 | ) |
||
51 | ); |
||
52 | |||
53 | $channelMock = $this->getMockBuilder( |
||
54 | '\PhpAmqpLib\Channel\AMQPChannel' |
||
55 | )->disableOriginalConstructor()->getMock(); |
||
56 | |||
57 | $producerMock->expects($this->once())->method('getChannel') |
||
58 | ->willReturn($channelMock); |
||
59 | |||
60 | $routerMock = $this->getMockBuilder('\Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor( |
||
61 | )->setMethods(['generate'])->getMockForAbstractClass(); |
||
62 | $routerMock->expects($this->once())->method('generate')->willReturn( |
||
63 | 'http://graviton-test.lo/worker/123jkl890yui567mkl' |
||
64 | ); |
||
65 | |||
66 | $requestMock = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor( |
||
67 | )->setMethods(['get'])->getMock(); |
||
68 | $requestMock->expects($this->atLeastOnce())->method('get')->will( |
||
69 | $this->returnCallback( |
||
70 | function () { |
||
71 | switch (func_get_arg(0)) { |
||
72 | case '_route': |
||
73 | return 'graviton.core.rest.product.post'; |
||
74 | break; |
||
75 | case 'selfLink': |
||
76 | return 'graviton-api-test/core/product'; |
||
77 | break; |
||
78 | } |
||
79 | } |
||
80 | ) |
||
81 | ); |
||
82 | |||
83 | $requestStackMock = $this->getMockBuilder( |
||
84 | '\Symfony\Component\HttpFoundation\RequestStack' |
||
85 | )->disableOriginalConstructor()->setMethods(['getCurrentRequest'])->getMock(); |
||
86 | $requestStackMock->expects($this->once())->method('getCurrentRequest')->willReturn($requestMock); |
||
87 | |||
88 | $cursorMock = $this->getMockBuilder('\Doctrine\MongoDB\CursorInterface')->disableOriginalConstructor( |
||
89 | )->getMockForAbstractClass(); |
||
90 | $cursorMock->expects($this->any())->method('toArray')->willReturn(['someWorkerId' => 'some content']); |
||
91 | |||
92 | $queryMock = $this->getMockBuilder('\Doctrine\MongoDB\Query\Query')->disableOriginalConstructor()->getMock(); |
||
93 | $queryMock->expects($this->any())->method('execute')->willReturn($cursorMock); |
||
94 | |||
95 | $queryBuilderMock = $this->getMockBuilder('\Doctrine\ODM\MongoDB\Query\Builder')->disableOriginalConstructor( |
||
96 | )->getMock(); |
||
97 | $queryBuilderMock->expects($this->any())->method('select')->willReturnSelf(); |
||
98 | $queryBuilderMock->expects($this->any())->method('field')->willReturnSelf(); |
||
99 | $queryBuilderMock->expects($this->any())->method('equals')->willReturnSelf(); |
||
100 | $queryBuilderMock->expects($this->any())->method('getQuery')->willReturn($queryMock); |
||
101 | |||
102 | $documentManagerMock = $this->getMockBuilder( |
||
103 | '\Doctrine\ODM\MongoDB\DocumentManager' |
||
104 | )->disableOriginalConstructor()->setMethods(['createQueryBuilder', 'persist', 'flush'])->getMock(); |
||
105 | $documentManagerMock->expects($this->any())->method('createQueryBuilder')->willReturn($queryBuilderMock); |
||
106 | $documentManagerMock->expects($this->any())->method('persist')->with( |
||
107 | $this->callback( |
||
108 | function ($obj) { |
||
109 | return |
||
110 | get_class($obj) == 'GravitonDyn\EventStatusBundle\Document\EventStatus' && |
||
111 | $obj->getCreatedate() instanceof \DateTime && |
||
112 | get_class($obj->getEventresource()) == 'GravitonDyn\EventStatusBundle\Document\\'. |
||
113 | 'EventStatusEventResourceEmbedded' && |
||
114 | get_class($obj->getEventresource()->getRef()) == 'Graviton\DocumentBundle\Entity\\'. |
||
115 | 'ExtReference' && |
||
116 | $obj->getEventresource()->getRef()->jsonSerialize() == ['$ref' => 'App', '$id' => 7] && |
||
117 | $obj->getEventname() == 'document.dude.config.create' && |
||
118 | count($obj->getStatus()) === 1 && |
||
119 | count($obj->getInformation()) === 0; |
||
120 | } |
||
121 | ) |
||
122 | ); |
||
123 | $documentManagerMock->expects($this->any())->method('flush'); |
||
124 | |||
125 | $extrefConverterMock = $this->getMockBuilder( |
||
126 | '\Graviton\DocumentBundle\Service\ExtReferenceConverter' |
||
127 | )->disableOriginalConstructor()->setMethods(['getExtReference'])->getMock(); |
||
128 | $extrefConverterMock->expects($this->exactly(1))->method('getExtReference') |
||
129 | ->willReturn(ExtReference::create('App', 7)); |
||
130 | |||
131 | $queueEventMock = $this->getMockBuilder( |
||
132 | '\Graviton\RabbitMqBundle\Document\QueueEvent' |
||
133 | )->setMethods(['getEvent', 'getDocumenturl'])->getMock(); |
||
134 | $queueEventMock->expects($this->exactly(5))->method('getEvent')->willReturn('document.dude.config.create'); |
||
135 | $queueEventMock->expects($this->exactly(2))->method('getDocumenturl')->willReturn('http://localhost/dude/4'); |
||
136 | |||
137 | $filterResponseEventMock = $this->getMockBuilder( |
||
138 | '\Symfony\Component\HttpKernel\Event\FilterResponseEvent' |
||
139 | )->disableOriginalConstructor()->setMethods(['isMasterRequest', 'getResponse'])->getMock(); |
||
140 | $filterResponseEventMock->expects($this->once())->method('isMasterRequest')->willReturn(true); |
||
141 | |||
142 | $response = new Response(); |
||
143 | $filterResponseEventMock->expects($this->once())->method('getResponse')->willReturn($response); |
||
144 | |||
145 | $listener = new EventStatusLinkResponseListener( |
||
146 | $producerMock, |
||
147 | $routerMock, |
||
148 | $requestStackMock, |
||
149 | $documentManagerMock, |
||
150 | $extrefConverterMock, |
||
151 | $queueEventMock, |
||
152 | [ |
||
153 | 'Testing' => [ |
||
154 | 'baseRoute' => 'graviton.core.rest.product', |
||
155 | 'events' => [ |
||
156 | 'post' => 'document.core.product.create', |
||
157 | 'put' => 'document.core.product.update', |
||
158 | 'delete' => 'document.core.product.delete' |
||
159 | ] |
||
160 | ] |
||
161 | ], |
||
162 | '\GravitonDyn\EventWorkerBundle\Document\EventWorker', |
||
163 | '\GravitonDyn\EventStatusBundle\Document\EventStatus', |
||
164 | '\GravitonDyn\EventStatusBundle\Document\EventStatusStatus', |
||
165 | '\GravitonDyn\EventStatusBundle\Document\EventStatusEventResourceEmbedded', |
||
166 | 'gravitondyn.eventstatus.rest.eventstatus.get' |
||
167 | ); |
||
168 | |||
169 | $listener->onKernelResponse($filterResponseEventMock); |
||
170 | |||
171 | $this->assertEquals( |
||
172 | '<http://graviton-test.lo/worker/123jkl890yui567mkl>; rel="eventStatus"', |
||
173 | $response->headers->get('Link') |
||
174 | ); |
||
175 | } |
||
176 | } |
||
177 |