Completed
Push — master ( 0f3db4...6e7c4d )
by Guillaume
16:16
created

ApiTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 46.32 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
c 4
b 1
f 0
lcom 1
cbo 5
dl 44
loc 95
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testApiCreate() 0 22 1
A testApiPatch() 15 15 1
A testApiUpdate() 15 15 1
A testApiDelete() 14 14 1
A createKeyValue() 0 9 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
/*
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 Maikuro\DistributedConfigurationBundle\Tests;
17
18
use Maikuro\DistributedConfigurationBundle\Handler\StoreHandler;
19
use Maikuro\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