Completed
Push — feature/EVO-9770-doc-version-c... ( cb03f2...43bc90 )
by
unknown
13:55
created

VersioningDocumentsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

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 0
loc 103
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 2
B testPut() 0 45 1
A testPost() 0 20 1
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
    public function setUp()
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
        $original2 = $client->getResults();
70
        $this->assertEquals("one-one", $original2->data, json_encode($original2));
71
        $this->assertEquals($initialVersion, $original2->version, json_encode($original2));
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