2013-12-05 07:29:35 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2013-12-05 07:29:35 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-12-05 07:29:35 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\BackgroundJob;
|
|
|
|
|
|
2023-04-20 07:18:28 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2024-02-08 05:52:40 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-10-24 09:26:53 -04:00
|
|
|
|
2016-05-20 09:38:20 -04:00
|
|
|
class JobTest extends \Test\TestCase {
|
2013-12-05 07:29:35 -05:00
|
|
|
private $run = false;
|
2023-04-20 07:18:28 -04:00
|
|
|
private ITimeFactory $timeFactory;
|
2024-02-08 05:52:40 -05:00
|
|
|
private LoggerInterface $logger;
|
2013-12-05 07:29:35 -05:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
2013-12-05 07:29:35 -05:00
|
|
|
$this->run = false;
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->timeFactory = Server::get(ITimeFactory::class);
|
2024-02-08 05:52:40 -05:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
|
|
|
|
|
|
\OC::$server->registerService(LoggerInterface::class, fn ($c) => $this->logger);
|
2013-12-05 07:29:35 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveAfterException(): void {
|
2013-12-05 07:29:35 -05:00
|
|
|
$jobList = new DummyJobList();
|
2016-01-14 04:38:53 -05:00
|
|
|
$e = new \Exception();
|
2025-06-12 12:31:58 -04:00
|
|
|
$job = new TestJob($this->timeFactory, $this, function () use ($e): void {
|
2016-01-14 04:38:53 -05:00
|
|
|
throw $e;
|
2013-12-05 07:29:35 -05:00
|
|
|
});
|
|
|
|
|
$jobList->add($job);
|
|
|
|
|
|
2024-02-08 05:52:40 -05:00
|
|
|
$this->logger->expects($this->once())
|
2023-04-20 09:29:47 -04:00
|
|
|
->method('error');
|
2015-12-18 09:05:32 -05:00
|
|
|
|
2013-12-05 07:29:35 -05:00
|
|
|
$this->assertCount(1, $jobList->getAll());
|
2024-02-08 05:52:40 -05:00
|
|
|
$job->start($jobList);
|
2013-12-05 07:29:35 -05:00
|
|
|
$this->assertTrue($this->run);
|
2015-12-18 09:05:32 -05:00
|
|
|
$this->assertCount(1, $jobList->getAll());
|
2013-12-05 07:29:35 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveAfterError(): void {
|
2023-04-20 07:18:28 -04:00
|
|
|
$jobList = new DummyJobList();
|
2025-06-12 12:31:58 -04:00
|
|
|
$job = new TestJob($this->timeFactory, $this, function (): void {
|
2023-04-20 07:18:28 -04:00
|
|
|
$test = null;
|
|
|
|
|
$test->someMethod();
|
|
|
|
|
});
|
|
|
|
|
$jobList->add($job);
|
|
|
|
|
|
2024-02-08 05:52:40 -05:00
|
|
|
$this->logger->expects($this->once())
|
2023-04-20 09:29:47 -04:00
|
|
|
->method('error');
|
2023-04-20 07:18:28 -04:00
|
|
|
|
|
|
|
|
$this->assertCount(1, $jobList->getAll());
|
2024-02-08 05:52:40 -05:00
|
|
|
$job->start($jobList);
|
2023-04-20 07:18:28 -04:00
|
|
|
$this->assertTrue($this->run);
|
|
|
|
|
$this->assertCount(1, $jobList->getAll());
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 07:29:35 -05:00
|
|
|
public function markRun() {
|
|
|
|
|
$this->run = true;
|
|
|
|
|
}
|
|
|
|
|
}
|