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\Migration\BackgroundRepair;
  23. use OC\NeedsUpdateException;
  24. use OCP\ILogger;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. use Symfony\Component\EventDispatcher\GenericEvent;
  29. use Test\TestCase;
  30. class TestRepairStep implements IRepairStep {
  31. /**
  32. * Returns the step's name
  33. *
  34. * @return string
  35. * @since 9.1.0
  36. */
  37. public function getName() {
  38. return 'A test repair step';
  39. }
  40. /**
  41. * Run repair step.
  42. * Must throw exception on error.
  43. *
  44. * @since 9.1.0
  45. * @throws \Exception in case of failure
  46. */
  47. public function run(IOutput $output) {
  48. // TODO: Implement run() method.
  49. }
  50. }
  51. class BackgroundRepairTest extends TestCase {
  52. /** @var \OC\BackgroundJob\JobList|\PHPUnit\Framework\MockObject\MockObject */
  53. private $jobList;
  54. /** @var BackgroundRepair|\PHPUnit\Framework\MockObject\MockObject */
  55. private $job;
  56. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  57. private $logger;
  58. /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject $dispatcher */
  59. private $dispatcher;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->jobList = $this->getMockBuilder('OC\BackgroundJob\JobList')
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->logger = $this->getMockBuilder(ILogger::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
  69. $this->job = $this->getMockBuilder(BackgroundRepair::class)
  70. ->setConstructorArgs([$this->dispatcher])
  71. ->setMethods(['loadApp'])
  72. ->getMock();
  73. }
  74. public function testNoArguments() {
  75. $this->jobList->expects($this->once())->method('remove');
  76. $this->job->execute($this->jobList);
  77. }
  78. public function testAppUpgrading() {
  79. $this->jobList->expects($this->never())->method('remove');
  80. $this->job->expects($this->once())->method('loadApp')->with('test')->willThrowException(new NeedsUpdateException());
  81. $this->job->setArgument([
  82. 'app' => 'test',
  83. 'step' => 'j'
  84. ]);
  85. $this->job->execute($this->jobList);
  86. }
  87. public function testUnknownStep() {
  88. $this->dispatcher->expects($this->never())->method('dispatch');
  89. $this->jobList->expects($this->once())->method('remove');
  90. $this->logger->expects($this->once())->method('logException');
  91. $this->job->setArgument([
  92. 'app' => 'test',
  93. 'step' => 'j'
  94. ]);
  95. $this->job->execute($this->jobList, $this->logger);
  96. }
  97. public function testWorkingStep() {
  98. $this->dispatcher->expects($this->once())->method('dispatch')
  99. ->with('\OC\Repair::step', new GenericEvent('\OC\Repair::step', ['A test repair step']));
  100. $this->jobList->expects($this->once())->method('remove');
  101. $this->job->setArgument([
  102. 'app' => 'test',
  103. 'step' => '\Test\Migration\TestRepairStep'
  104. ]);
  105. $this->job->execute($this->jobList, $this->logger);
  106. }
  107. }