RepairStepTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace Test;
  9. use OCP\Migration\IRepairStep;
  10. use Symfony\Component\EventDispatcher\EventDispatcher;
  11. class RepairStepTest implements IRepairStep {
  12. private $warning;
  13. public function __construct($warning = false) {
  14. $this->warning = $warning;
  15. }
  16. public function getName() {
  17. return 'Test Name';
  18. }
  19. public function run(\OCP\Migration\IOutput $out) {
  20. if ($this->warning) {
  21. $out->warning('Simulated warning');
  22. }
  23. else {
  24. $out->info('Simulated info');
  25. }
  26. }
  27. }
  28. class RepairTest extends TestCase {
  29. /** @var \OC\Repair */
  30. private $repair;
  31. /** @var string[] */
  32. private $outputArray;
  33. public function setUp() {
  34. parent::setUp();
  35. $dispatcher = new EventDispatcher();
  36. $this->repair = new \OC\Repair([], $dispatcher);
  37. $dispatcher->addListener('\OC\Repair::warning', function ($event) {
  38. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  39. $this->outputArray[] = 'warning: ' . $event->getArgument(0);
  40. });
  41. $dispatcher->addListener('\OC\Repair::info', function ($event) {
  42. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  43. $this->outputArray[] = 'info: ' . $event->getArgument(0);
  44. });
  45. $dispatcher->addListener('\OC\Repair::step', function ($event) {
  46. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  47. $this->outputArray[] = 'step: ' . $event->getArgument(0);
  48. });
  49. }
  50. public function testRunRepairStep() {
  51. $this->repair->addStep(new TestRepairStep(false));
  52. $this->repair->run();
  53. $this->assertEquals(
  54. array(
  55. 'step: Test Name',
  56. 'info: Simulated info',
  57. ),
  58. $this->outputArray
  59. );
  60. }
  61. public function testRunRepairStepThatFail() {
  62. $this->repair->addStep(new TestRepairStep(true));
  63. $this->repair->run();
  64. $this->assertEquals(
  65. array(
  66. 'step: Test Name',
  67. 'warning: Simulated warning',
  68. ),
  69. $this->outputArray
  70. );
  71. }
  72. public function testRunRepairStepsWithException() {
  73. $mock = $this->createMock(TestRepairStep::class);
  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. $this->repair->addStep($mock);
  81. $this->repair->addStep(new TestRepairStep(false));
  82. $thrown = false;
  83. try {
  84. $this->repair->run();
  85. }
  86. catch (\Exception $e) {
  87. $thrown = true;
  88. }
  89. $this->assertTrue($thrown);
  90. // jump out after exception
  91. $this->assertEquals(
  92. array(
  93. 'step: Exception Test',
  94. ),
  95. $this->outputArray
  96. );
  97. }
  98. public function testRunRepairStepsContinueAfterWarning() {
  99. $this->repair->addStep(new TestRepairStep(true));
  100. $this->repair->addStep(new TestRepairStep(false));
  101. $this->repair->run();
  102. $this->assertEquals(
  103. array(
  104. 'step: Test Name',
  105. 'warning: Simulated warning',
  106. 'step: Test Name',
  107. 'info: Simulated info',
  108. ),
  109. $this->outputArray
  110. );
  111. }
  112. }