-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for OpenProjectAPIController::getNotifications()
Signed-off-by: Artur Neumann <artur@jankaritech.com>
- Loading branch information
1 parent
d973906
commit d779c06
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* Nextcloud - OpenProject | ||
* | ||
* | ||
* @author Artur Neumann <artur@jankaritech.com> | ||
* @copyright Artur Neumann 2022 | ||
*/ | ||
|
||
namespace OCA\OpenProject\Controller; | ||
|
||
use OCA\OpenProject\Service\OpenProjectAPIService; | ||
use OCP\IConfig; | ||
use OCP\IRequest; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class OpenProjectAPIControllerTest extends TestCase | ||
{ | ||
/** @var IConfig $configMock */ | ||
private $configMock; | ||
|
||
/** @var IRequest $requestMock */ | ||
private $requestMock; | ||
|
||
/** | ||
* @return void | ||
* @before | ||
*/ | ||
function setUpMocks(): void { | ||
$this->requestMock = $this->createMock(IRequest::class); | ||
$this->configMock = $this->getMockBuilder(IConfig::class)->getMock(); | ||
$this->configMock | ||
->method('getAppValue') | ||
->withConsecutive( | ||
['integration_openproject', 'client_id'], | ||
['integration_openproject', 'client_secret'], | ||
)->willReturnOnConsecutiveCalls('cliendID', 'clientSecret'); | ||
} | ||
|
||
public function getUserValueMock($token = '123') { | ||
$this->configMock | ||
->method('getUserValue') | ||
->withConsecutive( | ||
['test','integration_openproject', 'token'], | ||
['test','integration_openproject', 'token_type'], | ||
['test','integration_openproject', 'refresh_token'], | ||
['test','integration_openproject', 'url'], | ||
)->willReturnOnConsecutiveCalls($token,'oauth', 'refreshToken','http://openproject.org'); | ||
} | ||
|
||
public function testGetNotifications() { | ||
$this->getUserValueMock(); | ||
$service = $this->getMockBuilder(OpenProjectAPIService::class) | ||
->disableOriginalConstructor() | ||
->onlyMethods(['getNotifications']) | ||
->getMock(); | ||
$service->expects($this->once()) | ||
->method('getNotifications') | ||
->willReturn(['some' => 'data']); | ||
|
||
$controller = new OpenProjectAPIController( | ||
'integration_openproject', $this->requestMock, $this->configMock, $service, 'test' | ||
); | ||
$response = $controller->getNotifications(); | ||
$this->assertSame(200, $response->getStatus()); | ||
$this->assertSame(['some' => 'data'], $response->getData()); | ||
} | ||
|
||
public function testGetNotificationsNoAccessToken() { | ||
$this->getUserValueMock(''); | ||
$service = $this->createMock(OpenProjectAPIService::class); | ||
$controller = new OpenProjectAPIController( | ||
'integration_openproject', $this->requestMock, $this->configMock, $service, 'test' | ||
); | ||
$response = $controller->getNotifications(); | ||
$this->assertSame(400, $response->getStatus()); | ||
} | ||
|
||
public function testGetNotificationsErrorResponse() { | ||
$this->getUserValueMock(); | ||
$service = $this->getMockBuilder(OpenProjectAPIService::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$service | ||
->method('getNotifications') | ||
->willReturn(['error' => 'something went wrong']); | ||
|
||
$controller = new OpenProjectAPIController( | ||
'integration_openproject', $this->requestMock, $this->configMock, $service, 'test' | ||
); | ||
$response = $controller->getNotifications(); | ||
$this->assertSame(401, $response->getStatus()); | ||
$this->assertSame(['error' => 'something went wrong'], $response->getData()); | ||
} | ||
|
||
} |