Completed
Pull Request — develop (#552)
by
unknown
13:11 queued 08:53
created

VersioningDocumentsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 103
Duplicated Lines 14.56 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 15
loc 103
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 15 15 2
B testPut() 0 45 1
A testPost() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * EmbeddingDocumentsTest class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use GravitonDyn\VersioningEntityBundle\DataFixtures\MongoDB\LoadVersioningEntityData;
9
use Symfony\Component\HttpFoundation\Response;
10
use Graviton\TestBundle\Test\RestTestCase;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class VersioningDocumentsTest extends RestTestCase
18
{
19
    /**
20
     * load fixtures
21
     *
22
     * @return void
23
     */
24 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
27
        if (!class_exists(LoadVersioningEntityData::class)) {
28
            $this->markTestSkipped('Test definitions are not loaded');
29
        }
30
31
        $this->loadFixtures(
32
            [
33
                LoadVersioningEntityData::class
34
            ],
35
            null,
36
            'doctrine_mongodb'
37
        );
38
    }
39
40
    /**
41
     * Test Document as embedded
42
     *
43
     * @return void
44
     */
45
    public function testPut()
46
    {
47
        // check document
48
        $client = static::createRestClient();
49
        $client->request('GET', '/testcase/versioning-entity/one');
50
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
51
        $original = $client->getResults();
52
53
        $initialVersion = $original->version;
54
55
        // Let's change something
56
        $original->data = "one-one";
57
58
        $client = static::createRestClient();
59
        $client->put('/testcase/versioning-entity/one', $original);
60
        $respo = $client->getResponse();
61
        $this->assertEquals(Response::HTTP_NO_CONTENT, $respo->getStatusCode(), $respo->getContent());
62
63
        // Version has been updated with put
64
        $initialVersion++;
65
66
        $client->request('GET', '/testcase/versioning-entity/one');
67
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
68
69
        $originalB = $client->getResults();
70
        $this->assertEquals("one-one", $originalB->data, json_encode($originalB));
71
        $this->assertEquals($initialVersion, $originalB->version, json_encode($originalB));
72
73
        // Let's change something, version should not be possible
74
        $original->data = "one";
75
        $original->version = 1;
76
77
        $client = static::createRestClient();
78
        $client->put('/testcase/versioning-entity/one', $original);
79
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
80
81
        // Update with correct version
82
        // Let's change something, version should not be possible
83
        $original->data = "one";
84
        $original->version = $initialVersion;
85
86
        $client = static::createRestClient();
87
        $client->put('/testcase/versioning-entity/one', $original);
88
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
89
    }
90
91
92
93
94
    /**
95
     * Test Document as embedded
96
     *
97
     * @return void
98
     */
99
    public function testPost()
100
    {
101
        $new = new \stdClass();
102
        $new->data = 'something-to-test';
103
104
        $client = static::createRestClient();
105
        $client->post('/testcase/versioning-entity/', $new);
106
        $resp = $client->getResponse();
107
        $this->assertEquals(Response::HTTP_CREATED, $resp->getStatusCode(), $resp->getContent());
108
        $urlNew = $resp->headers->get('Location');
109
110
        // check document
111
        $client = static::createRestClient();
112
        $client->request('GET', $urlNew);
113
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
114
        $original = $client->getResults();
115
116
        $this->assertEquals($new->data, $original->data);
117
        $this->assertEquals(1, $original->version);
118
    }
119
}
120