Skip to content

Commit f3c9f70

Browse files
author
Matt Humphrey
committed
Added AbstractApi tests
1 parent cb338df commit f3c9f70

14 files changed

+232
-25
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(Client $client)
3232

3333
/**
3434
* @return $this
35+
* @codeCoverageIgnore
3536
*/
3637
public function configure()
3738
{
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
use Gitlab\Api\AbstractApi;
4+
use Gitlab\Client;
5+
use Gitlab\HttpClient\Message\Response;
6+
7+
class AbstractApiTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldPassGETRequestToClient()
13+
{
14+
$response = $this->getResponse('value');
15+
16+
$httpClient = $this->getHttpMock();
17+
$httpClient
18+
->expects($this->any())
19+
->method('get')
20+
->with('/path', array('param1' => 'param1value'), array('header1' => 'header1value'))
21+
->will($this->returnValue($response));
22+
23+
$client = $this->getClientMock();
24+
$client->setHttpClient($httpClient);
25+
26+
$api = $this->getAbstractApiObject($client);
27+
$this->assertEquals('value', $api->get('/path', array('param1' => 'param1value'), array('header1' => 'header1value')));
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function shouldPassPOSTRequestToClient()
34+
{
35+
$response = $this->getResponse('value');
36+
37+
$httpClient = $this->getHttpMock();
38+
$httpClient
39+
->expects($this->any())
40+
->method('post')
41+
->with('/path', array('param1' => 'param1value'), array('header1' => 'header1value'))
42+
->will($this->returnValue($response));
43+
44+
$client = $this->getClientMock();
45+
$client->setHttpClient($httpClient);
46+
47+
$api = $this->getAbstractApiObject($client);
48+
$this->assertEquals('value', $api->post('/path', array('param1' => 'param1value'), array('header1' => 'header1value')));
49+
}
50+
51+
/**
52+
* @test
53+
*/
54+
public function shouldPassPUTRequestToClient()
55+
{
56+
$response = $this->getResponse('value');
57+
58+
$httpClient = $this->getHttpMock();
59+
$httpClient
60+
->expects($this->any())
61+
->method('put')
62+
->with('/path', array('param1' => 'param1value'), array('header1' => 'header1value'))
63+
->will($this->returnValue($response));
64+
65+
$client = $this->getClientMock();
66+
$client->setHttpClient($httpClient);
67+
68+
$api = $this->getAbstractApiObject($client);
69+
$this->assertEquals('value', $api->put('/path', array('param1' => 'param1value'), array('header1' => 'header1value')));
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function shouldPassDELETERequestToClient()
76+
{
77+
$response = $this->getResponse('value');
78+
79+
$httpClient = $this->getHttpMock();
80+
$httpClient
81+
->expects($this->any())
82+
->method('delete')
83+
->with('/path', array('param1' => 'param1value'), array('header1' => 'header1value'))
84+
->will($this->returnValue($response));
85+
86+
$client = $this->getClientMock();
87+
$client->setHttpClient($httpClient);
88+
89+
$api = $this->getAbstractApiObject($client);
90+
$this->assertEquals('value', $api->delete('/path', array('param1' => 'param1value'), array('header1' => 'header1value')));
91+
}
92+
93+
/**
94+
* @test
95+
*/
96+
public function shouldPassPATCHRequestToClient()
97+
{
98+
$response = $this->getResponse('value');
99+
100+
$httpClient = $this->getHttpMock();
101+
$httpClient
102+
->expects($this->any())
103+
->method('patch')
104+
->with('/path', array('param1' => 'param1value'), array('header1' => 'header1value'))
105+
->will($this->returnValue($response));
106+
107+
$client = $this->getClientMock();
108+
$client->setHttpClient($httpClient);
109+
110+
$api = $this->getAbstractApiObject($client);
111+
$this->assertEquals('value', $api->patch('/path', array('param1' => 'param1value'), array('header1' => 'header1value')));
112+
}
113+
114+
/**
115+
* @param mixed $value
116+
* @return Response
117+
*/
118+
protected function getResponse($value)
119+
{
120+
$response = new Response();
121+
$response->setContent($value);
122+
123+
return $response;
124+
}
125+
126+
/**
127+
* @param Client $client
128+
* @return AbstractApiTestInstance
129+
*/
130+
protected function getAbstractApiObject(Client $client)
131+
{
132+
return new AbstractApiTestInstance($client);
133+
}
134+
}
135+
136+
class AbstractApiTestInstance extends AbstractApi
137+
{
138+
/**
139+
* {@inheritDoc}
140+
*/
141+
public function get($path, array $parameters = array(), $requestHeaders = array())
142+
{
143+
return parent::get($path, $parameters, $requestHeaders);
144+
}
145+
146+
/**
147+
* {@inheritDoc}
148+
*/
149+
public function post($path, array $parameters = array(), $requestHeaders = array())
150+
{
151+
return parent::post($path, $parameters, $requestHeaders);
152+
}
153+
154+
/**
155+
* {@inheritDoc}
156+
*/
157+
public function patch($path, array $parameters = array(), $requestHeaders = array())
158+
{
159+
return parent::patch($path, $parameters, $requestHeaders);
160+
}
161+
162+
/**
163+
* {@inheritDoc}
164+
*/
165+
public function put($path, array $parameters = array(), $requestHeaders = array())
166+
{
167+
return parent::put($path, $parameters, $requestHeaders);
168+
}
169+
170+
/**
171+
* {@inheritDoc}
172+
*/
173+
public function delete($path, array $parameters = array(), $requestHeaders = array())
174+
{
175+
return parent::delete($path, $parameters, $requestHeaders);
176+
}
177+
}

test/Gitlab/Tests/Api/ApiTestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
abstract class ApiTestCase extends TestCase
4+
{
5+
abstract protected function getApiClass();
6+
7+
/**
8+
* @param array $methods
9+
* @return \PHPUnit_Framework_MockObject_MockObject|mixed
10+
*/
11+
protected function getApiMock($methods = array())
12+
{
13+
$client = $this->getClientMock();
14+
15+
$methods = array_merge(array('get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'), $methods);
16+
17+
return $this->getMockBuilder($this->getApiClass())
18+
->setMethods($methods)
19+
->setConstructorArgs(array($client))
20+
->getMock()
21+
;
22+
}
23+
}

test/Gitlab/Tests/Api/GroupsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Gitlab\Api\AbstractApi;
44

5-
class GroupsTest extends TestCase
5+
class GroupsTest extends ApiTestCase
66
{
77
/**
88
* @test

test/Gitlab/Tests/Api/IssuesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Gitlab\Api\AbstractApi;
44

5-
class IssuesTest extends TestCase
5+
class IssuesTest extends ApiTestCase
66
{
77
/**
88
* @test

test/Gitlab/Tests/Api/MergeRequestsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Gitlab\Api\AbstractApi;
44
use Gitlab\Api\MergeRequests;
55

6-
class MergeRequestsTest extends TestCase
6+
class MergeRequestsTest extends ApiTestCase
77
{
88
/**
99
* @test

test/Gitlab/Tests/Api/MilestonesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Gitlab\Tests\Api;
22

3-
class MilestonesTest extends TestCase
3+
class MilestonesTest extends ApiTestCase
44
{
55
/**
66
* @test

test/Gitlab/Tests/Api/ProjectNamespacesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Gitlab\Api\AbstractApi;
44

5-
class ProjectNamespacesTest extends TestCase
5+
class ProjectNamespacesTest extends ApiTestCase
66
{
77
/**
88
* @test

test/Gitlab/Tests/Api/ProjectsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Gitlab\Api\AbstractApi;
44
use Gitlab\Api\Projects;
55

6-
class ProjectsTest extends TestCase
6+
class ProjectsTest extends ApiTestCase
77
{
88
/**
99
* @test

test/Gitlab/Tests/Api/RepositoriesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Gitlab\Api\AbstractApi;
44

5-
class RepositoriesTest extends TestCase
5+
class RepositoriesTest extends ApiTestCase
66
{
77
/**
88
* @test

test/Gitlab/Tests/Api/SnippetsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Gitlab\Tests\Api;
22

3-
class SnippetsTest extends TestCase
3+
class SnippetsTest extends ApiTestCase
44
{
55
/**
66
* @test

test/Gitlab/Tests/Api/SystemHooksTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Gitlab\Tests\Api;
22

3-
class SystemHooksTest extends TestCase
3+
class SystemHooksTest extends ApiTestCase
44
{
55
/**
66
* @test

test/Gitlab/Tests/Api/TestCase.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
<?php namespace Gitlab\Tests\Api;
22

3+
use Buzz\Client\Curl;
4+
35
use Gitlab\Client;
6+
use Gitlab\HttpClient\HttpClientInterface;
47

58
abstract class TestCase extends \PHPUnit_Framework_TestCase
69
{
7-
abstract protected function getApiClass();
10+
/**
11+
* @return Client
12+
*/
13+
protected function getClientMock()
14+
{
15+
return new Client($this->getHttpMock());
16+
}
817

918
/**
10-
* @return \PHPUnit_Framework_MockObject_MockObject|mixed
19+
* @return \PHPUnit_Framework_MockObject_MockObject|HttpClientInterface
1120
*/
12-
protected function getApiMock($methods = array())
21+
protected function getHttpMock()
22+
{
23+
return $this->getMock('Gitlab\HttpClient\HttpClient', array(), array(null, array(), $this->getHttpClientMock()));
24+
}
25+
26+
/**
27+
* @return \PHPUnit_Framework_MockObject_MockObject|Curl
28+
*/
29+
protected function getHttpClientMock()
1330
{
1431
$httpClient = $this->getMock('Buzz\Client\Curl', array('send'));
1532
$httpClient
1633
->expects($this->any())
1734
->method('send');
1835

19-
$mock = $this->getMock('Gitlab\HttpClient\HttpClient', array(), array(null, array(), $httpClient));
20-
21-
$client = new Client($mock);
22-
$client->setHttpClient($mock);
23-
24-
$methods = array_merge(array('get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'), $methods);
25-
26-
return $this->getMockBuilder($this->getApiClass())
27-
->setMethods($methods)
28-
->setConstructorArgs(array($client))
29-
->getMock()
30-
;
36+
return $httpClient;
3137
}
3238
}

test/Gitlab/Tests/Api/UsersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Gitlab\Api\AbstractApi;
44

5-
class UsersTest extends TestCase
5+
class UsersTest extends ApiTestCase
66
{
77
/**
88
* @test

0 commit comments

Comments
 (0)