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: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
/vendor
/.php-cs-fixer.cache
/tests/.phpunit.result.cache
.php-cs-fixer.cache
.phpunit.result.cache
/tests/coverage
/build
.DS_store
1 change: 1 addition & 0 deletions .nextcloudignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ package-lock.json
screenshots
src
vendor
tests
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
"php": "8.1"
}
},
"autoload": {
"psr-4": {
"OCA\\PreviewGenerator\\": "lib/",
"OCA\\PreviewGenerator\\Tests\\": "tests/"
}
},
"scripts": {
"cs:fix": "php-cs-fixer fix",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"test:unit": "phpunit -c tests/phpunit.xml tests",
"test:unit:coverage": "XDEBUG_MODE=coverage phpunit -c tests/phpunit.xml tests"
"test:unit": "phpunit -c phpunit.xml tests",
"test:unit:coverage": "XDEBUG_MODE=coverage phpunit -c phpunit.xml tests"
},
"require-dev": {
"nextcloud/coding-standard": "^1.0.0",
Expand Down
7 changes: 4 additions & 3 deletions lib/BackgroundJob/PreviewJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

use OCA\PreviewGenerator\Service\ConfigService;
use OCA\PreviewGenerator\Service\PreGenerateService;
use OCA\PreviewGenerator\Support\OutputInterfaceLoggerAdapter;
use OCA\PreviewGenerator\Support\PreviewLimiter\CountLimiter;
use OCA\PreviewGenerator\Support\PreviewLimiter\ExecutionTimeLimiter;
use OCA\PreviewGenerator\Support\PreviewLimiter\MultiLimiter;
use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use Psr\Log\LoggerInterface;

class PreviewJob extends TimedJob {
private readonly PreviewLimiter $limiter;

public function __construct(
ITimeFactory $time,
private readonly PreGenerateService $preGenerateService,
private readonly OutputInterfaceLoggerAdapter $outputInterface,
private readonly LoggerInterface $logger,
private readonly ConfigService $configService,
) {
parent::__construct($time);
Expand Down Expand Up @@ -53,7 +53,8 @@ protected function run($argument) {
return;
}

$this->preGenerateService->setLogger($this->logger);
$this->preGenerateService->setLimiter($this->limiter);
$this->preGenerateService->preGenerate($this->outputInterface);
$this->preGenerateService->preGenerate();
}
}
15 changes: 5 additions & 10 deletions tests/phpunit.xml → phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="bootstrap.php"
bootstrap="./tests/Unit/bootstrap.php"
verbose="true"
timeoutForSmallTests="900"
timeoutForMediumTests="900"
Expand All @@ -13,18 +13,13 @@
>
<coverage>
<include>
<directory suffix=".php">../</directory>
<directory suffix=".php">./lib</directory>
</include>
<exclude>
<directory suffix=".php">../tests</directory>
<directory suffix=".php">../vendor</directory>
<file>../.php-cs-fixer.dist.php</file>
</exclude>
<report>
<html outputDirectory="./coverage"/>
<html outputDirectory="./tests/coverage"/>
</report>
</coverage>
<testsuite name="Preview generator tests">
<directory suffix="Test.php">.</directory>
<testsuite name="PreviewGenerator unit tests">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</phpunit>
119 changes: 119 additions & 0 deletions tests/Unit/BackgroundJob/PreviewJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests\Unit\BackgroundJob;

use OCA\PreviewGenerator\BackgroundJob\PreviewJob;
use OCA\PreviewGenerator\Service\ConfigService;
use OCA\PreviewGenerator\Service\PreGenerateService;
use OCA\PreviewGenerator\Support\PreviewLimiter\MultiLimiter;
use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class PreviewJobTest extends TestCase {
private PreviewJob $previewJob;

private ITimeFactory&MockObject $time;
private PreGenerateService&MockObject $preGenerateService;
private LoggerInterface&MockObject $logger;
private ConfigService&MockObject $configService;
private IJobList&MockObject $jobList;

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

$this->time = $this->createMock(ITimeFactory::class);
$this->preGenerateService = $this->createMock(PreGenerateService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->configService = $this->createMock(ConfigService::class);
$this->jobList = $this->createMock(IJobList::class);

$this->time->method('getTime')
->willReturnCallback(time(...));

$this->configService->expects(self::once())
->method('getMaxBackgroundJobPreviews')
->willReturn(100);
$this->configService->expects(self::once())
->method('getMaxBackgroundJobExecutionTime')
->willReturn(300);

$this->previewJob = new PreviewJob(
$this->time,
$this->preGenerateService,
$this->logger,
$this->configService,
);
}

public function testJobSettings(): void {
$this->assertEquals(300, $this->previewJob->getInterval());
$this->assertTrue($this->previewJob->isTimeSensitive());
}

public function testRun(): void {
$this->configService->method('isBackgroundJobDisabled')
->willReturn(false);
$this->configService->method('usesCronDaemon')
->willReturn(true);

$this->preGenerateService->expects(self::once())
->method('setLogger')
->with($this->logger);
$this->preGenerateService->expects(self::once())
->method('setLimiter')
->willReturnCallback(function (PreviewLimiter $limiter): void {
$this->assertInstanceOf(MultiLimiter::class, $limiter);
});

$this->preGenerateService->expects(self::once())
->method('preGenerate');

$this->previewJob->start($this->jobList);
}

/** @dataProvider runSkipsDataProvider */
public function testRunSkips(bool $isBackgroundJobDisabled, bool $usesCronDaemon): void {
$this->configService->method('isBackgroundJobDisabled')
->willReturn($isBackgroundJobDisabled);
$this->configService->method('usesCronDaemon')
->willReturn($usesCronDaemon);

$this->preGenerateService->expects(self::never())
->method('setLogger');
$this->preGenerateService->expects(self::never())
->method('setLimiter');

$this->preGenerateService->expects(self::never())
->method('preGenerate');

$this->previewJob->start($this->jobList);
}

public static function runSkipsDataProvider(): array {
return [
'background job is disabled' => [
'isBackgroundJobDisabled' => true,
'usesCronDaemon' => true,
],
'cron daemon is not used' => [
'isBackgroundJobDisabled' => false,
'usesCronDaemon' => false,
],
'background job is disabled and no cron daemon is used' => [
'isBackgroundJobDisabled' => true,
'usesCronDaemon' => false,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests\Service;
namespace OCA\PreviewGenerator\Tests\Unit\Service;

use OCA\PreviewGenerator\Service\ModuloService;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests\Service;
namespace OCA\PreviewGenerator\Tests\Unit\Service;

use OCA\PreviewGenerator\Service\NoMediaService;
use OCP\Files\File;
Expand Down
2 changes: 1 addition & 1 deletion tests/SizeHelperTest.php → tests/Unit/SizeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests;
namespace OCA\PreviewGenerator\Tests\Unit;

use OCA\PreviewGenerator\Service\ConfigService;
use OCA\PreviewGenerator\SizeHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests\Support\PreviewLimiter;
namespace OCA\PreviewGenerator\Tests\Unit\Support\PreviewLimiter;

use OCA\PreviewGenerator\Support\PreviewLimiter\CountLimiter;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests\Support\PreviewLimiter;
namespace OCA\PreviewGenerator\Tests\Unit\Support\PreviewLimiter;

use OC\AppFramework\Utility\TimeFactory;
use OCA\PreviewGenerator\Support\PreviewLimiter\ExecutionTimeLimiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Tests\Support\PreviewLimiter;
namespace OCA\PreviewGenerator\Tests\Unit\Support\PreviewLimiter;

use OCA\PreviewGenerator\Support\PreviewLimiter\MultiLimiter;
use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter;
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

use OCP\App\IAppManager;
use OCP\Server;

if (!defined('PHPUNIT_RUN')) {
define('PHPUNIT_RUN', 1);
}

require_once __DIR__ . '/../../../../lib/base.php';
require_once __DIR__ . '/../../../../tests/autoload.php';
require_once __DIR__ . '/../../vendor/autoload.php';

Server::get(IAppManager::class)->loadApp('previewgenerator');

OC_Hook::clear();
14 changes: 0 additions & 14 deletions tests/bootstrap.php

This file was deleted.

Loading