Skip to content

Commit 99df80d

Browse files
committed
Added unit tests for most of APIs classes.
1 parent 15a9418 commit 99df80d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

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

test/Github/Tests/Api/CommitTest.php

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
/**
8+
* CurrentUser deploy keys api test case
9+
*
10+
* @author Leszek Prabucki <[email protected]>
11+
*/
12+
class DeployKeysTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*/
17+
public function shouldShowKey()
18+
{
19+
$expectedValue = array('id' => '12', 'key' => 'ssh-rsa ...');
20+
21+
$api = $this->getApiMock();
22+
$api->expects($this->once())
23+
->method('get')
24+
->with('user/keys/12')
25+
->will($this->returnValue($expectedValue));
26+
27+
$this->assertEquals($expectedValue, $api->show(12));
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function shouldGetKeys()
34+
{
35+
$expectedValue = array(array('id' => '12', 'key' => 'ssh-rsa ...'));
36+
37+
$api = $this->getApiMock();
38+
$api->expects($this->once())
39+
->method('get')
40+
->with('user/keys')
41+
->will($this->returnValue($expectedValue));
42+
43+
$this->assertEquals($expectedValue, $api->all());
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function shouldCreateKey()
50+
{
51+
$expectedValue = array('id' => '123', 'key' => 'ssh-rsa ...');
52+
$data = array('title' => 'my key', 'key' => 'ssh-rsa ...');
53+
54+
$api = $this->getApiMock();
55+
$api->expects($this->once())
56+
->method('post')
57+
->with('user/keys', $data)
58+
->will($this->returnValue($expectedValue));
59+
60+
$this->assertEquals($expectedValue, $api->create($data));
61+
}
62+
63+
/**
64+
* @test
65+
* @expectedException Github\Exception\MissingArgumentException
66+
*/
67+
public function shouldNotCreateKeyWithoutTitleParam()
68+
{
69+
$data = array('key' => 'ssh-rsa ...');
70+
71+
$api = $this->getApiMock();
72+
$api->expects($this->never())
73+
->method('post');
74+
75+
$api->create($data);
76+
}
77+
78+
/**
79+
* @test
80+
* @expectedException Github\Exception\MissingArgumentException
81+
*/
82+
public function shouldNotCreateKeyWithoutKeyParam()
83+
{
84+
$data = array('title' => 'my key');
85+
86+
$api = $this->getApiMock();
87+
$api->expects($this->never())
88+
->method('post');
89+
90+
$api->create($data);
91+
}
92+
93+
/**
94+
* @test
95+
*/
96+
public function shouldUpdateKey()
97+
{
98+
$expectedValue = array('id' => '123', 'key' => 'ssh-rsa ...');
99+
$data = array('title' => 'my key', 'key' => 'ssh-rsa ...');
100+
101+
$api = $this->getApiMock();
102+
$api->expects($this->once())
103+
->method('patch')
104+
->with('user/keys/123', $data)
105+
->will($this->returnValue($expectedValue));
106+
107+
$this->assertEquals($expectedValue, $api->update(123, $data));
108+
}
109+
110+
/**
111+
* @test
112+
* @expectedException Github\Exception\MissingArgumentException
113+
*/
114+
public function shouldNotUpdateKeyWithoutTitleParam()
115+
{
116+
$data = array('key' => 'ssh-rsa ...');
117+
118+
$api = $this->getApiMock();
119+
$api->expects($this->never())
120+
->method('patch');
121+
122+
$api->update(123, $data);
123+
}
124+
125+
/**
126+
* @test
127+
* @expectedException Github\Exception\MissingArgumentException
128+
*/
129+
public function shouldNotUpdateKeyWithoutKeyParam()
130+
{
131+
$data = array('title' => 'my key');
132+
133+
$api = $this->getApiMock();
134+
$api->expects($this->never())
135+
->method('patch');
136+
137+
$api->update(123, $data);
138+
}
139+
140+
/**
141+
* @test
142+
*/
143+
public function shouldRemoveKey()
144+
{
145+
$expectedValue = array('some value');
146+
147+
$api = $this->getApiMock();
148+
$api->expects($this->once())
149+
->method('delete')
150+
->with('user/keys/123')
151+
->will($this->returnValue($expectedValue));
152+
153+
$this->assertEquals($expectedValue, $api->remove(123));
154+
}
155+
156+
protected function getApiClass()
157+
{
158+
return 'Github\Api\CurrentUser\DeployKeys';
159+
}
160+
}

0 commit comments

Comments
 (0)