2016-04-27 11:51:02 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
|
|
|
use OCP\Migration\IRepairStep;
|
2021-02-01 16:56:56 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-04-27 11:51:02 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
class RepairStepTest implements IRepairStep {
|
2016-04-27 11:51:02 +03:00
|
|
|
private $warning;
|
|
|
|
|
|
|
|
public function __construct($warning = false) {
|
|
|
|
$this->warning = $warning;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Test Name';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run(\OCP\Migration\IOutput $out) {
|
|
|
|
if ($this->warning) {
|
|
|
|
$out->warning('Simulated warning');
|
2020-04-10 15:19:56 +03:00
|
|
|
} else {
|
2016-04-27 11:51:02 +03:00
|
|
|
$out->info('Simulated info');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RepairTest extends TestCase {
|
|
|
|
/** @var \OC\Repair */
|
|
|
|
private $repair;
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
private $outputArray;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-04-27 11:51:02 +03:00
|
|
|
parent::setUp();
|
|
|
|
$dispatcher = new EventDispatcher();
|
2021-02-01 16:56:56 +03:00
|
|
|
$this->repair = new \OC\Repair([], $dispatcher, $this->createMock(LoggerInterface::class));
|
2016-04-27 11:51:02 +03:00
|
|
|
|
|
|
|
$dispatcher->addListener('\OC\Repair::warning', function ($event) {
|
|
|
|
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
|
|
|
|
$this->outputArray[] = 'warning: ' . $event->getArgument(0);
|
|
|
|
});
|
|
|
|
$dispatcher->addListener('\OC\Repair::info', function ($event) {
|
|
|
|
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
|
|
|
|
$this->outputArray[] = 'info: ' . $event->getArgument(0);
|
|
|
|
});
|
|
|
|
$dispatcher->addListener('\OC\Repair::step', function ($event) {
|
|
|
|
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
|
|
|
|
$this->outputArray[] = 'step: ' . $event->getArgument(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRunRepairStep() {
|
|
|
|
$this->repair->addStep(new TestRepairStep(false));
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2016-04-27 11:51:02 +03:00
|
|
|
'step: Test Name',
|
|
|
|
'info: Simulated info',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2016-04-27 11:51:02 +03:00
|
|
|
$this->outputArray
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRunRepairStepThatFail() {
|
|
|
|
$this->repair->addStep(new TestRepairStep(true));
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2016-04-27 11:51:02 +03:00
|
|
|
'step: Test Name',
|
|
|
|
'warning: Simulated warning',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2016-04-27 11:51:02 +03:00
|
|
|
$this->outputArray
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRunRepairStepsWithException() {
|
2017-04-20 13:27:32 +03:00
|
|
|
$mock = $this->createMock(TestRepairStep::class);
|
2016-04-27 11:51:02 +03:00
|
|
|
$mock->expects($this->any())
|
|
|
|
->method('run')
|
|
|
|
->will($this->throwException(new \Exception()));
|
|
|
|
$mock->expects($this->any())
|
|
|
|
->method('getName')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('Exception Test');
|
2016-04-27 11:51:02 +03:00
|
|
|
|
|
|
|
$this->repair->addStep($mock);
|
|
|
|
$this->repair->addStep(new TestRepairStep(false));
|
|
|
|
|
|
|
|
$thrown = false;
|
|
|
|
try {
|
|
|
|
$this->repair->run();
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2016-04-27 11:51:02 +03:00
|
|
|
$thrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($thrown);
|
|
|
|
// jump out after exception
|
|
|
|
$this->assertEquals(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2016-04-27 11:51:02 +03:00
|
|
|
'step: Exception Test',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2016-04-27 11:51:02 +03:00
|
|
|
$this->outputArray
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRunRepairStepsContinueAfterWarning() {
|
|
|
|
$this->repair->addStep(new TestRepairStep(true));
|
|
|
|
$this->repair->addStep(new TestRepairStep(false));
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2016-04-27 11:51:02 +03:00
|
|
|
'step: Test Name',
|
|
|
|
'warning: Simulated warning',
|
|
|
|
'step: Test Name',
|
|
|
|
'info: Simulated info',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2016-04-27 11:51:02 +03:00
|
|
|
$this->outputArray
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|