aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/BackgroundJob/JobListTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/BackgroundJob/JobListTest.php')
-rw-r--r--tests/lib/BackgroundJob/JobListTest.php64
1 files changed, 45 insertions, 19 deletions
diff --git a/tests/lib/BackgroundJob/JobListTest.php b/tests/lib/BackgroundJob/JobListTest.php
index a9d7df0e6f4..d816bf707e8 100644
--- a/tests/lib/BackgroundJob/JobListTest.php
+++ b/tests/lib/BackgroundJob/JobListTest.php
@@ -1,4 +1,7 @@
<?php
+
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -7,10 +10,13 @@
namespace Test\BackgroundJob;
+use OC\BackgroundJob\JobList;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\Server;
use Psr\Log\LoggerInterface;
use Test\TestCase;
@@ -24,28 +30,28 @@ class JobListTest extends TestCase {
/** @var \OC\BackgroundJob\JobList */
protected $instance;
- /** @var \OCP\IDBConnection */
+ /** @var IDBConnection */
protected $connection;
- /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
+ /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;
- /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
+ /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
protected $timeFactory;
private bool $ran = false;
protected function setUp(): void {
parent::setUp();
- $this->connection = \OC::$server->getDatabaseConnection();
+ $this->connection = Server::get(IDBConnection::class);
$this->clearJobsList();
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->instance = new \OC\BackgroundJob\JobList(
+ $this->instance = new JobList(
$this->connection,
$this->config,
$this->timeFactory,
- \OC::$server->get(LoggerInterface::class),
+ Server::get(LoggerInterface::class),
);
}
@@ -70,7 +76,7 @@ class JobListTest extends TestCase {
return $jobs;
}
- public function argumentProvider() {
+ public static function argumentProvider(): array {
return [
[null],
[false],
@@ -84,9 +90,9 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
public function testAddRemove($argument): void {
$existingJobs = $this->getAllSorted();
$job = new TestJob();
@@ -106,9 +112,9 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
public function testRemoveDifferentArgument($argument): void {
$existingJobs = $this->getAllSorted();
$job = new TestJob();
@@ -127,9 +133,9 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
public function testHas($argument): void {
$job = new TestJob();
$this->assertFalse($this->instance->has($job, $argument));
@@ -143,9 +149,9 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
public function testHasDifferentArgument($argument): void {
$job = new TestJob();
$this->instance->add($job, $argument);
@@ -153,7 +159,11 @@ class JobListTest extends TestCase {
$this->assertFalse($this->instance->has($job, 10));
}
- protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
+ protected function createTempJob($class,
+ $argument,
+ int $reservedTime = 0,
+ int $lastChecked = 0,
+ int $lastRun = 0): int {
if ($lastChecked === 0) {
$lastChecked = time();
}
@@ -163,11 +173,12 @@ class JobListTest extends TestCase {
->values([
'class' => $query->createNamedParameter($class),
'argument' => $query->createNamedParameter($argument),
- 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
+ 'last_run' => $query->createNamedParameter($lastRun, IQueryBuilder::PARAM_INT),
'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
]);
- $query->execute();
+ $query->executeStatement();
+ return $query->getLastInsertId();
}
public function testGetNext(): void {
@@ -200,6 +211,21 @@ class JobListTest extends TestCase {
$this->assertEquals(2, $nextJob->getArgument());
}
+ public function testGetNextSkipTimed(): void {
+ $job = new TestTimedJobNew($this->timeFactory);
+ $jobId = $this->createTempJob(get_class($job), 1, 123456789, 12345, 123456789 - 5);
+ $this->timeFactory->expects(self::atLeastOnce())
+ ->method('getTime')
+ ->willReturn(123456789);
+
+ $nextJob = $this->instance->getNext();
+
+ self::assertNull($nextJob);
+ $job = $this->instance->getById($jobId);
+ self::assertInstanceOf(TestTimedJobNew::class, $job);
+ self::assertEquals(123456789 - 5, $job->getLastRun());
+ }
+
public function testGetNextSkipNonExisting(): void {
$job = new TestJob();
$this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
@@ -215,9 +241,9 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
public function testGetById($argument): void {
$job = new TestJob();
$this->instance->add($job, $argument);
@@ -254,10 +280,10 @@ class JobListTest extends TestCase {
->method('getTime')
->willReturn(123456789);
- $job = new TestJob($this->timeFactory, $this, function () {
+ $job = new TestJob($this->timeFactory, $this, function (): void {
});
- $job2 = new TestJob($this->timeFactory, $this, function () {
+ $job2 = new TestJob($this->timeFactory, $this, function (): void {
});
$this->instance->add($job, 1);
@@ -287,10 +313,10 @@ class JobListTest extends TestCase {
return time();
});
- $job = new TestParallelAwareJob($this->timeFactory, $this, function () {
+ $job = new TestParallelAwareJob($this->timeFactory, $this, function (): void {
});
- $job2 = new TestParallelAwareJob($this->timeFactory, $this, function () {
+ $job2 = new TestParallelAwareJob($this->timeFactory, $this, function (): void {
});
$this->instance->add($job, 1);