Completed
Push — master ( 6124bb...a7a023 )
by Guillaume
03:14
created

ApiTest::testApiPatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

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 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the distributed-configuration-bundle package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Gundan\DistributedConfigurationBundle\Tests;
17
18
use Gundan\DistributedConfigurationBundle\Handler\StoreHandler;
19
use Gundan\DistributedConfigurationBundle\Model\KeyValue;
20
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
21
use Webmozart\KeyValueStore\JsonFileStore;
22
23
class ApiTest extends WebTestCase
24
{
25
    private $client;
26
27
    protected function setUp()
28
    {
29
        $this->client = static::createClient([], ['HTTP_ACCEPT' => 'application/json']);
30
    }
31
32
    public function testApiCreate()
33
    {
34
        $this->client->request(
35
            'POST',
36
            '/v1/keys',
37
            [],
38
            [],
39
            ['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
40
            json_encode(['key' => 'test_key', 'value' => 'test_value'])
41
        );
42
        $this->assertTrue(
43
            $this->client->getResponse()->headers->contains(
44
                'Content-Type',
45
                'application/json'
46
            )
47
        );
48
        $this->assertEquals(
49
            '{"key":"test_key","value":"test_value"}',
50
            $this->client->getResponse()->getContent()
51
        );
52
        $this->assertEquals(201, $this->client->getResponse()->getStatusCode());
53
    }
54
55 View Code Duplication
    public function testApiPatch()
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...
56
    {
57
        $this->createKeyValue('key_to_patch');
58
59
        $this->client->request(
60
            'PATCH',
61
            '/v1/keys/key_to_patch',
62
            [],
63
            [],
64
            ['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
65
            json_encode(['value' => 'value_to_patch'])
66
        );
67
68
        $this->assertEquals(204, $this->client->getResponse()->getStatusCode());
69
    }
70
71 View Code Duplication
    public function testApiUpdate()
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...
72
    {
73
        $this->createKeyValue('key_to_update');
74
75
        $this->client->request(
76
            'PUT',
77
            '/v1/keys/key_to_update',
78
            [],
79
            [],
80
            ['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
81
            json_encode(['key' => 'key_to_update', 'value' => 'test_update_value'])
82
        );
83
84
        $this->assertEquals(204, $this->client->getResponse()->getStatusCode());
85
    }
86
87 View Code Duplication
    public function testApiDelete()
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...
88
    {
89
        $this->createKeyValue('key with space');
90
91
        $this->client->request(
92
            'DELETE',
93
            '/v1/keys/key with space',
94
            [],
95
            [],
96
            ['HTTP_CONTENT_TYPE' => 'application/json', 'CONTENT_TYPE' => 'application/json']
97
        );
98
99
        $this->assertEquals(204, $this->client->getResponse()->getStatusCode());
100
    }
101
102
    /**
103
     * createKeyValue.
104
     *
105
     * @param string $key
106
     * @param string $value
107
     */
108
    private function createKeyValue($key = 'test_key', $value = 'test_value')
109
    {
110
        $keyValue = new KeyValue();
111
        $keyValue->setKey($key);
112
        $keyValue->setValue($value);
113
114
        $storeHandler = new StoreHandler(new JsonFileStore(__DIR__.'/App/test.json'));
115
        $storeHandler->flush($keyValue);
116
    }
117
}
118