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.

RepairStepTest.php 3.0KB

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