Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests for OpenProjectAPIService::searchWorkPackage() #27

Merged
merged 1 commit into from
Jan 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 78 additions & 16 deletions tests/lib/Service/OpenProjectAPIServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ function setUpMocks(): void {
);
}

/**
* @param array $onlyMethods
* @return OpenProjectAPIService|\PHPUnit\Framework\MockObject\MockObject
*/
private function getServiceMock(array $onlyMethods = ['request']): OpenProjectAPIService {
return $this->getMockBuilder(OpenProjectAPIService::class)
->disableOriginalConstructor()
->onlyMethods($onlyMethods)
->getMock();
}

public function urlsDataProvider(): array {
return [
['http://127.0.0.1', true],
Expand Down Expand Up @@ -111,6 +122,69 @@ public function testValidateOpenProjectURL(string $url, bool $expected) {
$this->assertSame($expected, $result);
}

public function searchWorkPackageDataProvider() {
return [
[ // description and subject search, both return a result
["_embedded" => ["elements" => [['id' => 1], ['id' => 2], ['id' => 3]]]],
["_embedded" => ["elements" => [['id' => 3], ['id' => 4], ['id' => 5]]]],
[['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4], ['id' => 5]]
],
[ // only subject search returns a result
[],
["_embedded" => ["elements" => [['id' => 3], ['id' => 4], ['id' => 5]]]],
[['id' => 3], ['id' => 4], ['id' => 5]]
],
[ // only description search returns a result
["_embedded" => ["elements" => [['id' => 1], ['id' => 2], ['id' => 3]]]],
[],
[['id' => 1], ['id' => 2], ['id' => 3]]
],
[ // no search result returned
[],
[],
[]
]
];
}

/**
* @param array $descriptionResponse
* @param array $subjectResponse
* @param array $expectedResult
* @return void
* @dataProvider searchWorkPackageDataProvider
*/
public function testSearchWorkPackageDescAndSubjectResponse(
array $descriptionResponse, array $subjectResponse, array $expectedResult
) {
$service = $this->getServiceMock();
$service->method('request')
->withConsecutive(
[
'url','token', 'type', 'refresh', 'id', 'secret', 'user', 'work_packages',
[
'filters' => '[{"description":{"operator":"~","values":["search query"]}},{"status":{"operator":"!","values":["14"]}}]',
'sortBy' => '[["updatedAt", "desc"]]',
]
],
[
'url','token', 'type', 'refresh', 'id', 'secret', 'user', 'work_packages',
[
'filters' => '[{"subject":{"operator":"~","values":["search query"]}},{"status":{"operator":"!","values":["14"]}}]',
'sortBy' => '[["updatedAt", "desc"]]',
]
]
)
->willReturnOnConsecutiveCalls(
$descriptionResponse,
$subjectResponse
);
$result = $service->searchWorkPackage(
'url','token', 'type', 'refresh', 'id', 'secret', 'user','search query'
);
$this->assertSame($expectedResult, $result);
}

public function testGetNotificationsRequest() {
$consumerRequest = new ConsumerRequest();
$consumerRequest
Expand Down Expand Up @@ -154,32 +228,23 @@ public function malformedResponsesDataProvider() {
* @dataProvider malformedResponsesDataProvider
*/
public function testGetNotificationsMalformedResponse($response) {
$service = $this->getMockBuilder(OpenProjectAPIService::class)
->disableOriginalConstructor()
->onlyMethods(['request'])
->getMock();
$service = $this->getServiceMock();
$service->method('request')
->willReturn($response);
$result = $service->getNotifications('', '', '', '', '', '', '');
$this->assertSame(["error" => "Malformed response"], $result);
}

public function testGetNotificationsErrorResponse() {
$service = $this->getMockBuilder(OpenProjectAPIService::class)
->disableOriginalConstructor()
->onlyMethods(['request'])
->getMock();
$service = $this->getServiceMock();
$service->method('request')
->willReturn(['error' => 'my error']);
$result = $service->getNotifications('', '', '', '', '', '', '');
$this->assertSame(["error" => "my error"], $result);
}

public function testGetNotificationsFilters() {
$service = $this->getMockBuilder(OpenProjectAPIService::class)
->disableOriginalConstructor()
->onlyMethods(['request', 'now'])
->getMock();
$service = $this->getServiceMock(['request', 'now']);
$service->method('now')
->willReturn("2022-01-27T08:15:48Z");
$service->expects($this->once())
Expand All @@ -195,10 +260,7 @@ public function testGetNotificationsFilters() {
}

public function testGetNotificationsLimit() {
$service = $this->getMockBuilder(OpenProjectAPIService::class)
->disableOriginalConstructor()
->onlyMethods(['request'])
->getMock();
$service = $this->getServiceMock();
$service->method('request')
->willReturn(["_embedded" => ["elements" => [['id' => 1], ['id' => 2], ['id' => 3]]]]);
$result = $service->getNotifications('', '', '', '', '', '', '','',2);
Expand Down