GuzzleClientSpec   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 24.49 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 12
c 3
b 0
f 2
lcom 0
cbo 1
dl 24
loc 98
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A it_should_make_a_call_to_remote_server() 0 4 1
A it_is_initializable() 0 5 1
A let() 0 5 1
A it_should_throw_exceptions_when_an_error_occurs() 0 5 1
A it_should_be_able_to_return_string() 0 5 1
A it_should_return_full_response_as_array() 0 9 1
A it_should_be_able_to_return_json_format_by_default_when_full_response() 0 7 1
A it_returns_response_in_xml_format() 13 13 1
A it_should_be_able_to_accept_query_parameters_as_string() 11 11 1
A it_accepts_query_parameters_as_array() 0 9 1
A it_should_save_the_file() 0 6 1
A it_should_not_save_the_file() 0 5 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 Superdesk Web Publisher Updater Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace spec\SWP\UpdaterBundle\Client;
15
16
use PhpSpec\ObjectBehavior;
17
18
/**
19
 * PHP >= 5.5.0
20
 *
21
 * @require GuzzleHttp\Client
22
 */
23
class GuzzleClientSpec extends ObjectBehavior
24
{
25
    function it_is_initializable()
26
    {
27
        $this->shouldHaveType('SWP\UpdaterBundle\Client\GuzzleClient');
28
        $this->shouldImplement('SWP\UpdaterBundle\Client\ClientInterface');
29
    }
30
31
    function let()
32
    {
33
        $config = array('base_uri' => 'http://httpbin.org');
34
        $this->beConstructedWith($config);
35
    }
36
37
    function it_should_make_a_call_to_remote_server()
38
    {
39
        $this->call('/status/200')->shouldBe('');
40
    }
41
42
    function it_should_throw_exceptions_when_an_error_occurs()
43
    {
44
        $this->shouldThrow('\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException')->duringCall('/status/404');
45
        $this->shouldThrow('\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException')->duringCall('/status/500');
46
    }
47
48
    function it_should_be_able_to_return_string()
49
    {
50
        $response = $this->call('/headers');
51
        $response->shouldBeString();
52
    }
53
54
    function it_should_return_full_response_as_array()
55
    {
56
        $response = $this->call('/headers', array(), array(), true);
57
        $response->shouldBeArray();
58
        $response['status']->shouldBe(200);
59
        $response['body']->shouldBeString();
60
        $response['version']->shouldEqual("1.1");
61
        $response['reason']->shouldBeString();
62
    }
63
64
    function it_should_be_able_to_return_json_format_by_default_when_full_response()
65
    {
66
        $response = $this->call('/headers', array(), array(), true);
67
        $response['headers']->shouldHaveKey('Content-Type');
68
        $response['headers']['Content-Type']->shouldContain('application/json');
69
        $response['body']->shouldBeString();
70
    }
71
72 View Code Duplication
    function it_returns_response_in_xml_format()
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...
73
    {
74
        $options = array(
75
            'options' => array(
76
                'Content-Type' => 'application/xml'
77
            )
78
        );
79
80
        $response = $this->call('/xml', array(), $options, true);
81
        $response['headers']->shouldHaveKey('Content-Type');
82
        $response['headers']['Content-Type']->shouldContain('application/xml');
83
        $response['body']->shouldBeString();
84
    }
85
86 View Code Duplication
    function it_should_be_able_to_accept_query_parameters_as_string()
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...
87
    {
88
        $arguments = array(
89
            'Server=httpbin'
90
        );
91
92
        $response = $this->call('/response-headers', $arguments, array(), true);
93
        $response['headers']->shouldHaveKey('Content-Type');
94
        $response['headers']['Content-Type']->shouldContain('application/json');
95
        $response['body']->shouldBeString();
96
    }
97
98
    function it_accepts_query_parameters_as_array()
99
    {
100
        $arguments = array(
101
            'Server' => 'httpbin'
102
        );
103
104
        $response = $this->call('/response-headers', $arguments);
105
        $response->shouldBeString();
106
    }
107
108
    function it_should_save_the_file()
109
    {
110
        $filePath = __DIR__.'/../../../file.png';
111
        $response = $this->saveFile('http://httpbin.org/image/png', $filePath);
112
        $response->shouldBe(true);
113
    }
114
115
    function it_should_not_save_the_file()
116
    {
117
        $response = $this->saveFile('fake_url', __DIR__.'/../../../file.png');
118
        $response->shouldBe(false);
119
    }
120
}
121