BackgroundRepairTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 OC\BackgroundJob\JobList;
  23. use OC\Migration\BackgroundRepair;
  24. use OC\NeedsUpdateException;
  25. use OC\Repair;
  26. use OC\Repair\Events\RepairStepEvent;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\IRepairStep;
  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. private JobList $jobList;
  56. private BackgroundRepair $job;
  57. private LoggerInterface $logger;
  58. private IEventDispatcher $dispatcher;
  59. private ITimeFactory $time;
  60. private Repair $repair;
  61. protected function setUp(): void {
  62. parent::setUp();
  63. $this->jobList = $this->getMockBuilder(JobList::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  70. $this->time = $this->createMock(ITimeFactory::class);
  71. $this->time->method('getTime')
  72. ->willReturn(999999);
  73. $this->repair = new Repair($this->dispatcher, $this->logger);
  74. $this->job = $this->getMockBuilder(BackgroundRepair::class)
  75. ->setConstructorArgs([$this->repair, $this->time, $this->logger, $this->jobList])
  76. ->setMethods(['loadApp'])
  77. ->getMock();
  78. }
  79. public function testNoArguments() {
  80. $this->jobList->expects($this->once())->method('remove');
  81. $this->job->start($this->jobList);
  82. }
  83. public function testAppUpgrading() {
  84. $this->jobList->expects($this->never())->method('remove');
  85. $this->job->expects($this->once())->method('loadApp')->with('test')->willThrowException(new NeedsUpdateException());
  86. $this->job->setArgument([
  87. 'app' => 'test',
  88. 'step' => 'j'
  89. ]);
  90. $this->job->start($this->jobList);
  91. }
  92. public function testUnknownStep() {
  93. $this->dispatcher->expects($this->never())->method('dispatchTyped');
  94. $this->jobList->expects($this->once())->method('remove');
  95. $this->logger->expects($this->once())->method('error');
  96. $this->job->setArgument([
  97. 'app' => 'test',
  98. 'step' => 'j'
  99. ]);
  100. $this->job->start($this->jobList);
  101. }
  102. public function testWorkingStep() {
  103. $this->dispatcher->expects($this->once())->method('dispatchTyped')
  104. ->with($this->equalTo(new RepairStepEvent('A test repair step')));
  105. $this->jobList->expects($this->once())->method('remove');
  106. $this->job->setArgument([
  107. 'app' => 'test',
  108. 'step' => '\Test\Migration\TestRepairStep'
  109. ]);
  110. $this->job->start($this->jobList);
  111. }
  112. }