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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.