You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepairTest.php 3.3KB

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