summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-06-16 11:13:10 +0200
committerVincent Petry <pvince81@owncloud.com>2014-06-16 11:13:10 +0200
commita48bcceb237295386e9e598caf25161d6d305453 (patch)
tree9ecbf7f212a86e255149d37f90b9bc17291a6761 /tests
parentd21845557e9fdbc27817f6ed2d8249d17c14a978 (diff)
parent05e351416e1c45b88c7e51d39a4e9961e5b8546f (diff)
downloadnextcloud-server-a48bcceb237295386e9e598caf25161d6d305453.tar.gz
nextcloud-server-a48bcceb237295386e9e598caf25161d6d305453.zip
Merge pull request #8917 from owncloud/repair-routine-base
Add support for repair step classes
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/repair.php159
1 files changed, 159 insertions, 0 deletions
diff --git a/tests/lib/repair.php b/tests/lib/repair.php
new file mode 100644
index 00000000000..121f41dedd9
--- /dev/null
+++ b/tests/lib/repair.php
@@ -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
+ );
+ }
+}