Completed
Push — develop ( e125f2...8852f9 )
by
unknown
09:03
created

AsyncLockingTest::testAsyncLock()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 80
Code Lines 45

Duplication

Lines 9
Ratio 11.25 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 80
rs 8.8387
cc 2
eloc 45
nc 2
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
 * test for locking feature
4
 */
5
6
namespace Graviton\RestBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  https://opensource.org/licenses/MIT MIT License
14
 * @link     http://swisscom.ch
15
 */
16
class AsyncLockingTest extends RestTestCase
17
{
18
19
    /**
20
     * try to send 30 async requests to /event/status and see if the result is consistent
21
     *
22
     * @return void
23
     */
24
    public function testAsyncLock()
25
    {
26
        /**
27
         * create test entry
28
         */
29
        $client = static::createRestClient();
30
        $statusEntry = new \stdClass();
31
        $statusEntry->id = 'locktest';
32
        $statusEntry->status = [];
33
        $defaultStatus = new \stdClass();
34
        $defaultStatus->workerId = 'hans';
35
        $defaultStatus->status = 'opened';
36
        $statusEntry->status[] = $defaultStatus;
37
38
        $client->put('/event/status/locktest', $statusEntry);
39
        $this->assertSame(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
40
41
        /**
42
         * the patch we will send
43
         */
44
        $patchJson = json_encode(
45
            [
46
                [
47
                    'op' => 'replace',
48
                    'path' => '/status/0/status',
49
                    'value' => 'ignored'
50
                ],
51
                [
52
                    'op' => 'add',
53
                    'path' => '/status/0/action',
54
                    'value' => [
55
                        '$ref' => 'http://localhost/event/action/default'
56
                    ]
57
                ],
58
                [
59
                    'op' => 'add',
60
                    'path' => '/information/-',
61
                    'value' => [
62
                        'workerId' => 'hans',
63
                        'type' => 'info',
64
                        'content' => 'addedInfo'
65
                    ]
66
                ]
67
            ]
68
        );
69
70
        $promiseStack = [];
71
        $deferred = new \React\Promise\Deferred();
72
73
        $i = 0;
74
        while ($i < 30) {
75
            $promise = $deferred->promise();
76
77
            $promise->then(
78
                function () use ($patchJson) {
79
                    $client = static::createRestClient();
80
                    $client->request('PATCH', '/event/status/locktest', [], [], [], $patchJson);
81
                    $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
82
                }
83
            );
84
85
            $promiseStack[] = $promise;
86
            $i++;
87
        }
88
89
        \React\Promise\all($promiseStack)->then(
90 View Code Duplication
            function () {
91
                // after all is done; check entry
92
                $client = static::createRestClient();
93
                $client->request('GET', '/event/status/locktest');
94
                $result = $client->getResults();
95
96
                $this->assertSame(30, count($result->information));
97
                $this->assertSame(1, count($result->status));
98
            }
99
        );
100
101
        // start the whole thing
102
        $deferred->resolve();
103
    }
104
}
105