RepairTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\Repair;
  9. use OC\Repair\Events\RepairErrorEvent;
  10. use OC\Repair\Events\RepairInfoEvent;
  11. use OC\Repair\Events\RepairStepEvent;
  12. use OC\Repair\Events\RepairWarningEvent;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Migration\IRepairStep;
  15. use Psr\Log\LoggerInterface;
  16. class TestRepairStep implements IRepairStep {
  17. private bool $warning;
  18. public function __construct(bool $warning = false) {
  19. $this->warning = $warning;
  20. }
  21. public function getName() {
  22. return 'Test Name';
  23. }
  24. public function run(\OCP\Migration\IOutput $out) {
  25. if ($this->warning) {
  26. $out->warning('Simulated warning');
  27. } else {
  28. $out->info('Simulated info');
  29. }
  30. }
  31. }
  32. class RepairTest extends TestCase {
  33. private Repair $repair;
  34. /** @var string[] */
  35. private array $outputArray = [];
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  39. $this->repair = new Repair($dispatcher, $this->createMock(LoggerInterface::class));
  40. $dispatcher->addListener(RepairWarningEvent::class, function (RepairWarningEvent $event) {
  41. $this->outputArray[] = 'warning: ' . $event->getMessage();
  42. });
  43. $dispatcher->addListener(RepairInfoEvent::class, function (RepairInfoEvent $event) {
  44. $this->outputArray[] = 'info: ' . $event->getMessage();
  45. });
  46. $dispatcher->addListener(RepairStepEvent::class, function (RepairStepEvent $event) {
  47. $this->outputArray[] = 'step: ' . $event->getStepName();
  48. });
  49. $dispatcher->addListener(RepairErrorEvent::class, function (RepairErrorEvent $event) {
  50. $this->outputArray[] = 'error: ' . $event->getMessage();
  51. });
  52. }
  53. public function testRunRepairStep() {
  54. $this->repair->addStep(new TestRepairStep(false));
  55. $this->repair->run();
  56. $this->assertEquals(
  57. [
  58. 'step: Test Name',
  59. 'info: Simulated info',
  60. ],
  61. $this->outputArray
  62. );
  63. }
  64. public function testRunRepairStepThatFail() {
  65. $this->repair->addStep(new TestRepairStep(true));
  66. $this->repair->run();
  67. $this->assertEquals(
  68. [
  69. 'step: Test Name',
  70. 'warning: Simulated warning',
  71. ],
  72. $this->outputArray
  73. );
  74. }
  75. public function testRunRepairStepsWithException() {
  76. $mock = $this->createMock(TestRepairStep::class);
  77. $mock->expects($this->any())
  78. ->method('run')
  79. ->will($this->throwException(new \Exception('Exception text')));
  80. $mock->expects($this->any())
  81. ->method('getName')
  82. ->willReturn('Exception Test');
  83. $this->repair->addStep($mock);
  84. $this->repair->addStep(new TestRepairStep(false));
  85. $thrown = false;
  86. try {
  87. $this->repair->run();
  88. } catch (\Exception $e) {
  89. $thrown = true;
  90. }
  91. $this->assertFalse($thrown);
  92. // jump out after exception
  93. $this->assertEquals(
  94. [
  95. 'step: Exception Test',
  96. 'error: Exception text',
  97. 'step: Test Name',
  98. 'info: Simulated info',
  99. ],
  100. $this->outputArray
  101. );
  102. }
  103. public function testRunRepairStepsContinueAfterWarning() {
  104. $this->repair->addStep(new TestRepairStep(true));
  105. $this->repair->addStep(new TestRepairStep(false));
  106. $this->repair->run();
  107. $this->assertEquals(
  108. [
  109. 'step: Test Name',
  110. 'warning: Simulated warning',
  111. 'step: Test Name',
  112. 'info: Simulated info',
  113. ],
  114. $this->outputArray
  115. );
  116. }
  117. }