AvailabilityTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  4. *
  5. * @copyright Copyright (c) 2015, 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\Files\Storage\Wrapper;
  22. class AvailabilityTest extends \Test\TestCase {
  23. protected function getWrapperInstance() {
  24. $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $wrapper = new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]);
  28. return [$storage, $wrapper];
  29. }
  30. /**
  31. * Storage is available
  32. */
  33. public function testAvailable() {
  34. list($storage, $wrapper) = $this->getWrapperInstance();
  35. $storage->expects($this->once())
  36. ->method('getAvailability')
  37. ->willReturn(['available' => true, 'last_checked' => 0]);
  38. $storage->expects($this->never())
  39. ->method('test');
  40. $storage->expects($this->once())
  41. ->method('mkdir');
  42. $wrapper->mkdir('foobar');
  43. }
  44. /**
  45. * Storage marked unavailable, TTL not expired
  46. *
  47. * @expectedException \OCP\Files\StorageNotAvailableException
  48. */
  49. public function testUnavailable() {
  50. list($storage, $wrapper) = $this->getWrapperInstance();
  51. $storage->expects($this->once())
  52. ->method('getAvailability')
  53. ->willReturn(['available' => false, 'last_checked' => time()]);
  54. $storage->expects($this->never())
  55. ->method('test');
  56. $storage->expects($this->never())
  57. ->method('mkdir');
  58. $wrapper->mkdir('foobar');
  59. }
  60. /**
  61. * Storage marked unavailable, TTL expired
  62. */
  63. public function testUnavailableRecheck() {
  64. list($storage, $wrapper) = $this->getWrapperInstance();
  65. $storage->expects($this->once())
  66. ->method('getAvailability')
  67. ->willReturn(['available' => false, 'last_checked' => 0]);
  68. $storage->expects($this->once())
  69. ->method('test')
  70. ->willReturn(true);
  71. $storage->expects($this->exactly(2))
  72. ->method('setAvailability')
  73. ->withConsecutive(
  74. [$this->equalTo(false)], // prevents concurrent rechecks
  75. [$this->equalTo(true)] // sets correct availability
  76. );
  77. $storage->expects($this->once())
  78. ->method('mkdir');
  79. $wrapper->mkdir('foobar');
  80. }
  81. /**
  82. * Storage marked available, but throws StorageNotAvailableException
  83. *
  84. * @expectedException \OCP\Files\StorageNotAvailableException
  85. */
  86. public function testAvailableThrowStorageNotAvailable() {
  87. list($storage, $wrapper) = $this->getWrapperInstance();
  88. $storage->expects($this->once())
  89. ->method('getAvailability')
  90. ->willReturn(['available' => true, 'last_checked' => 0]);
  91. $storage->expects($this->never())
  92. ->method('test');
  93. $storage->expects($this->once())
  94. ->method('mkdir')
  95. ->will($this->throwException(new \OCP\Files\StorageNotAvailableException()));
  96. $storage->expects($this->once())
  97. ->method('setAvailability')
  98. ->with($this->equalTo(false));
  99. $wrapper->mkdir('foobar');
  100. }
  101. /**
  102. * Storage available, but call fails
  103. * Method failure does not indicate storage unavailability
  104. */
  105. public function testAvailableFailure() {
  106. list($storage, $wrapper) = $this->getWrapperInstance();
  107. $storage->expects($this->once())
  108. ->method('getAvailability')
  109. ->willReturn(['available' => true, 'last_checked' => 0]);
  110. $storage->expects($this->never())
  111. ->method('test');
  112. $storage->expects($this->once())
  113. ->method('mkdir')
  114. ->willReturn(false);
  115. $storage->expects($this->never())
  116. ->method('setAvailability');
  117. $wrapper->mkdir('foobar');
  118. }
  119. /**
  120. * Storage available, but throws exception
  121. * Standard exception does not indicate storage unavailability
  122. *
  123. * @expectedException \Exception
  124. */
  125. public function testAvailableThrow() {
  126. list($storage, $wrapper) = $this->getWrapperInstance();
  127. $storage->expects($this->once())
  128. ->method('getAvailability')
  129. ->willReturn(['available' => true, 'last_checked' => 0]);
  130. $storage->expects($this->never())
  131. ->method('test');
  132. $storage->expects($this->once())
  133. ->method('mkdir')
  134. ->will($this->throwException(new \Exception()));
  135. $storage->expects($this->never())
  136. ->method('setAvailability');
  137. $wrapper->mkdir('foobar');
  138. }
  139. }