From 5d313da7098d82cdf6cc4a5235bf1455b6833435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 Aug 2022 11:01:01 +0200 Subject: Fix RepairTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- tests/lib/RepairStepTest.php | 131 ---------------------------------------- tests/lib/RepairTest.php | 138 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 131 deletions(-) delete mode 100644 tests/lib/RepairStepTest.php create mode 100644 tests/lib/RepairTest.php (limited to 'tests') diff --git a/tests/lib/RepairStepTest.php b/tests/lib/RepairStepTest.php deleted file mode 100644 index b1d16fdd3fd..00000000000 --- a/tests/lib/RepairStepTest.php +++ /dev/null @@ -1,131 +0,0 @@ - - * 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; -use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcher; - -class RepairStepTest implements IRepairStep { - 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'); - } else { - $out->info('Simulated info'); - } - } -} - -class RepairTest extends TestCase { - /** @var \OC\Repair */ - private $repair; - - /** @var string[] */ - private $outputArray; - - protected function setUp(): void { - parent::setUp(); - $dispatcher = new EventDispatcher(); - $this->repair = new \OC\Repair([], $dispatcher, $this->createMock(LoggerInterface::class)); - - $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( - [ - 'step: Test Name', - 'info: Simulated info', - ], - $this->outputArray - ); - } - - public function testRunRepairStepThatFail() { - $this->repair->addStep(new TestRepairStep(true)); - $this->repair->run(); - - $this->assertEquals( - [ - 'step: Test Name', - 'warning: Simulated warning', - ], - $this->outputArray - ); - } - - public function testRunRepairStepsWithException() { - $mock = $this->createMock(TestRepairStep::class); - $mock->expects($this->any()) - ->method('run') - ->will($this->throwException(new \Exception())); - $mock->expects($this->any()) - ->method('getName') - ->willReturn('Exception Test'); - - $this->repair->addStep($mock); - $this->repair->addStep(new TestRepairStep(false)); - - $thrown = false; - try { - $this->repair->run(); - } catch (\Exception $e) { - $thrown = true; - } - - $this->assertTrue($thrown); - // jump out after exception - $this->assertEquals( - [ - 'step: Exception Test', - ], - $this->outputArray - ); - } - - public function testRunRepairStepsContinueAfterWarning() { - $this->repair->addStep(new TestRepairStep(true)); - $this->repair->addStep(new TestRepairStep(false)); - $this->repair->run(); - - $this->assertEquals( - [ - 'step: Test Name', - 'warning: Simulated warning', - 'step: Test Name', - 'info: Simulated info', - ], - $this->outputArray - ); - } -} diff --git a/tests/lib/RepairTest.php b/tests/lib/RepairTest.php new file mode 100644 index 00000000000..1a2fd620e49 --- /dev/null +++ b/tests/lib/RepairTest.php @@ -0,0 +1,138 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test; + +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Migration\IRepairStep; +use OC\Repair; +use OC\Repair\Events\RepairInfoEvent; +use OC\Repair\Events\RepairStepEvent; +use OC\Repair\Events\RepairWarningEvent; +use OC\Repair\Events\RepairErrorEvent; +use Psr\Log\LoggerInterface; + +class TestRepairStep implements IRepairStep { + private bool $warning; + + public function __construct(bool $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'); + } else { + $out->info('Simulated info'); + } + } +} + +class RepairTest extends TestCase { + private Repair $repair; + + /** @var string[] */ + private array $outputArray = []; + + protected function setUp(): void { + parent::setUp(); + $dispatcher = \OC::$server->get(IEventDispatcher::class); + $this->repair = new \OC\Repair([], $dispatcher, $this->createMock(LoggerInterface::class)); + + $dispatcher->addListener(RepairWarningEvent::class, function (RepairWarningEvent $event) { + $this->outputArray[] = 'warning: ' . $event->getMessage(); + }); + $dispatcher->addListener(RepairInfoEvent::class, function (RepairInfoEvent $event) { + $this->outputArray[] = 'info: ' . $event->getMessage(); + }); + $dispatcher->addListener(RepairStepEvent::class, function (RepairStepEvent $event) { + $this->outputArray[] = 'step: ' . $event->getStepName(); + }); + $dispatcher->addListener(RepairErrorEvent::class, function (RepairErrorEvent $event) { + $this->outputArray[] = 'error: ' . $event->getMessage(); + }); + } + + public function testRunRepairStep() { + $this->repair->addStep(new TestRepairStep(false)); + $this->repair->run(); + + $this->assertEquals( + [ + 'step: Test Name', + 'info: Simulated info', + ], + $this->outputArray + ); + } + + public function testRunRepairStepThatFail() { + $this->repair->addStep(new TestRepairStep(true)); + $this->repair->run(); + + $this->assertEquals( + [ + 'step: Test Name', + 'warning: Simulated warning', + ], + $this->outputArray + ); + } + + public function testRunRepairStepsWithException() { + $mock = $this->createMock(TestRepairStep::class); + $mock->expects($this->any()) + ->method('run') + ->will($this->throwException(new \Exception('Exception text'))); + $mock->expects($this->any()) + ->method('getName') + ->willReturn('Exception Test'); + + $this->repair->addStep($mock); + $this->repair->addStep(new TestRepairStep(false)); + + $thrown = false; + try { + $this->repair->run(); + } catch (\Exception $e) { + $thrown = true; + } + + $this->assertFalse($thrown); + // jump out after exception + $this->assertEquals( + [ + 'step: Exception Test', + 'error: Exception text', + 'step: Test Name', + 'info: Simulated info', + ], + $this->outputArray + ); + } + + public function testRunRepairStepsContinueAfterWarning() { + $this->repair->addStep(new TestRepairStep(true)); + $this->repair->addStep(new TestRepairStep(false)); + $this->repair->run(); + + $this->assertEquals( + [ + 'step: Test Name', + 'warning: Simulated warning', + 'step: Test Name', + 'info: Simulated info', + ], + $this->outputArray + ); + } +} -- cgit v1.2.3