JobTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\ILogger;
  10. class JobTest extends \Test\TestCase {
  11. private $run = false;
  12. protected function setUp(): void {
  13. parent::setUp();
  14. $this->run = false;
  15. }
  16. public function testRemoveAfterException() {
  17. $jobList = new DummyJobList();
  18. $e = new \Exception();
  19. $job = new TestJob($this, function () use ($e) {
  20. throw $e;
  21. });
  22. $jobList->add($job);
  23. $logger = $this->getMockBuilder(ILogger::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $logger->expects($this->once())
  27. ->method('logException')
  28. ->with($e);
  29. $this->assertCount(1, $jobList->getAll());
  30. $job->execute($jobList, $logger);
  31. $this->assertTrue($this->run);
  32. $this->assertCount(1, $jobList->getAll());
  33. }
  34. public function testRemoveAfterError() {
  35. $jobList = new DummyJobList();
  36. $job = new TestJob($this, function () {
  37. $test = null;
  38. $test->someMethod();
  39. });
  40. $jobList->add($job);
  41. $logger = $this->getMockBuilder(ILogger::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $logger->expects($this->once())
  45. ->method('logException')
  46. ->with($this->isInstanceOf(\Throwable::class));
  47. $this->assertCount(1, $jobList->getAll());
  48. $job->execute($jobList, $logger);
  49. $this->assertTrue($this->run);
  50. $this->assertCount(1, $jobList->getAll());
  51. }
  52. public function markRun() {
  53. $this->run = true;
  54. }
  55. }