RepairStepTest.php 3.0 KB

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