Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
adjust tests
Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz authored and Backportbot committed Sep 9, 2019
commit 478241ceb0e04d534ebf68745adeb008ea7100f8
94 changes: 53 additions & 41 deletions tests/lib/Files/Storage/Wrapper/AvailabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,46 @@

namespace Test\Files\Storage\Wrapper;

use OC\Files\Cache\Storage as StorageCache;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Availability;
use OCP\Files\StorageNotAvailableException;

class AvailabilityTest extends \Test\TestCase {
protected function getWrapperInstance() {
$storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
->disableOriginalConstructor()
->getMock();
$wrapper = new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]);
return [$storage, $wrapper];

/** @var \PHPUnit\Framework\MockObject\MockObject|StorageCache */
protected $storageCache;
/** @var \PHPUnit\Framework\MockObject\MockObject|Temporary */
protected $storage;
/** @var Availability */
protected $wrapper;

public function setUp() {
parent::setUp();

$this->storageCache = $this->createMock(StorageCache::class);

$this->storage = $this->createMock(Temporary::class);
$this->storage->expects($this->any())
->method('getStorageCache')
->willReturn($this->storageCache);

$this->wrapper = new Availability(['storage' => $this->storage]);
}

/**
* Storage is available
*/
public function testAvailable() {
list($storage, $wrapper) = $this->getWrapperInstance();
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('getAvailability')
->willReturn(['available' => true, 'last_checked' => 0]);
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('test');
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('mkdir');

$wrapper->mkdir('foobar');
$this->wrapper->mkdir('foobar');
}

/**
Expand All @@ -52,39 +69,37 @@ public function testAvailable() {
* @expectedException \OCP\Files\StorageNotAvailableException
*/
public function testUnavailable() {
list($storage, $wrapper) = $this->getWrapperInstance();
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('getAvailability')
->willReturn(['available' => false, 'last_checked' => time()]);
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('test');
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('mkdir');

$wrapper->mkdir('foobar');
$this->wrapper->mkdir('foobar');
}

/**
* Storage marked unavailable, TTL expired
*/
public function testUnavailableRecheck() {
list($storage, $wrapper) = $this->getWrapperInstance();
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('getAvailability')
->willReturn(['available' => false, 'last_checked' => 0]);
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('test')
->willReturn(true);
$storage->expects($this->exactly(2))
$this->storage->expects($this->exactly(2))
->method('setAvailability')
->withConsecutive(
[$this->equalTo(false)], // prevents concurrent rechecks
[$this->equalTo(true)] // sets correct availability
);
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('mkdir');

$wrapper->mkdir('foobar');
$this->wrapper->mkdir('foobar');
}

/**
Expand All @@ -93,40 +108,38 @@ public function testUnavailableRecheck() {
* @expectedException \OCP\Files\StorageNotAvailableException
*/
public function testAvailableThrowStorageNotAvailable() {
list($storage, $wrapper) = $this->getWrapperInstance();
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('getAvailability')
->willReturn(['available' => true, 'last_checked' => 0]);
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('test');
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('mkdir')
->will($this->throwException(new \OCP\Files\StorageNotAvailableException()));
$storage->expects($this->once())
->will($this->throwException(new StorageNotAvailableException()));
$this->storageCache->expects($this->once())
->method('setAvailability')
->with($this->equalTo(false));

$wrapper->mkdir('foobar');
$this->wrapper->mkdir('foobar');
}

/**
* Storage available, but call fails
* Method failure does not indicate storage unavailability
*/
public function testAvailableFailure() {
list($storage, $wrapper) = $this->getWrapperInstance();
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('getAvailability')
->willReturn(['available' => true, 'last_checked' => 0]);
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('test');
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('mkdir')
->willReturn(false);
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('setAvailability');

$wrapper->mkdir('foobar');
$this->wrapper->mkdir('foobar');
}

/**
Expand All @@ -136,18 +149,17 @@ public function testAvailableFailure() {
* @expectedException \Exception
*/
public function testAvailableThrow() {
list($storage, $wrapper) = $this->getWrapperInstance();
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('getAvailability')
->willReturn(['available' => true, 'last_checked' => 0]);
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('test');
$storage->expects($this->once())
$this->storage->expects($this->once())
->method('mkdir')
->will($this->throwException(new \Exception()));
$storage->expects($this->never())
$this->storage->expects($this->never())
->method('setAvailability');

$wrapper->mkdir('foobar');
$this->wrapper->mkdir('foobar');
}
}