BackgroundRepairTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @author Thomas Müller <thomas.mueller@tmit.eu>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Migration;
  22. use OC\Migration\BackgroundRepair;
  23. use OC\NeedsUpdateException;
  24. use OCP\ILogger;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. use Symfony\Component\EventDispatcher\EventDispatcher;
  28. use Symfony\Component\EventDispatcher\GenericEvent;
  29. use Test\TestCase;
  30. class TestRepairStep implements IRepairStep {
  31. /**
  32. * Returns the step's name
  33. *
  34. * @return string
  35. * @since 9.1.0
  36. */
  37. public function getName() {
  38. return 'A test repair step';
  39. }
  40. /**
  41. * Run repair step.
  42. * Must throw exception on error.
  43. *
  44. * @since 9.1.0
  45. * @throws \Exception in case of failure
  46. */
  47. public function run(IOutput $output) {
  48. // TODO: Implement run() method.
  49. }
  50. }
  51. class BackgroundRepairTest extends TestCase {
  52. /** @var \OC\BackgroundJob\JobList | \PHPUnit_Framework_MockObject_MockObject */
  53. private $jobList;
  54. /** @var BackgroundRepair | \PHPUnit_Framework_MockObject_MockObject */
  55. private $job;
  56. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
  57. private $logger;
  58. public function setUp() {
  59. parent::setUp();
  60. $this->jobList = $this->getMockBuilder('OC\BackgroundJob\JobList')
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->logger = $this->getMockBuilder(ILogger::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->job = $this->getMockBuilder(BackgroundRepair::class)
  67. ->setMethods(['loadApp'])
  68. ->getMock();
  69. }
  70. public function testNoArguments() {
  71. $this->jobList->expects($this->once())->method('remove');
  72. $this->job->execute($this->jobList);
  73. }
  74. public function testAppUpgrading() {
  75. $this->jobList->expects($this->never())->method('remove');
  76. $this->job->expects($this->once())->method('loadApp')->with('test')->willThrowException(new NeedsUpdateException());
  77. $this->job->setArgument([
  78. 'app' => 'test',
  79. 'step' => 'j'
  80. ]);
  81. $this->job->execute($this->jobList);
  82. }
  83. public function testUnknownStep() {
  84. $this->jobList->expects($this->once())->method('remove');
  85. $this->logger->expects($this->once())->method('logException');
  86. $this->job->setArgument([
  87. 'app' => 'test',
  88. 'step' => 'j'
  89. ]);
  90. $this->job->execute($this->jobList, $this->logger);
  91. }
  92. public function testWorkingStep() {
  93. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject $dispatcher */
  94. $dispatcher = $this->createMock(EventDispatcher::class);
  95. $dispatcher->expects($this->once())->method('dispatch')
  96. ->with('\OC\Repair::step', new GenericEvent('\OC\Repair::step', ['A test repair step']));
  97. $this->jobList->expects($this->once())->method('remove');
  98. $this->job->setDispatcher($dispatcher);
  99. $this->job->setArgument([
  100. 'app' => 'test',
  101. 'step' => '\Test\Migration\TestRepairStep'
  102. ]);
  103. $this->job->execute($this->jobList, $this->logger);
  104. }
  105. }