Skip to content

Commit

Permalink
unit tests for OpenProjectAPIController::getNotifications()
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Neumann <artur@jankaritech.com>
  • Loading branch information
individual-it committed Jan 28, 2022
1 parent d973906 commit 5c1e326
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$classLoader->addPsr4("OCP\\", $serverPath . '/lib/public', true);
$classLoader->addPsr4("OC\\", $serverPath . '/lib/private', true);
$classLoader->addPsr4("OCA\\OpenProject\\AppInfo\\", __DIR__ . '/lib/AppInfo', true);
$classLoader->addPsr4("OCA\\OpenProject\\Controller\\", __DIR__ . '/lib/Controller', true);
$classLoader->register();

set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/share/php');
96 changes: 96 additions & 0 deletions tests/lib/Controller/OpenProjectAPIControllerTest.php
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());
}

}

0 comments on commit 5c1e326

Please sign in to comment.