nextcloud/tests/lib/BackgroundJob/JobClassesRegistryTest.php
Benjamin Gaussorgues 8db8776f2d
feat(jobs): introduce background job classes registry
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
2026-05-29 09:14:48 +02:00

82 lines
2.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\BackgroundJob;
use InvalidArgumentException;
use OC\BackgroundJob\JobClassesRegistry;
use OCP\IDBConnection;
use OCP\Server;
use OCP\Snowflake\ISnowflakeGenerator;
use Override;
use Test\TestCase;
/**
* @package Test\BackgroundJob
*/
#[\PHPUnit\Framework\Attributes\Group('DB')]
class JobClassesRegistryTest extends TestCase {
private readonly IDBConnection $connection;
private readonly ISnowflakeGenerator $snowflakeGenerator;
private JobClassesRegistry $registry;
#[Override]
protected function setUp(): void {
parent::setUp();
$this->connection = Server::get(IDBConnection::class);
$this->snowflakeGenerator = Server::get(ISnowflakeGenerator::class);
$this->registry = new JobClassesRegistry($this->connection, $this->snowflakeGenerator);
}
public function testResolveNonExistingClass() {
$className = 'invalid_class_name_122278';
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class ' . $className . ' doesnt exists');
$this->registry->getId($className);
}
public function testResolveInvalidClass() {
$className = self::class;
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class ' . $className . ' isnt an instance of OCP\BackgroundJob\IJob');
$this->registry->getId($className);
}
public function testResolveValidClass() {
$className = DummyJob::class;
$classId = $this->registry->getId($className);
$this->assertIsString($classId);
$this->assertGreaterThan(0, $classId);
// Renew register. ID should stay the same
$this->registry = new JobClassesRegistry($this->connection, $this->snowflakeGenerator);
$newId = $this->registry->getId($className);
$this->assertEquals($classId, $newId);
}
public function testResolveValidId() {
$className = DummyJob::class;
$classId = $this->registry->getId($className);
$resolvedClass = $this->registry->getName($classId);
$this->assertEquals($className, $resolvedClass);
}
public function testResolveInvalidId() {
$classId = PHP_INT_MAX;
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class ID ' . $classId . ' doesnt match any class name');
$this->registry->getName($classId);
}
}