Completed
Pull Request — develop (#552)
by
unknown
28:27 queued 23:25
created

VersioningDocumentsTest::testPut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
rs 9.2258
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
/**
3
 * Versioning Document Entity class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\SchemaBundle\Constraint\VersionFieldConstraint;
9
use GravitonDyn\TestCaseVersioningEntityBundle\DataFixtures\MongoDB\LoadTestCaseVersioningEntityData;
10
use Symfony\Component\HttpFoundation\Response;
11
use Graviton\TestBundle\Test\RestTestCase;
12
13
/**
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class VersioningDocumentsTest extends RestTestCase
19
{
20
    /**
21
     * load fixtures
22
     *
23
     * @return void
24
     */
25 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...
26
    {
27
28
        if (!class_exists(LoadTestCaseVersioningEntityData::class)) {
29
            $this->markTestSkipped('Test definitions are not loaded');
30
        }
31
32
        $this->loadFixtures(
33
            [
34
                LoadTestCaseVersioningEntityData::class
35
            ],
36
            null,
37
            'doctrine_mongodb'
38
        );
39
    }
40
41
    /**
42
     * Test Document as embedded
43
     *
44
     * @return void
45
     */
46
    public function testPut()
47
    {
48
        // check document
49
        $client = static::createRestClient();
50
        $client->request('GET', '/testcase/versioning-entity/one');
51
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
52
        $original = $client->getResults();
53
        $this->assertObjectHasAttribute('version', $original, 'Response have no version: '.json_encode($original));
54
55
        $initialVersion = $original->version;
56
57
        // Let's change something
58
        $original->data = "one-one";
59
60
        $client = static::createRestClient();
61
        $client->put('/testcase/versioning-entity/one', $original);
62
        $respo = $client->getResponse();
63
        $this->assertEquals(Response::HTTP_NO_CONTENT, $respo->getStatusCode(), $respo->getContent());
64
65
        // Version has been updated with put
66
        $initialVersion++;
67
68
        $client->request('GET', '/testcase/versioning-entity/one');
69
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
70
71
        $originalB = $client->getResults();
72
        $this->assertEquals("one-one", $originalB->data, json_encode($originalB));
73
        $this->assertEquals($initialVersion, $originalB->version, json_encode($originalB));
74
75
        // Let's change something, version should not be possible
76
        $original->data = "one";
77
        $original->version = 1;
78
79
        $client = static::createRestClient();
80
        $client->put('/testcase/versioning-entity/one', $original);
81
        $response = $client->getResponse();
82
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
83
        $this->assertEquals($initialVersion, $response->headers->get(VersionFieldConstraint::HEADER_NAME));
84
85
86
        // Update with correct version
87
        // Let's change something, version should not be possible
88
        $original->data = "one";
89
        $original->version = $initialVersion;
90
91
        $client = static::createRestClient();
92
        $client->put('/testcase/versioning-entity/one', $original);
93
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
94
    }
95
96
97
98
99
    /**
100
     * Test Document as embedded
101
     *
102
     * @return void
103
     */
104
    public function testPost()
105
    {
106
        $new = new \stdClass();
107
        $new->data = 'something-to-test';
108
109
        $client = static::createRestClient();
110
        $client->post('/testcase/versioning-entity/', $new);
111
        $resp = $client->getResponse();
112
        $this->assertEquals(Response::HTTP_CREATED, $resp->getStatusCode(), $resp->getContent());
113
        $urlNew = $resp->headers->get('Location');
114
115
        // check document
116
        $client = static::createRestClient();
117
        $client->request('GET', $urlNew);
118
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
119
        $original = $client->getResults();
120
121
        $this->assertEquals($new->data, $original->data);
122
        $this->assertEquals(1, $original->version);
123
    }
124
}
125