Skip to content

Commit 04ee86b

Browse files
committed
Added findVersionByName method
1 parent 3701b07 commit 04ee86b

File tree

2 files changed

+80
-25
lines changed

2 files changed

+80
-25
lines changed

src/Jira/Api.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,33 @@ public function getVersions($projectKey)
347347
return $this->api(self::REQUEST_GET, "/rest/api/2/project/{$projectKey}/versions", array(), true);
348348
}
349349

350+
/**
351+
* Helper method to find a specific version based on the name of the version.
352+
*
353+
* @param string $projectKey Project Key
354+
* @param string $name The version name to match on
355+
*
356+
* @return int|null VersionId on match or null when there is no match
357+
*/
358+
public function findVersionByName($projectKey, $name)
359+
{
360+
// Fetch all versions of this project
361+
$versions = $this->getVersions($projectKey);
362+
363+
// Filter on name in the PHP (Can't be done using the API?)
364+
$matching_versions = array_filter($versions, function (array $version) use ($name) {
365+
return $version['name'] == $name;
366+
});
367+
368+
// Early out for no results
369+
if (empty($matching_versions)) {
370+
return null;
371+
}
372+
373+
// Multiple results should not happen since name is unique
374+
return reset($matching_versions);
375+
}
376+
350377
/**
351378
* Get available priorities
352379
*
@@ -514,7 +541,6 @@ public function releaseVersion($versionId, $releaseDate = null, $params = array(
514541
$releaseDate = date("Y-m-d");
515542
}
516543

517-
// TODO Not entirely sure what to do yet with userReleaseDate
518544
$params = array_merge(
519545
array(
520546
"releaseDate" => $releaseDate,

tests/Jira/ApiTest.php

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ public function testSetEndpointTrailingSlash()
3131
*/
3232
public function testUpdateVersion()
3333
{
34-
$params = [
35-
"released" => true,
36-
"releaseDate" => "2010-07-06",
37-
];
34+
$params = array(
35+
'released' => true,
36+
'releaseDate' => '2010-07-06',
37+
);
3838

3939
// Stub the api method and keep the rest intact
4040
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
41-
$api = $this->getMockBuilder(Api::class)->setMethods(["api"])->disableOriginalConstructor()->getMock();
42-
$api->expects($this->once())->method("api")->with(
41+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
42+
$api->expects($this->once())->method('api')->with(
4343
$this->equalTo(Api::REQUEST_PUT),
44-
$this->equalTo("/rest/api/2/version/111000"),
44+
$this->equalTo('/rest/api/2/version/111000'),
4545
$this->equalTo($params)
4646
);
4747

@@ -53,17 +53,17 @@ public function testUpdateVersion()
5353
*/
5454
public function testReleaseVersion()
5555
{
56-
$params = [
57-
"released" => true,
58-
"releaseDate" => date("Y-m-d"),
59-
];
56+
$params = array(
57+
'released' => true,
58+
'releaseDate' => date('Y-m-d'),
59+
);
6060

6161
// Stub the api method and keep the rest intact
6262
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
63-
$api = $this->getMockBuilder(Api::class)->setMethods(["api"])->disableOriginalConstructor()->getMock();
64-
$api->expects($this->once())->method("api")->with(
63+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
64+
$api->expects($this->once())->method('api')->with(
6565
$this->equalTo(Api::REQUEST_PUT),
66-
$this->equalTo("/rest/api/2/version/111000"),
66+
$this->equalTo('/rest/api/2/version/111000'),
6767
$this->equalTo($params)
6868
);
6969

@@ -75,23 +75,52 @@ public function testReleaseVersion()
7575
*/
7676
public function testReleaseVersionAdvanced()
7777
{
78-
$releaseDate = "2010-07-06";
78+
$releaseDate = '2010-07-06';
7979

80-
$params = [
81-
"released" => true,
82-
"releaseDate" => $releaseDate,
83-
"test" => "extra"
84-
];
80+
$params = array(
81+
'released' => true,
82+
'releaseDate' => $releaseDate,
83+
'test' => 'extra'
84+
);
8585

8686
// Stub the api method and keep the rest intact
8787
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
88-
$api = $this->getMockBuilder(Api::class)->setMethods(["api"])->disableOriginalConstructor()->getMock();
89-
$api->expects($this->once())->method("api")->with(
88+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('api'))->disableOriginalConstructor()->getMock();
89+
$api->expects($this->once())->method('api')->with(
9090
$this->equalTo(Api::REQUEST_PUT),
91-
$this->equalTo("/rest/api/2/version/111000"),
91+
$this->equalTo('/rest/api/2/version/111000'),
9292
$this->equalTo($params)
9393
);
9494

95-
$api->releaseVersion(111000, $releaseDate, ["test" => "extra"]);
95+
$api->releaseVersion(111000, $releaseDate, array('test' => 'extra'));
96+
}
97+
98+
/**
99+
* Tests FindVersionByName
100+
*/
101+
public function testFindVersionByName()
102+
{
103+
$name = '3.36.0';
104+
$versionId = '14206';
105+
$projectKey = 'POR';
106+
107+
$versions = array(
108+
array('id' => '14205', 'name' => '3.62.0'),
109+
array('id' => $versionId, 'name' => $name),
110+
array('id' => '14207', 'name' => '3.66.0'),
111+
);
112+
113+
// Stub the getVersions method and keep the rest intact
114+
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $api */
115+
$api = $this->getMockBuilder('\chobie\Jira\Api')->setMethods(array('getVersions'))->disableOriginalConstructor()->getMock();
116+
$api->expects($this->exactly(2))->method('getVersions')->with(
117+
$this->equalTo($projectKey)
118+
)->willReturn($versions);
119+
120+
// He should find this one
121+
$this->assertEquals(array('id' => $versionId, 'name' => $name), $api->findVersionByName($projectKey, $name));
122+
123+
// And there should be no result for this one
124+
$this->assertNull($api->findVersionByName($projectKey, 'i_do_not_exist'));
96125
}
97126
}

0 commit comments

Comments
 (0)