Skip to content

Commit a1174d7

Browse files
committed
Migrating JobsAPI to use new HTTPlug.
Now returning a StreamInterface instead of a string for artifact files.
1 parent 866532d commit a1174d7

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Discovery\StreamFactoryDiscovery;
66
use Http\Message\MultipartStream\MultipartStreamBuilder;
77
use Http\Message\StreamFactory;
8+
use Psr\Http\Message\ResponseInterface;
89

910
/**
1011
* Abstract class for Api classes
@@ -52,18 +53,29 @@ public function configure()
5253
}
5354

5455
/**
56+
* Performs a GET query and returns the response as a PSR-7 response object.
57+
*
5558
* @param string $path
5659
* @param array $parameters
5760
* @param array $requestHeaders
58-
* @return mixed
61+
* @return ResponseInterface
5962
*/
60-
protected function get($path, array $parameters = array(), $requestHeaders = array())
63+
protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array())
6164
{
6265
$path = $this->preparePath($path, $parameters);
6366

64-
$response = $this->client->getHttpClient()->get($path, $requestHeaders);
67+
return $this->client->getHttpClient()->get($path, $requestHeaders);
68+
}
6569

66-
return ResponseMediator::getContent($response);
70+
/**
71+
* @param string $path
72+
* @param array $parameters
73+
* @param array $requestHeaders
74+
* @return mixed
75+
*/
76+
protected function get($path, array $parameters = array(), $requestHeaders = array())
77+
{
78+
return ResponseMediator::getContent($this->getAsResponse($path, $parameters, $requestHeaders));
6779
}
6880

6981
/**

lib/Gitlab/Api/Jobs.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Gitlab\Api;
22

3+
use Psr\Http\Message\StreamInterface;
4+
35
class Jobs extends AbstractApi
46
{
57
const SCOPE_CREATED = 'created';
@@ -49,24 +51,24 @@ public function show($project_id, $job_id)
4951
/**
5052
* @param int|string $project_id
5153
* @param int $job_id
52-
* @return string
54+
* @return StreamInterface
5355
*/
5456
public function artifacts($project_id, $job_id)
5557
{
56-
return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts");
58+
return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts")->getBody();
5759
}
5860

5961
/**
6062
* @param int|string $project_id
6163
* @param string $ref_name
6264
* @param string $job_name
63-
* @return string
65+
* @return StreamInterface
6466
*/
6567
public function artifactsByRefName($project_id, $ref_name, $job_name)
6668
{
67-
return $this->get("projects/".$this->encodePath($project_id)."/jobs/artifacts/".$this->encodePath($ref_name)."/download", array(
69+
return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/artifacts/".$this->encodePath($ref_name)."/download", array(
6870
'job' => $job_name
69-
));
71+
))->getBody();
7072
}
7173

7274
/**

test/Gitlab/Tests/Api/JobsTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php namespace Gitlab\Tests\Api;
22

33
use Gitlab\Api\Jobs;
4+
use GuzzleHttp\Psr7\Response;
45

5-
class JobsTest extends ApiTestCase
6+
class JobsTest extends TestCase
67
{
78
/**
89
* @test
@@ -70,35 +71,35 @@ public function shouldGetJob()
7071
*/
7172
public function shouldGetArtifacts()
7273
{
73-
$expectedString = "some file content";
74+
$returnedStream = new Response(200, [], 'foobar');
7475

7576
$api = $this->getApiMock();
7677
$api->expects($this->once())
77-
->method('get')
78+
->method('getAsResponse')
7879
->with('projects/1/jobs/3/artifacts')
79-
->will($this->returnValue($expectedString))
80+
->will($this->returnValue($returnedStream))
8081
;
8182

82-
$this->assertEquals($expectedString, $api->artifacts(1, 3));
83+
$this->assertEquals('foobar', $api->artifacts(1, 3)->getContents());
8384
}
8485

8586
/**
8687
* @test
8788
*/
8889
public function shouldGetArtifactsByRefName()
8990
{
90-
$expectedString = "some file content";
91+
$returnedStream = new Response(200, [], 'foobar');
9192

9293
$api = $this->getApiMock();
9394
$api->expects($this->once())
94-
->method('get')
95+
->method('getAsResponse')
9596
->with('projects/1/jobs/artifacts/master/download', array(
9697
'job' => 'job_name'
9798
))
98-
->will($this->returnValue($expectedString))
99+
->will($this->returnValue($returnedStream))
99100
;
100101

101-
$this->assertEquals($expectedString, $api->artifactsByRefName(1, 'master', 'job_name'));
102+
$this->assertEquals('foobar', $api->artifactsByRefName(1, 'master', 'job_name')->getContents());
102103
}
103104

104105
/**

test/Gitlab/Tests/Api/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function getApiMock(array $methods = [])
2828
$client = Client::createWithHttpClient($httpClient);
2929

3030
return $this->getMockBuilder($this->getApiClass())
31-
->setMethods(array_merge(array('get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'), $methods))
31+
->setMethods(array_merge(array('getAsResponse', 'get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'), $methods))
3232
->setConstructorArgs(array($client))
3333
->getMock();
3434
}

0 commit comments

Comments
 (0)