better tests for SimpleFolder
test behavior, not implementation Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
7b7d69d5da
commit
fed86e8382
|
@ -24,116 +24,93 @@
|
||||||
namespace Test\File\SimpleFS;
|
namespace Test\File\SimpleFS;
|
||||||
|
|
||||||
use OC\Files\SimpleFS\SimpleFolder;
|
use OC\Files\SimpleFS\SimpleFolder;
|
||||||
|
use OC\Files\Storage\Temporary;
|
||||||
|
use OC\Files\View;
|
||||||
use OCP\Files\File;
|
use OCP\Files\File;
|
||||||
use OCP\Files\Folder;
|
use OCP\Files\Folder;
|
||||||
use OCP\Files\Node;
|
use OCP\Files\Node;
|
||||||
use OCP\Files\NotFoundException;
|
use OCP\Files\NotFoundException;
|
||||||
use OCP\Files\SimpleFS\ISimpleFile;
|
use OCP\Files\SimpleFS\ISimpleFile;
|
||||||
|
use Test\Traits\MountProviderTrait;
|
||||||
|
use Test\Traits\UserTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DB
|
||||||
|
*/
|
||||||
class SimpleFolderTest extends \Test\TestCase {
|
class SimpleFolderTest extends \Test\TestCase {
|
||||||
/** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
|
use MountProviderTrait;
|
||||||
|
use UserTrait;
|
||||||
|
|
||||||
|
/** @var Folder */
|
||||||
private $folder;
|
private $folder;
|
||||||
|
|
||||||
|
/** @var Folder */
|
||||||
|
private $parentFolder;
|
||||||
|
|
||||||
/** @var SimpleFolder */
|
/** @var SimpleFolder */
|
||||||
private $simpleFolder;
|
private $simpleFolder;
|
||||||
|
|
||||||
|
private $storage;
|
||||||
|
|
||||||
protected function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->folder = $this->createMock(Folder::class);
|
$this->storage = new Temporary([]);
|
||||||
|
$this->createUser('simple', 'simple');
|
||||||
|
$this->registerMount('simple', $this->storage, '/simple/files');
|
||||||
|
$this->loginAsUser('simple');
|
||||||
|
|
||||||
|
$this->parentFolder = \OC::$server->getUserFolder('simple');
|
||||||
|
|
||||||
|
$this->folder = $this->parentFolder->newFolder('test');
|
||||||
$this->simpleFolder = new SimpleFolder($this->folder);
|
$this->simpleFolder = new SimpleFolder($this->folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetName() {
|
public function testGetName() {
|
||||||
$this->folder->expects($this->once())
|
$this->assertEquals('test', $this->simpleFolder->getName());
|
||||||
->method('getName')
|
|
||||||
->willReturn('myname');
|
|
||||||
|
|
||||||
$this->assertEquals('myname', $this->simpleFolder->getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDelete() {
|
public function testDelete() {
|
||||||
$this->folder->expects($this->once())
|
$this->assertTrue($this->parentFolder->nodeExists('test'));
|
||||||
->method('delete');
|
|
||||||
|
|
||||||
$this->simpleFolder->delete();
|
$this->simpleFolder->delete();
|
||||||
|
$this->assertFalse($this->parentFolder->nodeExists('test'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataFileExists() {
|
public function testFileExists() {
|
||||||
return [
|
$this->folder->newFile('exists');
|
||||||
[true],
|
|
||||||
[false],
|
$this->assertFalse($this->simpleFolder->fileExists('not-exists'));
|
||||||
];
|
$this->assertTrue($this->simpleFolder->fileExists('exists'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testGetFile() {
|
||||||
* @dataProvider dataFileExists
|
$this->folder->newFile('exists');
|
||||||
* @param bool $exists
|
|
||||||
*/
|
|
||||||
public function testFileExists($exists) {
|
|
||||||
$this->folder->expects($this->once())
|
|
||||||
->method('nodeExists')
|
|
||||||
->with($this->equalTo('file'))
|
|
||||||
->willReturn($exists);
|
|
||||||
|
|
||||||
$this->assertEquals($exists, $this->simpleFolder->fileExists('file'));
|
$result = $this->simpleFolder->getFile('exists');
|
||||||
}
|
|
||||||
|
|
||||||
public function dataGetFile() {
|
|
||||||
return [
|
|
||||||
[File::class, false],
|
|
||||||
[Folder::class, true],
|
|
||||||
[Node::class, true],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider dataGetFile
|
|
||||||
* @param string $class
|
|
||||||
* @param bool $exception
|
|
||||||
*/
|
|
||||||
public function testGetFile($class, $exception) {
|
|
||||||
$node = $this->createMock($class);
|
|
||||||
|
|
||||||
$this->folder->expects($this->once())
|
|
||||||
->method('get')
|
|
||||||
->with($this->equalTo('file'))
|
|
||||||
->willReturn($node);
|
|
||||||
|
|
||||||
try {
|
|
||||||
$result = $this->simpleFolder->getFile('file');
|
|
||||||
$this->assertFalse($exception);
|
|
||||||
$this->assertInstanceOf(ISimpleFile::class, $result);
|
$this->assertInstanceOf(ISimpleFile::class, $result);
|
||||||
} catch (NotFoundException $e) {
|
|
||||||
$this->assertTrue($exception);
|
$this->expectException(NotFoundException::class);
|
||||||
}
|
$this->simpleFolder->getFile('not-exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNewFile() {
|
public function testNewFile() {
|
||||||
$file = $this->createMock(File::class);
|
|
||||||
|
|
||||||
$this->folder->expects($this->once())
|
|
||||||
->method('newFile')
|
|
||||||
->with($this->equalTo('file'))
|
|
||||||
->willReturn($file);
|
|
||||||
|
|
||||||
$result = $this->simpleFolder->newFile('file');
|
$result = $this->simpleFolder->newFile('file');
|
||||||
$this->assertInstanceOf(ISimpleFile::class, $result);
|
$this->assertInstanceOf(ISimpleFile::class, $result);
|
||||||
|
$this->assertFalse($this->folder->nodeExists('file'));
|
||||||
|
$result->putContent('bar');
|
||||||
|
|
||||||
|
$this->assertTrue($this->folder->nodeExists('file'));
|
||||||
|
$this->assertEquals('bar', $result->getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetDirectoryListing() {
|
public function testGetDirectoryListing() {
|
||||||
$file = $this->createMock(File::class);
|
$this->folder->newFile('file1');
|
||||||
$folder = $this->createMock(Folder::class);
|
$this->folder->newFile('file2');
|
||||||
$node = $this->createMock(Node::class);
|
|
||||||
|
|
||||||
$this->folder->expects($this->once())
|
|
||||||
->method('getDirectoryListing')
|
|
||||||
->willReturn([$file, $folder, $node]);
|
|
||||||
|
|
||||||
$result = $this->simpleFolder->getDirectoryListing();
|
$result = $this->simpleFolder->getDirectoryListing();
|
||||||
|
$this->assertCount(2, $result);
|
||||||
$this->assertCount(1, $result);
|
|
||||||
$this->assertInstanceOf(ISimpleFile::class, $result[0]);
|
$this->assertInstanceOf(ISimpleFile::class, $result[0]);
|
||||||
|
$this->assertInstanceOf(ISimpleFile::class, $result[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue