Skip to content

Commit 3e15c73

Browse files
committed
Use User Api in IssueApi
1 parent bdd04df commit 3e15c73

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

src/Redmine/Api/Issue.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Issue extends AbstractApi
4747
*/
4848
private $trackerApi;
4949

50+
/**
51+
* @var User
52+
*/
53+
private $userApi;
54+
5055
/**
5156
* List issues.
5257
*
@@ -304,15 +309,16 @@ private function cleanParams(array $params = [])
304309
}
305310

306311
if (isset($params['assigned_to'])) {
307-
/** @var User */
308-
$apiUser = $this->client->getApi('user');
309-
$params['assigned_to_id'] = $apiUser->getIdByUsername($params['assigned_to']);
312+
$userApi = $this->getUserApi();
313+
314+
$params['assigned_to_id'] = $userApi->getIdByUsername($params['assigned_to']);
310315
unset($params['assigned_to']);
311316
}
317+
312318
if (isset($params['author'])) {
313-
/** @var User */
314-
$apiUser = $this->client->getApi('user');
315-
$params['author_id'] = $apiUser->getIdByUsername($params['author']);
319+
$userApi = $this->getUserApi();
320+
321+
$params['author_id'] = $userApi->getIdByUsername($params['author']);
316322
unset($params['author']);
317323
}
318324

@@ -447,4 +453,23 @@ private function getTrackerApi()
447453

448454
return $this->trackerApi;
449455
}
456+
457+
/**
458+
* @return User
459+
*/
460+
private function getUserApi()
461+
{
462+
if ($this->userApi === null) {
463+
if ($this->client !== null && ! $this->client instanceof NativeCurlClient && ! $this->client instanceof Psr18Client) {
464+
/** @var User */
465+
$userApi = $this->client->getApi('user');
466+
} else {
467+
$userApi = new User($this->getHttpClient());
468+
}
469+
470+
$this->userApi = $userApi;
471+
}
472+
473+
return $this->userApi;
474+
}
450475
}

tests/Unit/Api/IssueTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ public function testCreateCallsPost()
437437
* @covers ::getIssueCategoryApi
438438
* @covers ::getIssueStatusApi
439439
* @covers ::getProjectApi
440+
* @covers ::getTrackerApi
441+
* @covers ::getUserApi
440442
* @test
441443
*/
442444
public function testCreateWithClientCleansParameters()
@@ -467,7 +469,7 @@ public function testCreateWithClientCleansParameters()
467469
->willReturn('cleanedValue');
468470

469471
$client = $this->createMock(Client::class);
470-
$client->expects($this->exactly(6))
472+
$client->expects($this->exactly(5))
471473
->method('getApi')
472474
->willReturnMap(
473475
[
@@ -722,6 +724,60 @@ public function testCreateWithHttpClientRetrievesTrackerId()
722724
);
723725
}
724726

727+
/**
728+
* @covers ::create
729+
* @covers ::cleanParams
730+
* @covers ::getUserApi
731+
* @test
732+
*/
733+
public function testCreateWithHttpClientRetrievesUserId()
734+
{
735+
$client = $this->createMock(HttpClient::class);
736+
$client->expects($this->exactly(2))
737+
->method('request')
738+
->willReturnCallback(function (string $method, string $path, string $body = '') {
739+
if ($method === 'GET') {
740+
$this->assertSame('/users.json', $path);
741+
$this->assertSame('', $body);
742+
743+
return $this->createConfiguredMock(
744+
Response::class,
745+
[
746+
'getContentType' => 'application/json',
747+
'getBody' => '{"users":[{"login":"Author Name","id":5},{"login":"Assigned to User Name","id":6}]}',
748+
]
749+
);
750+
}
751+
752+
if ($method === 'POST') {
753+
$this->assertSame('/issues.xml', $path);
754+
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><issue><assigned_to_id>6</assigned_to_id><author_id>5</author_id></issue>', $body);
755+
756+
return $this->createConfiguredMock(
757+
Response::class,
758+
[
759+
'getContentType' => 'application/xml',
760+
'getBody' => '<?xml version="1.0"?><issue></issue>',
761+
]
762+
);
763+
}
764+
765+
throw new \Exception();
766+
});
767+
768+
// Create the object under test
769+
$api = new Issue($client);
770+
771+
$xmlElement = $api->create(['assigned_to' => 'Assigned to User Name', 'author' => 'Author Name']);
772+
773+
// Perform the tests
774+
$this->assertInstanceOf(SimpleXMLElement::class, $xmlElement);
775+
$this->assertXmlStringEqualsXmlString(
776+
'<?xml version="1.0"?><issue></issue>',
777+
$xmlElement->asXml(),
778+
);
779+
}
780+
725781
/**
726782
* Test create() and buildXML().
727783
*
@@ -844,7 +900,7 @@ public function testUpdateCleansParameters()
844900
->willReturn('cleanedValue');
845901

846902
$client = $this->createMock(Client::class);
847-
$client->expects($this->exactly(6))
903+
$client->expects($this->exactly(5))
848904
->method('getApi')
849905
->willReturnMap(
850906
[

0 commit comments

Comments
 (0)