Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

### Added
- Added unit tests for controllers
[#790](https://github.com/nextcloud/cookbook/pull/790) @christianlupus

### Fixed
- Mark app as compatible with Nextcloud 22
[#778](https://github.com/nextcloud/cookbook/pull/778) @christianlupus
Expand Down
8 changes: 1 addition & 7 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use OCP\AppFramework\Controller;

use OCA\Cookbook\Service\RecipeService;
use OCP\IURLGenerator;
use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Helper\RestParameterParser;

Expand All @@ -17,10 +16,6 @@ class ConfigController extends Controller {
* @var RecipeService
*/
private $service;
/**
* @var IURLGenerator
*/
private $urlGenerator;

/**
* @var DbCacheService
Expand All @@ -32,11 +27,10 @@ class ConfigController extends Controller {
*/
private $restParser;

public function __construct($AppName, IRequest $request, IURLGenerator $urlGenerator, RecipeService $recipeService, DbCacheService $dbCacheService, RestParameterParser $restParser) {
public function __construct($AppName, IRequest $request, RecipeService $recipeService, DbCacheService $dbCacheService, RestParameterParser $restParser) {
parent::__construct($AppName, $request);

$this->service = $recipeService;
$this->urlGenerator = $urlGenerator;
$this->dbCacheService = $dbCacheService;
$this->restParser = $restParser;
}
Expand Down
7 changes: 1 addition & 6 deletions lib/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ public function search($query) {
}

return new DataResponse($recipes, 200, ['Content-Type' => 'application/json']);
// TODO: Remove obsolete code below when this is ready
$response = new TemplateResponse($this->appName, 'content/search', ['query' => $query, 'recipes' => $recipes]);
$response->renderAs('blank');

return $response;
} catch (\Exception $e) {
return new DataResponse($e->getMessage(), 500);
}
Expand Down Expand Up @@ -237,7 +232,7 @@ public function tags($keywords) {

return new DataResponse($recipes, Http::STATUS_OK, ['Content-Type' => 'application/json']);
} catch (\Exception $e) {
error_log($e->getMessage());
// error_log($e->getMessage());
return new DataResponse($e->getMessage(), 500);
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function show($id) {
* @param $id
*
* @return DataResponse
* @todo Parameter id is never used. Fix that
*/
public function update($id) {
$this->dbCacheService->triggerCheck();
Expand Down Expand Up @@ -145,7 +146,7 @@ public function destroy($id) {

try {
$this->service->deleteRecipe($id);
return new DataResponse('Recipe ' . $_GET['id'] . ' deleted successfully', Http::STATUS_OK);
return new DataResponse('Recipe ' . $id . ' deleted successfully', Http::STATUS_OK);
} catch (\Exception $e) {
return new DataResponse($e->getMessage(), 502);
}
Expand Down
173 changes: 173 additions & 0 deletions tests/Unit/Controller/ConfigControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace OCA\Cookbook\tests\Unit\Controller;

use OCP\IRequest;
use PHPUnit\Framework\TestCase;
use OCA\Cookbook\Service\RecipeService;
use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Helper\RestParameterParser;
use PHPUnit\Framework\MockObject\MockObject;
use OCA\Cookbook\Controller\ConfigController;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use ReflectionProperty;

/**
* @coversDefaultClass OCA\Cookbook\Controller\ConfigController
* @covers ::<private>
* @covers ::<protected>
*/
class ConfigControllerTest extends TestCase {

/**
* @var ConfigController|MockObject
*/
private $sut;
/**
* @var RecipeService|MockObject
*/
private $recipeService;
/**
* @var DbCacheService|MockObject
*/
private $dbCacheService;
/**
* @var RestParameterParser|MockObject
*/
private $restParser;
/**
* @var IRequest|MockObject
*/
private $request;

public function setUp(): void {
parent::setUp();

$this->request = $this->createMock(IRequest::class);
$this->recipeService = $this->createMock(RecipeService::class);
$this->dbCacheService = $this->createMock(DbCacheService::class);
$this->restParser = $this->createMock(RestParameterParser::class);

$this->sut = new ConfigController('cookbook', $this->request, $this->recipeService, $this->dbCacheService, $this->restParser);
}

/**
* @covers ::__construct
*/
public function testConstructor(): void {
$this->ensurePropertyIsCorrect('service', $this->recipeService);
$this->ensurePropertyIsCorrect('dbCacheService', $this->dbCacheService);
$this->ensurePropertyIsCorrect('restParser', $this->restParser);
}

private function ensurePropertyIsCorrect(string $name, &$val) {
$property = new ReflectionProperty(ConfigController::class, $name);
$property->setAccessible(true);
$this->assertSame($val, $property->getValue($this->sut));
}

/**
* @covers ::reindex
*/
public function testReindex(): void {
$this->dbCacheService->expects($this->once())->method('updateCache');

/**
* @var Response $response
*/
$response = $this->sut->reindex();

$this->assertEquals(200, $response->getStatus());
}

/**
* @covers ::list
*/
public function testList(): void {
$this->dbCacheService->expects($this->once())->method('triggerCheck');

$folder = '/the/folder/to/check';
$interval = 5 * 60;
$printImage = true;

$expectedData = [
'folder' => $folder,
'update_interval' => $interval,
'print_image' => $printImage,
];

$this->recipeService->method('getUserFolderPath')->willReturn($folder);
$this->dbCacheService->method('getSearchIndexUpdateInterval')->willReturn($interval);
$this->recipeService->method('getPrintImage')->willReturn($printImage);

/**
* @var DataResponse $response
*/
$response = $this->sut->list();

$this->assertEquals(200, $response->getStatus());
$this->assertEquals($expectedData, $response->getData());
}

/**
* @dataProvider dataProviderConfig
* @covers ::config
*/
public function testConfig($data, $folderPath, $interval, $printImage): void {
$this->restParser->method('getParameters')->willReturn($data);

$this->dbCacheService->expects($this->once())->method('triggerCheck');

if (is_null($folderPath)) {
$this->recipeService->expects($this->never())->method('setUserFolderPath');
$this->dbCacheService->expects($this->never())->method('updateCache');
} else {
$this->recipeService->expects($this->once())->method('setUserFolderPath')->with($folderPath);
$this->dbCacheService->expects($this->once())->method('updateCache');
}

if (is_null($interval)) {
$this->recipeService->expects($this->never())->method('setSearchIndexUpdateInterval');
} else {
$this->recipeService->expects($this->once())->method('setSearchIndexUpdateInterval')->with($interval);
}

if (is_null($printImage)) {
$this->recipeService->expects($this->never())->method('setPrintImage');
} else {
$this->recipeService->expects($this->once())->method('setPrintImage')->with($printImage);
}

/**
* @var DataResponse $response
*/
$response = $this->sut->config();

$this->assertEquals(200, $response->getStatus());
}

public function dataProviderConfig() {
return [
'noChange' => [
[], null, null, null
],
'changeFolder' => [
['folder' => '/path/to/whatever'], '/path/to/whatever', null, null
],
'changeinterval' => [
['update_interval' => 15], null, 15, null
],
'changePrint' => [
['print_image' => true], null, null, true
],
'changeAll' => [
[
'folder' => '/my/custom/path',
'update_interval' => 12,
'print_image' => false
], '/my/custom/path', 12, false
],
];
}
}
Loading