1
0

BackgroundRepairTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @author Thomas Müller <thomas.mueller@tmit.eu>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Migration;
  22. use OCP\AppFramework\Utility\ITimeFactory;
  23. use OCP\EventDispatcher\IEventDispatcher;
  24. use OCP\Migration\IOutput;
  25. use OCP\Migration\IRepairStep;
  26. use OC\BackgroundJob\JobList;
  27. use OC\Migration\BackgroundRepair;
  28. use OC\NeedsUpdateException;
  29. use OC\Repair\Events\RepairStepEvent;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Psr\Log\LoggerInterface;
  32. use Test\TestCase;
  33. class TestRepairStep implements IRepairStep {
  34. /**
  35. * Returns the step's name
  36. *
  37. * @return string
  38. * @since 9.1.0
  39. */
  40. public function getName() {
  41. return 'A test repair step';
  42. }
  43. /**
  44. * Run repair step.
  45. * Must throw exception on error.
  46. *
  47. * @since 9.1.0
  48. * @throws \Exception in case of failure
  49. */
  50. public function run(IOutput $output) {
  51. // TODO: Implement run() method.
  52. }
  53. }
  54. class BackgroundRepairTest extends TestCase {
  55. /** @var JobList|MockObject */
  56. private $jobList;
  57. /** @var BackgroundRepair|MockObject */
  58. private $job;
  59. /** @var LoggerInterface|MockObject */
  60. private $logger;
  61. /** @var IEventDispatcher|MockObject $dispatcher */
  62. private $dispatcher;
  63. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject $dispatcher */
  64. private $time;
  65. protected function setUp(): void {
  66. parent::setUp();
  67. $this->jobList = $this->getMockBuilder(JobList::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  74. $this->time = $this->createMock(ITimeFactory::class);
  75. $this->time->method('getTime')
  76. ->willReturn(999999);
  77. $this->job = $this->getMockBuilder(BackgroundRepair::class)
  78. ->setConstructorArgs([$this->dispatcher, $this->time, $this->logger, $this->jobList])
  79. ->setMethods(['loadApp'])
  80. ->getMock();
  81. }
  82. public function testNoArguments() {
  83. $this->jobList->expects($this->once())->method('remove');
  84. $this->job->start($this->jobList);
  85. }
  86. public function testAppUpgrading() {
  87. $this->jobList->expects($this->never())->method('remove');
  88. $this->job->expects($this->once())->method('loadApp')->with('test')->willThrowException(new NeedsUpdateException());
  89. $this->job->setArgument([
  90. 'app' => 'test',
  91. 'step' => 'j'
  92. ]);
  93. $this->job->start($this->jobList);
  94. }
  95. public function testUnknownStep() {
  96. $this->dispatcher->expects($this->never())->method('dispatchTyped');
  97. $this->jobList->expects($this->once())->method('remove');
  98. $this->logger->expects($this->once())->method('error');
  99. $this->job->setArgument([
  100. 'app' => 'test',
  101. 'step' => 'j'
  102. ]);
  103. $this->job->start($this->jobList);
  104. }
  105. public function testWorkingStep() {
  106. $this->dispatcher->expects($this->once())->method('dispatchTyped')
  107. ->with($this->equalTo(new RepairStepEvent('A test repair step')));
  108. $this->jobList->expects($this->once())->method('remove');
  109. $this->job->setArgument([
  110. 'app' => 'test',
  111. 'step' => '\Test\Migration\TestRepairStep'
  112. ]);
  113. $this->job->start($this->jobList);
  114. }
  115. }