JobTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 OCP\ILogger;
  11. class JobTest extends \Test\TestCase {
  12. private $run = false;
  13. private ITimeFactory $timeFactory;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->run = false;
  17. $this->timeFactory = \OC::$server->get(ITimeFactory::class);
  18. }
  19. public function testRemoveAfterException() {
  20. $jobList = new DummyJobList();
  21. $e = new \Exception();
  22. $job = new TestJob($this->timeFactory, $this, function () use ($e) {
  23. throw $e;
  24. });
  25. $jobList->add($job);
  26. $logger = $this->getMockBuilder(ILogger::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $logger->expects($this->once())
  30. ->method('error');
  31. $this->assertCount(1, $jobList->getAll());
  32. $job->execute($jobList, $logger);
  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. $logger = $this->getMockBuilder(ILogger::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $logger->expects($this->once())
  47. ->method('error');
  48. $this->assertCount(1, $jobList->getAll());
  49. $job->execute($jobList, $logger);
  50. $this->assertTrue($this->run);
  51. $this->assertCount(1, $jobList->getAll());
  52. }
  53. public function markRun() {
  54. $this->run = true;
  55. }
  56. }