Skip to content

Commit

Permalink
fix(migration): Don't redo app initialisation
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Aug 26, 2024
1 parent 2609f3d commit f17df62
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"psalm:clear": "psalm.phar --clear-cache && psalm.phar --clear-global-cache",
"psalm:fix": "psalm.phar --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"test:unit": "vendor/bin/phpunit -c tests/phpunit.xml"
"test:unit": "vendor/bin/phpunit --color -c tests/phpunit.xml"
},
"require-dev": {
"nextcloud/coding-standard": "^1.2",
Expand Down
18 changes: 16 additions & 2 deletions lib/Migration/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

namespace OCA\QuotaWarning\Migration;

use OCA\QuotaWarning\AppInfo\Application;
use OCA\QuotaWarning\Job\User;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Migration\IOutput;
Expand All @@ -37,11 +39,17 @@ class Install implements IRepairStep {

/** @var IJobList */
protected $jobList;
/** @var IConfig */
protected $config;

public function __construct(IUserManager $userManager,
IJobList $jobList) {
public function __construct(
IUserManager $userManager,
IJobList $jobList,
IConfig $config,
) {
$this->userManager = $userManager;
$this->jobList = $jobList;
$this->config = $config;
}

/**
Expand All @@ -59,6 +67,10 @@ public function getName(): string {
* @since 9.1.0
*/
public function run(IOutput $output): void {
if ($this->config->getAppValue(Application::APP_ID, 'initialised', 'no') === 'yes') {
return;
}

$output->startProgress();
$this->userManager->callForSeenUsers(function (IUser $user) use ($output) {
$this->jobList->add(
Expand All @@ -68,5 +80,7 @@ public function run(IOutput $output): void {
$output->advance();
});
$output->finishProgress();

$this->config->setAppValue(Application::APP_ID, 'initialised', 'yes');
}
}
11 changes: 6 additions & 5 deletions tests/AppInfo/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
Expand Down Expand Up @@ -53,12 +56,12 @@ protected function setUp(): void {
$this->container = $this->app->getContainer();
}

public function testContainerAppName() {
public function testContainerAppName(): void {
$this->app = new Application();
$this->assertEquals(Application::APP_ID, $this->container->getAppName());
}

public function dataContainerQuery() {
public function dataContainerQuery(): array {
return [
[Application::class, App::class],
[Notifier::class, INotifier::class],
Expand All @@ -70,10 +73,8 @@ public function dataContainerQuery() {

/**
* @dataProvider dataContainerQuery
* @param string $service
* @param string $expected
*/
public function testContainerQuery($service, $expected) {
public function testContainerQuery(string $service, string $expected): void {
$this->assertInstanceOf($expected, $this->container->query($service));
}
}
7 changes: 5 additions & 2 deletions tests/Job/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
Expand Down Expand Up @@ -49,11 +52,11 @@ protected function setUp(): void {
);
}

public function testInterval() {
public function testInterval(): void {
$this->assertSame(86400, self::invokePrivate($this->job, 'interval'));
}

public function testRun() {
public function testRun(): void {
$this->checkQuota->expects($this->once())
->method('check')
->with('test1');
Expand Down
49 changes: 39 additions & 10 deletions tests/Migration/InstallTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
Expand Down Expand Up @@ -26,45 +28,50 @@
use OCA\QuotaWarning\Job\User;
use OCA\QuotaWarning\Migration\Install;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Migration\IOutput;
use PHPUnit\Framework\MockObject\MockObject;

class InstallTest extends \Test\TestCase {
/** @var Install */
protected $migration;

/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
/** @var IJobList|MockObject */
protected $jobList;
/** @var IConfig|MockObject */
protected $config;
/** @var Install */
protected $migration;

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

$this->userManager = $this->createMock(IUserManager::class);
$this->jobList = $this->createMock(IJobList::class);
$this->config = $this->createMock(IConfig::class);

$this->migration = new Install(
$this->userManager,
$this->jobList
$this->jobList,
$this->config
);
}

public function testGetName() {
public function testGetName(): void {
$this->assertIsString($this->migration->getName());
}

protected function getUser($uid) {
protected function getUser(string $uid): MockObject {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn($uid);
return $user;
}

public function testRun() {
/** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $output */
public function testRun(): void {
/** @var IOutput|MockObject $output */
$output = $this->createMock(IOutput::class);
$output->expects($this->once())
->method('startProgress');
Expand All @@ -89,4 +96,26 @@ public function testRun() {

$this->migration->run($output);
}

public function testRunSkipped(): void {
/** @var IOutput|MockObject $output */
$output = $this->createMock(IOutput::class);
$output->expects($this->never())
->method('startProgress');
$output->expects($this->never())
->method('finishProgress');

$this->config->expects($this->once())
->method('getAppValue')
->with('quota_warning', 'initialised', 'no')
->willReturn('yes');

$this->userManager->expects($this->never())
->method('callForSeenUsers');

$this->jobList->expects($this->never())
->method('add');

$this->migration->run($output);
}
}
31 changes: 15 additions & 16 deletions tests/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
Expand Down Expand Up @@ -31,20 +33,21 @@
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;

class NotifierTest extends \Test\TestCase {
/** @var Notifier */
protected $notifier;

/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var IFactory|MockObject */
protected $factory;
/** @var CheckQuota|\PHPUnit_Framework_MockObject_MockObject */
/** @var CheckQuota|MockObject */
protected $checkQuota;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
/** @var IConfig|MockObject */
protected $config;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var IURLGenerator|MockObject */
protected $urlGenerator;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
/** @var IL10N|MockObject */
protected $l;

protected function setUp(): void {
Expand Down Expand Up @@ -72,8 +75,8 @@ protected function setUp(): void {
);
}

public function testPrepareWrongApp() {
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
public function testPrepareWrongApp(): void {
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand All @@ -87,8 +90,8 @@ public function testPrepareWrongApp() {
$this->notifier->prepare($notification, 'en');
}

public function testPrepareLessUsage() {
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
public function testPrepareLessUsage(): void {
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand All @@ -114,7 +117,7 @@ public function testPrepareLessUsage() {
$this->notifier->prepare($notification, 'en');
}

public function dataPrepare() {
public function dataPrepare(): array {
return [
[85.1, ' 85% ', 'app-dark.svg'],
[94.9, ' 95% ', 'app-warning.svg'],
Expand All @@ -124,12 +127,8 @@ public function dataPrepare() {

/**
* @dataProvider dataPrepare
*
* @param float $quota
* @param string $stringContains
* @param string $image
*/
public function testPrepare($quota, $stringContains, $image) {
public function testPrepare(float $quota, string $stringContains, string $image): void {
$this->checkQuota->expects($this->once())
->method('getRelativeQuotaUsage')
->with('user')
Expand All @@ -143,7 +142,7 @@ public function testPrepare($quota, $stringContains, $image) {
['quota_warning', 'alert_percentage', '95', '95'],
]);

/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);
$notification->expects($this->once())
->method('getApp')
Expand Down

0 comments on commit f17df62

Please sign in to comment.