1
0

joblist.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Copyright (c) 2014 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 JobList extends \Test\TestCase {
  10. /**
  11. * @var \OC\BackgroundJob\JobList
  12. */
  13. protected $instance;
  14. /**
  15. * @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject $config
  16. */
  17. protected $config;
  18. protected function setUp() {
  19. parent::setUp();
  20. $conn = \OC::$server->getDatabaseConnection();
  21. $this->config = $this->getMock('\OCP\IConfig');
  22. $this->instance = new \OC\BackgroundJob\JobList($conn, $this->config);
  23. }
  24. public function argumentProvider() {
  25. return array(
  26. array(null),
  27. array(false),
  28. array('foobar'),
  29. array(12),
  30. array(array(
  31. 'asd' => 5,
  32. 'foo' => 'bar'
  33. ))
  34. );
  35. }
  36. /**
  37. * @dataProvider argumentProvider
  38. * @param $argument
  39. */
  40. public function testAddRemove($argument) {
  41. $existingJobs = $this->instance->getAll();
  42. $job = new TestJob();
  43. $this->instance->add($job, $argument);
  44. $jobs = $this->instance->getAll();
  45. $this->assertCount(count($existingJobs) + 1, $jobs);
  46. $addedJob = $jobs[count($jobs) - 1];
  47. $this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
  48. $this->assertEquals($argument, $addedJob->getArgument());
  49. $this->instance->remove($job, $argument);
  50. $jobs = $this->instance->getAll();
  51. $this->assertEquals($existingJobs, $jobs);
  52. }
  53. /**
  54. * @dataProvider argumentProvider
  55. * @param $argument
  56. */
  57. public function testRemoveDifferentArgument($argument) {
  58. $existingJobs = $this->instance->getAll();
  59. $job = new TestJob();
  60. $this->instance->add($job, $argument);
  61. $jobs = $this->instance->getAll();
  62. $this->instance->remove($job, 10);
  63. $jobs2 = $this->instance->getAll();
  64. $this->assertEquals($jobs, $jobs2);
  65. $this->instance->remove($job, $argument);
  66. $jobs = $this->instance->getAll();
  67. $this->assertEquals($existingJobs, $jobs);
  68. }
  69. /**
  70. * @dataProvider argumentProvider
  71. * @param $argument
  72. */
  73. public function testHas($argument) {
  74. $job = new TestJob();
  75. $this->assertFalse($this->instance->has($job, $argument));
  76. $this->instance->add($job, $argument);
  77. $this->assertTrue($this->instance->has($job, $argument));
  78. $this->instance->remove($job, $argument);
  79. $this->assertFalse($this->instance->has($job, $argument));
  80. }
  81. /**
  82. * @dataProvider argumentProvider
  83. * @param $argument
  84. */
  85. public function testHasDifferentArgument($argument) {
  86. $job = new TestJob();
  87. $this->instance->add($job, $argument);
  88. $this->assertFalse($this->instance->has($job, 10));
  89. $this->instance->remove($job, $argument);
  90. }
  91. public function testGetLastJob() {
  92. $this->config->expects($this->once())
  93. ->method('getAppValue')
  94. ->with('backgroundjob', 'lastjob', 0)
  95. ->will($this->returnValue(15));
  96. $this->assertEquals(15, $this->instance->getLastJob());
  97. }
  98. public function testGetNext() {
  99. $job = new TestJob();
  100. $this->instance->add($job, 1);
  101. $this->instance->add($job, 2);
  102. $jobs = $this->instance->getAll();
  103. $savedJob1 = $jobs[count($jobs) - 2];
  104. $savedJob2 = $jobs[count($jobs) - 1];
  105. $this->config->expects($this->once())
  106. ->method('getAppValue')
  107. ->with('backgroundjob', 'lastjob', 0)
  108. ->will($this->returnValue($savedJob1->getId()));
  109. $nextJob = $this->instance->getNext();
  110. $this->assertEquals($savedJob2, $nextJob);
  111. $this->instance->remove($job, 1);
  112. $this->instance->remove($job, 2);
  113. }
  114. public function testGetNextWrapAround() {
  115. $job = new TestJob();
  116. $this->instance->add($job, 1);
  117. $this->instance->add($job, 2);
  118. $jobs = $this->instance->getAll();
  119. $savedJob2 = $jobs[count($jobs) - 1];
  120. $this->config->expects($this->once())
  121. ->method('getAppValue')
  122. ->with('backgroundjob', 'lastjob', 0)
  123. ->will($this->returnValue($savedJob2->getId()));
  124. $nextJob = $this->instance->getNext();
  125. $this->assertEquals($jobs[0], $nextJob);
  126. $this->instance->remove($job, 1);
  127. $this->instance->remove($job, 2);
  128. }
  129. /**
  130. * @dataProvider argumentProvider
  131. * @param $argument
  132. */
  133. public function testGetById($argument) {
  134. $job = new TestJob();
  135. $this->instance->add($job, $argument);
  136. $jobs = $this->instance->getAll();
  137. $addedJob = $jobs[count($jobs) - 1];
  138. $this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
  139. $this->instance->remove($job, $argument);
  140. }
  141. public function testSetLastRun() {
  142. $job = new TestJob();
  143. $this->instance->add($job);
  144. $jobs = $this->instance->getAll();
  145. $addedJob = $jobs[count($jobs) - 1];
  146. $timeStart = time();
  147. $this->instance->setLastRun($addedJob);
  148. $timeEnd = time();
  149. $addedJob = $this->instance->getById($addedJob->getId());
  150. $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
  151. $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
  152. $this->instance->remove($job);
  153. }
  154. }