Completed
Pull Request — develop (#552)
by
unknown
14:01 queued 09:14
created

VersioningDocumentsTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * Versioning Document Entity class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use GravitonDyn\TestCaseVersioningEntityBundle\DataFixtures\MongoDB\LoadTestCaseVersioningEntityData;
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(LoadTestCaseVersioningEntityData::class)) {
28
            $this->markTestSkipped('Test definitions are not loaded');
29
        }
30
31
        $this->loadFixtures(
32
            [
33
                LoadTestCaseVersioningEntityData::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
        $this->assertObjectHasAttribute('version', $original, 'Response have no version: '.json_encode($original));
53
54
        $initialVersion = $original->version;
55
56
        // Let's change something
57
        $original->data = "one-one";
58
59
        $client = static::createRestClient();
60
        $client->put('/testcase/versioning-entity/one', $original);
61
        $respo = $client->getResponse();
62
        $this->assertEquals(Response::HTTP_NO_CONTENT, $respo->getStatusCode(), $respo->getContent());
63
64
        // Version has been updated with put
65
        $initialVersion++;
66
67
        $client->request('GET', '/testcase/versioning-entity/one');
68
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
69
70
        $originalB = $client->getResults();
71
        $this->assertEquals("one-one", $originalB->data, json_encode($originalB));
72
        $this->assertEquals($initialVersion, $originalB->version, json_encode($originalB));
73
74
        // Let's change something, version should not be possible
75
        $original->data = "one";
76
        $original->version = 1;
77
78
        $client = static::createRestClient();
79
        $client->put('/testcase/versioning-entity/one', $original);
80
        $response = $client->getResponse();
81
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
82
        $this->assertEquals($initialVersion, $response->headers->get('X-Current-Version'));
83
84
85
        // Update with correct version
86
        // Let's change something, version should not be possible
87
        $original->data = "one";
88
        $original->version = $initialVersion;
89
90
        $client = static::createRestClient();
91
        $client->put('/testcase/versioning-entity/one', $original);
92
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
93
    }
94
95
96
97
98
    /**
99
     * Test Document as embedded
100
     *
101
     * @return void
102
     */
103
    public function testPost()
104
    {
105
        $new = new \stdClass();
106
        $new->data = 'something-to-test';
107
108
        $client = static::createRestClient();
109
        $client->post('/testcase/versioning-entity/', $new);
110
        $resp = $client->getResponse();
111
        $this->assertEquals(Response::HTTP_CREATED, $resp->getStatusCode(), $resp->getContent());
112
        $urlNew = $resp->headers->get('Location');
113
114
        // check document
115
        $client = static::createRestClient();
116
        $client->request('GET', $urlNew);
117
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
118
        $original = $client->getResults();
119
120
        $this->assertEquals($new->data, $original->data);
121
        $this->assertEquals(1, $original->version);
122
    }
123
}
124