Completed
Push — develop ( dc031d...86c271 )
by
unknown
14s
created

testEventStatusLinkResponseListener()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 8.9667
c 0
b 0
f 0
cc 1
eloc 49
nc 1
nop 0

How to fix   Long Method   

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 for /event/status
4
 */
5
6
namespace Graviton\RabbitMqBundle\Tests\Controller;
7
8
use Graviton\RabbitMqBundle\Producer\Dummy;
9
use Graviton\TestBundle\Test\RestTestCase;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Functional test for /event/status.
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 EventStatusControllerTest extends RestTestCase
20
{
21
22
    /**
23
     * test to see if we can insert a status and if graviton complains about an invalid status
24
     *
25
     * @return void
26
     */
27
    public function testInvalidStatus()
28
    {
29
        $status = new \stdClass();
30
        $status->id = 'mynewstatus';
31
        $status->createDate = '2015-09-24T07:21:24+0000';
32
        $status->eventName = 'document.test.app.create';
33
34
        $statusEntry = new \stdClass();
35
        $statusEntry->workerId = 'testworker';
36
        $statusEntry->status = 'opened';
37
        $statusEntry->action = (object) [
38
            '$ref' => 'http://localhost/event/action/abba'
39
        ];
40
41
        $status->status = [$statusEntry];
42
43
        $client = static::createRestClient();
44
        $client->put('/event/status/mynewstatus', $status);
45
46
        $this->assertNull($client->getResults());
47
        $this->assertNull($client->getResponse()->headers->get('Location'));
48
        $this->assertEquals(204, $client->getResponse()->getStatusCode());
49
50
        // get our object again
51
        $client = static::createRestClient();
52
        $client->request('GET', '/event/status/mynewstatus');
53
        $results = $client->getResults();
54
55
        $this->assertEquals('opened', $results->status[0]->status);
56
        $this->assertEquals('http://localhost/event/action/abba', $results->status[0]->action->{'$ref'});
57
58
        // set invalid status
59
        $results->status[0]->status = 'thinking';
60
61
        $client = static::createRestClient();
62
        $client->put('/event/status/mynewstatus', $results);
63
        $results = $client->getResults();
64
65
        $this->assertEquals(400, $client->getResponse()->getStatusCode());
66
        $this->assertEquals("status[0].status", $results[0]->propertyPath);
67
        $this->assertContains(
68
            "Does not have a value in the enumeration [\"opened\",\"working\",\"ignored\",\"done\",\"failed\"]",
69
            $results[0]->message
70
        );
71
    }
72
73
    /**
74
     * test to see if we can insert a status and if graviton complains about an invalid status
75
     *
76
     * @return void
77
     */
78
    public function testInvalidInformationType()
79
    {
80
        $status = new \stdClass();
81
        $status->id = 'mynewstatus';
82
        $status->createDate = '2015-09-24T07:21:24+0000';
83
        $status->eventName = 'document.test.app.create';
84
85
        $informationEntry = new \stdClass();
86
        $informationEntry->workerId = 'testworker';
87
        $informationEntry->type = 'info';
88
        $informationEntry->content = 'see the attached document';
89
        $informationEntry->{'$ref'} = 'http://localhost/core/app/admin';
90
91
        $status->information = [$informationEntry];
92
93
        $client = static::createRestClient();
94
        $client->put('/event/status/mynewstatus', $status);
95
96
        $this->assertNull($client->getResults());
97
        $this->assertNull($client->getResponse()->headers->get('Location'));
98
        $this->assertEquals(204, $client->getResponse()->getStatusCode());
99
100
        // get our object again
101
        $client = static::createRestClient();
102
        $client->request('GET', '/event/status/mynewstatus');
103
        $results = $client->getResults();
104
105
        $this->assertEquals('testworker', $results->information[0]->workerId);
106
        $this->assertEquals('info', $results->information[0]->type);
107
        $this->assertEquals('see the attached document', $results->information[0]->content);
108
        $this->assertEquals('http://localhost/core/app/admin', $results->information[0]->{'$ref'});
109
110
        // set invalid information type
111
        $results->information[0]->type = 'bogus';
112
113
        $client = static::createRestClient();
114
        $client->put('/event/status/mynewstatus', $results);
115
        $results = $client->getResults();
116
117
        $this->assertEquals(400, $client->getResponse()->getStatusCode());
118
        $this->assertEquals("information[0].type", $results[0]->propertyPath);
119
        $this->assertContains(
120
            "Does not have a value in the enumeration [\"debug\",\"info\",\"warning\",\"error\"]",
121
            $results[0]->message
122
        );
123
    }
124
125
    /**
126
     * Event Status Action
127
     *
128
     * @return void
129
     */
130
    public function testEventStatusActionStatus()
131
    {
132
        // Create a action and translation
133
        $action = new \stdClass();
134
        $action->id = 'new-worker-action-id';
135
        $action->description = new \stdClass();
136
        $action->description->en = "Some translated action";
137
138
        $client = static::createRestClient();
139
        $client->put('/event/action/'.$action->id, $action);
140
141
        // Check result
142
        $this->assertEquals(204, $client->getResponse()->getStatusCode());
143
144
        // get our object again
145
        $client = static::createRestClient();
146
        $client->request('GET', '/event/action/'.$action->id);
147
        $results = $client->getResults();
148
        $this->assertEquals($action->description->en, $results->description->en);
149
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
150
151
        // Creating the event
152
        $eventStatus = new \stdClass();
153
        $eventStatus->id = 'mynewstatus2';
154
        $eventStatus->createDate = '2015-09-24T07:21:24+0000';
155
        $eventStatus->eventName = 'document.test.app.create';
156
157
        $status = new \stdClass();
158
        $status->workerId = 'testworker';
159
        $status->status = 'opened';
160
        $status->action = new \stdClass();
161
        $status->action->{'$ref'} = 'http://localhost/event/action/'.$action->id;
162
        $eventStatus->status = [$status];
163
164
        // Save the status
165
        $client = static::createRestClient();
166
        $client->put('/event/status/mynewstatus2', $eventStatus);
167
168
        $this->assertNull($client->getResults());
169
        $this->assertNull($client->getResponse()->headers->get('Location'));
170
        $this->assertEquals(204, $client->getResponse()->getStatusCode());
171
172
        // get our object again, checking
173
        $client = static::createRestClient();
174
        $client->request('GET', '/event/status/mynewstatus2');
175
        $results = $client->getResults();
176
177
        $this->assertEquals('opened', $results->status[0]->status);
178
        $this->assertEquals('http://localhost/event/action/'.$action->id, $results->status[0]->action->{'$ref'});
179
    }
180
181
182
    /**
183
     * Verifies the correct workflow of the ResponseListener
184
     *
185
     * @return void
186
     */
187
    public function testEventStatusLinkResponseListener()
188
    {
189
        // Create a test worker
190
        $worker = new \stdClass();
191
        $worker->id = 'test-worker-listener';
192
        $worker->subscription = [];
193
        $event = new \stdClass();
194
        $event->event = 'document.core.app.create';
195
        $worker->subscription[] = $event;
196
        $event = new \stdClass();
197
        $event->event = 'document.core.app.update';
198
        $worker->subscription[] = $event;
199
200
        $client = static::createRestClient();
201
        $client->put('/event/worker/' . $worker->id, $worker);
202
        $response = $client->getResponse();
203
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode(), $response->getContent());
204
205
        $testApp = new \stdClass();
206
        $testApp->id = "test-event-app";
207
        $testApp->name = new \stdClass();
208
        $testApp->name->en = "test-event-app";
209
        $testApp->showInMenu = false;
210
211
        $client = static::createRestClient();
212
        /** @var Dummy $dbProducer */
213
        $dbProducer = $client->getContainer()->get('graviton.rabbitmq.jobproducer');
214
        $dbProducer->resetEventList();
215
216
        $client->put('/core/app/' . $testApp->id, $testApp);
217
        $response = $client->getResponse();
218
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode(), $response->getContent());
219
        // the worker relative our cannot be in the Link header
220
        $this->assertNotContains('backendalias:9443', $response->headers->get('link'));
221
        $this->assertContains('eventStatus', $response->headers->get('link'));
222
223
        /** @var Dummy $dbProducer */
224
        $events = $dbProducer->getEventList();
225
226
        $this->assertCount(1, $events);
227
        $data = json_decode($events[0], true);
228
229
        $this->assertEquals('document.core.app.update', $data['event']);
230
        $this->assertEquals('anonymous', $data['coreUserId']);
231
        $this->assertEquals('https://backendalias:9443/core/app/test-event-app', $data['document']['$ref']);
232
        $this->assertContains('https://backendalias:9443/event/status/', $data['status']['$ref']);
233
234
        // A failing event should not be published
235
        // using patch
236
        $patchObject = json_encode(
237
            [
238
                [
239
                    'op' => 'replace',
240
                    'path' => '/lastModified',
241
                    'value' => '2014-10-03T20:10:05+2000'
242
                ]
243
            ]
244
        );
245
        $client->request('PATCH', '/core/app/' . $testApp->id, [], [], [], $patchObject);
246
        $response = $client->getResponse();
247
        $this->assertEquals(Response::HTTP_NOT_MODIFIED, $response->getStatusCode(), $response->getContent());
248
249
        /** @var Dummy $dbProducer */
250
        $events = $dbProducer->getEventList();
251
        $this->assertCount(1, $events);
252
253
        // With a wrong param S
254
        $testApp->showInMenuS = false;
255
        $client->put('/core/app/' . $testApp->id, $testApp);
256
        $response = $client->getResponse();
257
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode(), $response->getContent());
258
259
        /** @var Dummy $dbProducer */
260
        $events = $dbProducer->getEventList();
261
        $this->assertCount(1, $events);
262
    }
263
}
264