1
0

timedjob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. class TestTimedJob extends \OC\BackgroundJob\TimedJob {
  10. private $testCase;
  11. /**
  12. * @param TimedJob $testCase
  13. */
  14. public function __construct($testCase) {
  15. $this->setInterval(10);
  16. $this->testCase = $testCase;
  17. }
  18. public function run($argument) {
  19. $this->testCase->markRun();
  20. }
  21. }
  22. class TimedJob extends \Test\TestCase {
  23. /**
  24. * @var DummyJobList $jobList
  25. */
  26. private $jobList;
  27. /**
  28. * @var \OC\BackgroundJob\TimedJob $job
  29. */
  30. private $job;
  31. private $jobRun = false;
  32. public function markRun() {
  33. $this->jobRun = true;
  34. }
  35. protected function setup() {
  36. parent::setUp();
  37. $this->jobList = new DummyJobList();
  38. $this->job = new TestTimedJob($this);
  39. $this->jobList->add($this->job);
  40. $this->jobRun = false;
  41. }
  42. public function testShouldRunAfterInterval() {
  43. $this->job->setLastRun(time() - 12);
  44. $this->job->execute($this->jobList);
  45. $this->assertTrue($this->jobRun);
  46. }
  47. public function testShouldNotRunWithinInterval() {
  48. $this->job->setLastRun(time() - 5);
  49. $this->job->execute($this->jobList);
  50. $this->assertFalse($this->jobRun);
  51. }
  52. public function testShouldNotTwice() {
  53. $this->job->setLastRun(time() - 15);
  54. $this->job->execute($this->jobList);
  55. $this->assertTrue($this->jobRun);
  56. $this->jobRun = false;
  57. $this->job->execute($this->jobList);
  58. $this->assertFalse($this->jobRun);
  59. }
  60. }