JobTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\BackgroundJob;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use Psr\Log\LoggerInterface;
  10. class JobTest extends \Test\TestCase {
  11. private $run = false;
  12. private ITimeFactory $timeFactory;
  13. private LoggerInterface $logger;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->run = false;
  17. $this->timeFactory = \OCP\Server::get(ITimeFactory::class);
  18. $this->logger = $this->createMock(LoggerInterface::class);
  19. \OC::$server->registerService(LoggerInterface::class, fn ($c) => $this->logger);
  20. }
  21. public function testRemoveAfterException(): void {
  22. $jobList = new DummyJobList();
  23. $e = new \Exception();
  24. $job = new TestJob($this->timeFactory, $this, function () use ($e) {
  25. throw $e;
  26. });
  27. $jobList->add($job);
  28. $this->logger->expects($this->once())
  29. ->method('error');
  30. $this->assertCount(1, $jobList->getAll());
  31. $job->start($jobList);
  32. $this->assertTrue($this->run);
  33. $this->assertCount(1, $jobList->getAll());
  34. }
  35. public function testRemoveAfterError(): void {
  36. $jobList = new DummyJobList();
  37. $job = new TestJob($this->timeFactory, $this, function () {
  38. $test = null;
  39. $test->someMethod();
  40. });
  41. $jobList->add($job);
  42. $this->logger->expects($this->once())
  43. ->method('error');
  44. $this->assertCount(1, $jobList->getAll());
  45. $job->start($jobList);
  46. $this->assertTrue($this->run);
  47. $this->assertCount(1, $jobList->getAll());
  48. }
  49. public function markRun() {
  50. $this->run = true;
  51. }
  52. }