Merge pull request #8917 from owncloud/repair-routine-base
Add support for repair step classesremotes/origin/ldap_group_count
commit
a48bcceb23
@ -0,0 +1,44 @@ |
||||
<?php |
||||
/** |
||||
* ownCloud |
||||
* |
||||
* @author Vincent Petry |
||||
* @copyright 2014 Vincent Petry pvince81@owncloud.com |
||||
* |
||||
* This library is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
||||
* License as published by the Free Software Foundation; either |
||||
* version 3 of the License, or any later version. |
||||
* |
||||
* This library is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public |
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
namespace OC; |
||||
|
||||
/** |
||||
* Repair step |
||||
*/ |
||||
interface RepairStep { |
||||
|
||||
/** |
||||
* Returns the step's name |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function getName(); |
||||
|
||||
/** |
||||
* Run repair step. |
||||
* Must throw exception on error. |
||||
* |
||||
* @throws \Exception in case of failure |
||||
*/ |
||||
public function run(); |
||||
|
||||
} |
||||
@ -0,0 +1,159 @@ |
||||
<?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. |
||||
*/ |
||||
|
||||
use OC\Hooks\BasicEmitter; |
||||
|
||||
class TestRepairStep extends BasicEmitter implements \OC\RepairStep{ |
||||
private $warning; |
||||
|
||||
public function __construct($warning = false) { |
||||
$this->warning = $warning; |
||||
} |
||||
|
||||
public function getName() { |
||||
return 'Test Name'; |
||||
} |
||||
|
||||
public function run() { |
||||
if ($this->warning) { |
||||
$this->emit('\OC\Repair', 'warning', array('Simulated warning')); |
||||
} |
||||
else { |
||||
$this->emit('\OC\Repair', 'info', array('Simulated info')); |
||||
} |
||||
} |
||||
} |
||||
|
||||
class Test_Repair extends PHPUnit_Framework_TestCase { |
||||
public function testRunRepairStep() { |
||||
$output = array(); |
||||
|
||||
$repair = new \OC\Repair(); |
||||
$repair->addStep(new TestRepairStep(false)); |
||||
|
||||
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) { |
||||
$output[] = 'warning: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) { |
||||
$output[] = 'info: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) { |
||||
$output[] = 'step: ' . $description; |
||||
}); |
||||
|
||||
$repair->run(); |
||||
|
||||
$this->assertEquals( |
||||
array( |
||||
'step: Test Name', |
||||
'info: Simulated info', |
||||
), |
||||
$output |
||||
); |
||||
} |
||||
|
||||
public function testRunRepairStepThatFail() { |
||||
$output = array(); |
||||
|
||||
$repair = new \OC\Repair(); |
||||
$repair->addStep(new TestRepairStep(true)); |
||||
|
||||
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) { |
||||
$output[] = 'warning: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) { |
||||
$output[] = 'info: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) { |
||||
$output[] = 'step: ' . $description; |
||||
}); |
||||
|
||||
$repair->run(); |
||||
|
||||
$this->assertEquals( |
||||
array( |
||||
'step: Test Name', |
||||
'warning: Simulated warning', |
||||
), |
||||
$output |
||||
); |
||||
} |
||||
|
||||
public function testRunRepairStepsWithException() { |
||||
$output = array(); |
||||
|
||||
$mock = $this->getMock('TestRepairStep'); |
||||
$mock->expects($this->any()) |
||||
->method('run') |
||||
->will($this->throwException(new Exception)); |
||||
$mock->expects($this->any()) |
||||
->method('getName') |
||||
->will($this->returnValue('Exception Test')); |
||||
|
||||
$repair = new \OC\Repair(); |
||||
$repair->addStep($mock); |
||||
$repair->addStep(new TestRepairStep(false)); |
||||
|
||||
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) { |
||||
$output[] = 'warning: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) { |
||||
$output[] = 'info: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) { |
||||
$output[] = 'step: ' . $description; |
||||
}); |
||||
|
||||
$thrown = false; |
||||
try { |
||||
$repair->run(); |
||||
} |
||||
catch (Exception $e) { |
||||
$thrown = true; |
||||
} |
||||
|
||||
$this->assertTrue($thrown); |
||||
// jump out after exception |
||||
$this->assertEquals( |
||||
array( |
||||
'step: Exception Test', |
||||
), |
||||
$output |
||||
); |
||||
} |
||||
|
||||
public function testRunRepairStepsContinueAfterWarning() { |
||||
$output = array(); |
||||
|
||||
$repair = new \OC\Repair(); |
||||
$repair->addStep(new TestRepairStep(true)); |
||||
$repair->addStep(new TestRepairStep(false)); |
||||
|
||||
$repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) { |
||||
$output[] = 'warning: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'info', function ($description) use (&$output) { |
||||
$output[] = 'info: ' . $description; |
||||
}); |
||||
$repair->listen('\OC\Repair', 'step', function ($description) use (&$output) { |
||||
$output[] = 'step: ' . $description; |
||||
}); |
||||
|
||||
$repair->run(); |
||||
|
||||
$this->assertEquals( |
||||
array( |
||||
'step: Test Name', |
||||
'warning: Simulated warning', |
||||
'step: Test Name', |
||||
'info: Simulated info', |
||||
), |
||||
$output |
||||
); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue