repair.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. use OC\Hooks\BasicEmitter;
  9. class TestRepairStep extends BasicEmitter implements \OC\RepairStep{
  10. private $warning;
  11. public function __construct($warning = false) {
  12. $this->warning = $warning;
  13. }
  14. public function getName() {
  15. return 'Test Name';
  16. }
  17. public function run() {
  18. if ($this->warning) {
  19. $this->emit('\OC\Repair', 'warning', array('Simulated warning'));
  20. }
  21. else {
  22. $this->emit('\OC\Repair', 'info', array('Simulated info'));
  23. }
  24. }
  25. }
  26. class Test_Repair extends \Test\TestCase {
  27. public function testRunRepairStep() {
  28. $output = array();
  29. $repair = new \OC\Repair();
  30. $repair->addStep(new TestRepairStep(false));
  31. $repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
  32. $output[] = 'warning: ' . $description;
  33. });
  34. $repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
  35. $output[] = 'info: ' . $description;
  36. });
  37. $repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
  38. $output[] = 'step: ' . $description;
  39. });
  40. $repair->run();
  41. $this->assertEquals(
  42. array(
  43. 'step: Test Name',
  44. 'info: Simulated info',
  45. ),
  46. $output
  47. );
  48. }
  49. public function testRunRepairStepThatFail() {
  50. $output = array();
  51. $repair = new \OC\Repair();
  52. $repair->addStep(new TestRepairStep(true));
  53. $repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
  54. $output[] = 'warning: ' . $description;
  55. });
  56. $repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
  57. $output[] = 'info: ' . $description;
  58. });
  59. $repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
  60. $output[] = 'step: ' . $description;
  61. });
  62. $repair->run();
  63. $this->assertEquals(
  64. array(
  65. 'step: Test Name',
  66. 'warning: Simulated warning',
  67. ),
  68. $output
  69. );
  70. }
  71. public function testRunRepairStepsWithException() {
  72. $output = array();
  73. $mock = $this->getMock('TestRepairStep');
  74. $mock->expects($this->any())
  75. ->method('run')
  76. ->will($this->throwException(new Exception));
  77. $mock->expects($this->any())
  78. ->method('getName')
  79. ->will($this->returnValue('Exception Test'));
  80. $repair = new \OC\Repair();
  81. $repair->addStep($mock);
  82. $repair->addStep(new TestRepairStep(false));
  83. $repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
  84. $output[] = 'warning: ' . $description;
  85. });
  86. $repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
  87. $output[] = 'info: ' . $description;
  88. });
  89. $repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
  90. $output[] = 'step: ' . $description;
  91. });
  92. $thrown = false;
  93. try {
  94. $repair->run();
  95. }
  96. catch (Exception $e) {
  97. $thrown = true;
  98. }
  99. $this->assertTrue($thrown);
  100. // jump out after exception
  101. $this->assertEquals(
  102. array(
  103. 'step: Exception Test',
  104. ),
  105. $output
  106. );
  107. }
  108. public function testRunRepairStepsContinueAfterWarning() {
  109. $output = array();
  110. $repair = new \OC\Repair();
  111. $repair->addStep(new TestRepairStep(true));
  112. $repair->addStep(new TestRepairStep(false));
  113. $repair->listen('\OC\Repair', 'warning', function ($description) use (&$output) {
  114. $output[] = 'warning: ' . $description;
  115. });
  116. $repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
  117. $output[] = 'info: ' . $description;
  118. });
  119. $repair->listen('\OC\Repair', 'step', function ($description) use (&$output) {
  120. $output[] = 'step: ' . $description;
  121. });
  122. $repair->run();
  123. $this->assertEquals(
  124. array(
  125. 'step: Test Name',
  126. 'warning: Simulated warning',
  127. 'step: Test Name',
  128. 'info: Simulated info',
  129. ),
  130. $output
  131. );
  132. }
  133. }