Skip to content

Commit 0f15c8a

Browse files
author
Alexander Obuhovich
authored
Merge pull request #114 from aik099/api-test-use-proper-mocking
Use Prophecy for mocking in "Api" class tests
2 parents e3976a2 + 4b5b075 commit 0f15c8a

File tree

2 files changed

+120
-71
lines changed

2 files changed

+120
-71
lines changed

src/Jira/Api.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public function getVersions($project_key)
426426
* @param string $project_key Project Key.
427427
* @param string $name The version name to match on.
428428
*
429-
* @return integer|null VersionId on match or null when there is no match.
429+
* @return array|null Version data on match or null when there is no match.
430430
* @since 2.0.0
431431
*/
432432
public function findVersionByName($project_key, $name)
@@ -592,7 +592,7 @@ public function createVersion($project_key, $version, array $options = array())
592592
* @param integer $version_id Version ID.
593593
* @param array $params Key->Value list to update the version with.
594594
*
595-
* @return Result|false
595+
* @return false
596596
* @since 2.0.0
597597
* @link https://docs.atlassian.com/jira/REST/latest/#api/2/version-updateVersion
598598
*/
@@ -608,7 +608,7 @@ public function updateVersion($version_id, array $params = array())
608608
* @param string|null $release_date Date in Y-m-d format (defaults to today).
609609
* @param array $params Optionally extra parameters.
610610
*
611-
* @return Result|false
611+
* @return false
612612
* @since 2.0.0
613613
*/
614614
public function releaseVersion($version_id, $release_date = null, array $params = array())
@@ -858,7 +858,7 @@ public function getProjectComponents($project_key)
858858
{
859859
return $this->api(self::REQUEST_GET, sprintf('/rest/api/2/project/%s/components', $project_key), array(), true);
860860
}
861-
861+
862862
/**
863863
* Get all issue types with valid status values for a project.
864864
*
@@ -867,11 +867,11 @@ public function getProjectComponents($project_key)
867867
* @return array
868868
* @since 2.0.0
869869
*/
870-
public function getProjectIssueTypes($project_key)
870+
public function getProjectIssueTypes($project_key)
871871
{
872872
return $this->api(self::REQUEST_GET, sprintf('/rest/api/2/project/%s/statuses', $project_key), array(), true);
873873
}
874-
874+
875875
/**
876876
* Returns a list of all resolutions.
877877
*
@@ -880,7 +880,7 @@ public function getProjectIssueTypes($project_key)
880880
* @return array|false
881881
* @since 2.0.0
882882
*/
883-
public function getResolutions()
883+
public function getResolutions()
884884
{
885885
return $this->api(self::REQUEST_GET, '/rest/api/2/resolution', array(), true);
886886
}

tests/Jira/ApiTest.php

Lines changed: 113 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55

66
use chobie\Jira\Api;
7-
use chobie\Jira\Api\Authentication\Anonymous;
7+
use chobie\Jira\Api\Authentication\AuthenticationInterface;
8+
use Prophecy\Prophecy\ObjectProphecy;
89

910
/**
1011
* Class ApiTest
@@ -14,116 +15,164 @@
1415
class ApiTest extends \PHPUnit_Framework_TestCase
1516
{
1617

18+
const ENDPOINT = 'http://jira.company.com';
19+
20+
/**
21+
* Api.
22+
*
23+
* @var Api
24+
*/
25+
protected $api;
26+
1727
/**
18-
* Tests that any trailing slash in the endpoint url is removed before being stored in the object state
28+
* Credential.
29+
*
30+
* @var AuthenticationInterface
1931
*/
20-
public function testSetEndpointTrailingSlash()
32+
protected $credential;
33+
34+
/**
35+
* Client.
36+
*
37+
* @var ObjectProphecy
38+
*/
39+
protected $client;
40+
41+
protected function setUp()
2142
{
22-
$api = new Api('https://test.test/', new Anonymous(), null);
23-
$this->assertEquals('https://test.test', $api->getEndpoint());
43+
parent::setUp();
2444

25-
// Make sure nothing is removed if there is no trailing slash
26-
$url = 'https://urlwithouttrailing.slash';
27-
$api->setEndPoint($url);
28-
$this->assertEquals($url, $api->getEndpoint());
45+
$this->credential = $this->prophesize('chobie\Jira\Api\Authentication\AuthenticationInterface')->reveal();
46+
$this->client = $this->prophesize('chobie\Jira\Api\Client\ClientInterface');
47+
48+
$this->api = new Api(self::ENDPOINT, $this->credential, $this->client->reveal());
2949
}
3050

3151
/**
32-
* Tests that the updateVersion call constructs the correct api call
52+
* @dataProvider setEndpointDataProvider
3353
*/
54+
public function testSetEndpoint($given_endpoint, $used_endpoint)
55+
{
56+
$api = new Api($given_endpoint, $this->credential, $this->client->reveal());
57+
$this->assertEquals($used_endpoint, $api->getEndpoint());
58+
}
59+
60+
public function setEndpointDataProvider()
61+
{
62+
return array(
63+
'trailing slash removed' => array('https://test.test/', 'https://test.test'),
64+
'nothing removed' => array('https://test.test', 'https://test.test'),
65+
);
66+
}
67+
3468
public function testUpdateVersion()
3569
{
3670
$params = array(
37-
'released' => true,
38-
'releaseDate' => '2010-07-06',
71+
'overdue' => true,
72+
'description' => 'new description',
3973
);
4074

41-
// Stub the api method and keep the rest intact
42-
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
43-
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
44-
$api->expects($this->once())->method('api')->with(
45-
$this->equalTo(Api::REQUEST_PUT),
46-
$this->equalTo('/rest/api/2/version/111000'),
47-
$this->equalTo($params)
75+
$this->expectClientCall(
76+
Api::REQUEST_PUT,
77+
'/rest/api/2/version/111000',
78+
$params,
79+
''
4880
);
4981

50-
$api->updateVersion(111000, $params);
82+
$this->assertFalse($this->api->updateVersion(111000, $params));
5183
}
5284

53-
/**
54-
* Tests that the releaseVersion call constructs the correct api call
55-
*/
56-
public function testReleaseVersion()
85+
public function testReleaseVersionAutomaticReleaseDate()
5786
{
5887
$params = array(
5988
'released' => true,
6089
'releaseDate' => date('Y-m-d'),
6190
);
6291

63-
// Stub the api method and keep the rest intact
64-
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
65-
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
66-
$api->expects($this->once())->method('api')->with(
67-
$this->equalTo(Api::REQUEST_PUT),
68-
$this->equalTo('/rest/api/2/version/111000'),
69-
$this->equalTo($params)
92+
$this->expectClientCall(
93+
Api::REQUEST_PUT,
94+
'/rest/api/2/version/111000',
95+
$params,
96+
''
7097
);
7198

72-
$api->releaseVersion(111000);
99+
$this->assertFalse($this->api->releaseVersion(111000));
73100
}
74101

75-
/**
76-
* Tests that the releaseVersion call constructs the correct api call with overriden release data and params
77-
*/
78-
public function testReleaseVersionAdvanced()
102+
public function testReleaseVersionParameterMerging()
79103
{
80-
$releaseDate = '2010-07-06';
104+
$release_date = '2010-07-06';
81105

82-
$params = array(
106+
$expected_params = array(
83107
'released' => true,
84-
'releaseDate' => $releaseDate,
108+
'releaseDate' => $release_date,
85109
'test' => 'extra',
86110
);
87111

88-
// Stub the api method and keep the rest intact
89-
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
90-
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
91-
$api->expects($this->once())->method('api')->with(
92-
$this->equalTo(Api::REQUEST_PUT),
93-
$this->equalTo('/rest/api/2/version/111000'),
94-
$this->equalTo($params)
112+
$this->expectClientCall(
113+
Api::REQUEST_PUT,
114+
'/rest/api/2/version/111000',
115+
$expected_params,
116+
''
95117
);
96118

97-
$api->releaseVersion(111000, $releaseDate, array('test' => 'extra'));
119+
$this->assertFalse($this->api->releaseVersion(111000, $release_date, array('test' => 'extra')));
98120
}
99121

100-
/**
101-
* Tests FindVersionByName
102-
*/
103122
public function testFindVersionByName()
104123
{
105-
$name = '3.36.0';
106-
$versionId = '14206';
107-
$projectKey = 'POR';
124+
$project_key = 'POR';
125+
$version_id = '14206';
126+
$version_name = '3.36.0';
108127

109128
$versions = array(
110129
array('id' => '14205', 'name' => '3.62.0'),
111-
array('id' => $versionId, 'name' => $name),
130+
array('id' => $version_id, 'name' => $version_name),
112131
array('id' => '14207', 'name' => '3.66.0'),
113132
);
114133

115-
// Stub the getVersions method and keep the rest intact
116-
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
117-
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('getVersions'))->disableOriginalConstructor()->getMock();
118-
$api->expects($this->exactly(2))->method('getVersions')->with(
119-
$this->equalTo($projectKey)
120-
)->willReturn($versions);
134+
$this->expectClientCall(
135+
Api::REQUEST_GET,
136+
'/rest/api/2/project/' . $project_key . '/versions',
137+
array(),
138+
json_encode($versions)
139+
);
121140

122-
// He should find this one
123-
$this->assertEquals(array('id' => $versionId, 'name' => $name), $api->findVersionByName($projectKey, $name));
141+
$this->assertEquals(
142+
array('id' => $version_id, 'name' => $version_name),
143+
$this->api->findVersionByName($project_key, $version_name),
144+
'Version found'
145+
);
124146

125-
// And there should be no result for this one
126-
$this->assertNull($api->findVersionByName($projectKey, 'i_do_not_exist'));
147+
$this->assertNull(
148+
$this->api->findVersionByName($project_key, 'i_do_not_exist')
149+
);
150+
}
151+
152+
/**
153+
* Expects a particular client call.
154+
*
155+
* @param string $method Request method.
156+
* @param string $url URL.
157+
* @param array|string $data Request data.
158+
* @param string $return_value Return value.
159+
* @param boolean $is_file This is a file upload request.
160+
* @param boolean $debug Debug this request.
161+
*
162+
* @return void
163+
*/
164+
protected function expectClientCall(
165+
$method,
166+
$url,
167+
$data = array(),
168+
$return_value,
169+
$is_file = false,
170+
$debug = false
171+
) {
172+
$this->client
173+
->sendRequest($method, $url, $data, self::ENDPOINT, $this->credential, $is_file, $debug)
174+
->willReturn($return_value)
175+
->shouldBeCalled();
127176
}
128177

129178
}

0 commit comments

Comments
 (0)