Skip to content

Commit 52965f4

Browse files
gregoire-jiandam1guelpf
authored andcommitted
Use QueryStringBuilder in AbstractAPI::put
1 parent 860e26f commit 52965f4

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Http\Message\MultipartStream\MultipartStreamBuilder;
99
use Http\Message\StreamFactory;
1010
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\StreamInterface;
1112
use Symfony\Component\OptionsResolver\OptionsResolver;
1213

1314
/**
@@ -89,7 +90,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
8990

9091
$body = null;
9192
if (empty($files) && !empty($parameters)) {
92-
$body = $this->streamFactory->createStream(QueryStringBuilder::build($parameters));
93+
$body = $this->prepareBody($parameters);
9394
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
9495
} elseif (!empty($files)) {
9596
$builder = new MultipartStreamBuilder($this->streamFactory);
@@ -128,7 +129,7 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
128129

129130
$body = null;
130131
if (!empty($parameters)) {
131-
$body = $this->streamFactory->createStream(http_build_query($parameters));
132+
$body = $this->prepareBody($parameters);
132133
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
133134
}
134135

@@ -197,6 +198,18 @@ protected function createOptionsResolver()
197198
return $resolver;
198199
}
199200

201+
/**
202+
* @param array $parameters
203+
* @return StreamInterface
204+
*/
205+
private function prepareBody(array $parameters = [])
206+
{
207+
$raw = QueryStringBuilder::build($parameters);
208+
$stream = $this->streamFactory->createStream($raw);
209+
210+
return $stream;
211+
}
212+
200213
private function preparePath($path, array $parameters = [])
201214
{
202215
if (count($parameters) > 0) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
use Gitlab\Client;
4+
use Http\Client\HttpClient;
5+
use ReflectionClass;
6+
7+
class AbstractApiTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldPrepareBodyWithCleanArrays()
13+
{
14+
$parameters = [
15+
'array_param' => [
16+
'value1',
17+
'value2'
18+
]
19+
];
20+
$expectedBody = 'array_param[]=value1&array_param[]=value2';
21+
22+
$abstractApiMock = $this->getAbstractApiMock();
23+
$reflection = new ReflectionClass(get_class($abstractApiMock));
24+
$method = $reflection->getMethod('prepareBody');
25+
$method->setAccessible(true);
26+
$stream = $method->invokeArgs(
27+
$abstractApiMock,
28+
[
29+
$parameters
30+
]
31+
);
32+
33+
$this->assertEquals($expectedBody, urldecode((string)$stream));
34+
}
35+
36+
protected function getAbstractApiMock(array $methods = [])
37+
{
38+
$httpClient = $this->getMockBuilder(HttpClient::class)
39+
->setMethods(array('sendRequest'))
40+
->getMock()
41+
;
42+
$httpClient
43+
->expects($this->any())
44+
->method('sendRequest')
45+
;
46+
$client = Client::createWithHttpClient($httpClient);
47+
48+
$abstractApiMock = $this->getMockBuilder('Gitlab\Api\AbstractApi')
49+
->setConstructorArgs([
50+
$client,
51+
null
52+
])
53+
->setMethods($methods)
54+
->getMockForAbstractClass()
55+
;
56+
57+
return $abstractApiMock;
58+
}
59+
}

0 commit comments

Comments
 (0)