1
0

JobTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\BackgroundJob;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use Psr\Log\LoggerInterface;
  11. class JobTest extends \Test\TestCase {
  12. private $run = false;
  13. private ITimeFactory $timeFactory;
  14. private LoggerInterface $logger;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->run = false;
  18. $this->timeFactory = \OCP\Server::get(ITimeFactory::class);
  19. $this->logger = $this->createMock(LoggerInterface::class);
  20. \OC::$server->registerService(LoggerInterface::class, fn ($c) => $this->logger);
  21. }
  22. public function testRemoveAfterException() {
  23. $jobList = new DummyJobList();
  24. $e = new \Exception();
  25. $job = new TestJob($this->timeFactory, $this, function () use ($e) {
  26. throw $e;
  27. });
  28. $jobList->add($job);
  29. $this->logger->expects($this->once())
  30. ->method('error');
  31. $this->assertCount(1, $jobList->getAll());
  32. $job->start($jobList);
  33. $this->assertTrue($this->run);
  34. $this->assertCount(1, $jobList->getAll());
  35. }
  36. public function testRemoveAfterError() {
  37. $jobList = new DummyJobList();
  38. $job = new TestJob($this->timeFactory, $this, function () {
  39. $test = null;
  40. $test->someMethod();
  41. });
  42. $jobList->add($job);
  43. $this->logger->expects($this->once())
  44. ->method('error');
  45. $this->assertCount(1, $jobList->getAll());
  46. $job->start($jobList);
  47. $this->assertTrue($this->run);
  48. $this->assertCount(1, $jobList->getAll());
  49. }
  50. public function markRun() {
  51. $this->run = true;
  52. }
  53. }