Improve NodeTest

* Do not use DB
* Fix phpunit-5.4 warnigns
* Moved commong stuff to setup
This commit is contained in:
Roeland Jago Douma 2016-07-11 15:41:02 +02:00
parent d15fd5cb96
commit 3b4535c3be
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
1 changed files with 76 additions and 123 deletions

View File

@ -11,15 +11,45 @@ namespace Test\Files\Node;
use OC\Files\FileInfo;
class NodeTest extends \Test\TestCase {
/** @var \OC\User\User */
private $user;
/** @var \OC\Files\Mount\Manager */
private $manager;
/** @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject */
private $view;
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject */
private $root;
protected function setUp() {
parent::setUp();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy);
$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
->disableOriginalConstructor()
->getMock();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config, $urlGenerator);
$this->manager = $this->getMockBuilder('\OC\Files\Mount\Manager')
->disableOriginalConstructor()
->getMock();
$this->view = $this->getMockBuilder('\OC\Files\View')
->disableOriginalConstructor()
->getMock();
$this->root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
}
protected function getMockStorage() {
$storage = $this->getMock('\OCP\Files\Storage');
$storage = $this->getMockBuilder('\OCP\Files\Storage')
->disableOriginalConstructor()
->getMock();
$storage->expects($this->any())
->method('getId')
->will($this->returnValue('home::someuser'));
@ -31,13 +61,7 @@ class NodeTest extends \Test\TestCase {
}
public function testStat() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
@ -49,23 +73,17 @@ class NodeTest extends \Test\TestCase {
'permissions' => 0
);
$view->expects($this->once())
$this->view->expects($this->once())
->method('stat')
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals($stat, $node->stat());
}
public function testGetId() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
@ -76,23 +94,17 @@ class NodeTest extends \Test\TestCase {
'mtime' => 50
));
$view->expects($this->once())
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals(1, $node->getId());
}
public function testGetSize() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
@ -104,23 +116,17 @@ class NodeTest extends \Test\TestCase {
'mtime' => 50
));
$view->expects($this->once())
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals(100, $node->getSize());
}
public function testGetEtag() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
@ -131,23 +137,17 @@ class NodeTest extends \Test\TestCase {
'mtime' => 50
));
$view->expects($this->once())
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals('qwerty', $node->getEtag());
}
public function testGetMTime() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
@ -158,117 +158,91 @@ class NodeTest extends \Test\TestCase {
'mtime' => 50
));
$view->expects($this->once())
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals(50, $node->getMTime());
}
public function testGetStorage() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->getMock('\OC\Files\Storage\Storage');
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
->disableOriginalConstructor()
->getMock();
$view->expects($this->once())
$this->view->expects($this->once())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue(array($storage, 'foo')));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals($storage, $node->getStorage());
}
public function testGetPath() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals('/bar/foo', $node->getPath());
}
public function testGetInternalPath() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->getMock('\OC\Files\Storage\Storage');
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
->disableOriginalConstructor()
->getMock();
$view->expects($this->once())
$this->view->expects($this->once())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue(array($storage, 'foo')));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals('foo', $node->getInternalPath());
}
public function testGetName() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$this->assertEquals('foo', $node->getName());
}
public function testTouchSetMTime() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$view->expects($this->once())
$this->view->expects($this->once())
->method('touch')
->with('/bar/foo', 100)
->will($this->returnValue(true));
$view->expects($this->once())
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
$node = new \OC\Files\Node\Node($this->root, $this->view, '/bar/foo');
$node->touch(100);
$this->assertEquals(100, $node->getMTime());
}
@ -294,34 +268,26 @@ class NodeTest extends \Test\TestCase {
$hooksRun++;
};
/**
* @var \OC\Files\Mount\Manager $manager
*/
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = new \OC\Files\Node\Root($manager, $view, $this->user);
$root = new \OC\Files\Node\Root($this->manager, $this->view, $this->user);
$root->listen('\OC\Files', 'preTouch', $preListener);
$root->listen('\OC\Files', 'postTouch', $postListener);
$view->expects($this->once())
$this->view->expects($this->once())
->method('touch')
->with('/bar/foo', 100)
->will($this->returnValue(true));
$view->expects($this->any())
$this->view->expects($this->any())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue(array(null, 'foo')));
$view->expects($this->any())
$this->view->expects($this->any())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
$node = new \OC\Files\Node\Node($root, $this->view, '/bar/foo');
$node->touch(100);
$this->assertEquals(2, $hooksRun);
}
@ -330,22 +296,16 @@ class NodeTest extends \Test\TestCase {
* @expectedException \OCP\Files\NotPermittedException
*/
public function testTouchNotPermitted() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$view->expects($this->any())
$this->view->expects($this->any())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
$node = new \OC\Files\Node\Node($this->root, $this->view, '/bar/foo');
$node->touch(100);
}
@ -353,14 +313,7 @@ class NodeTest extends \Test\TestCase {
* @expectedException \OCP\Files\InvalidPathException
*/
public function testInvalidPath() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$node = new \OC\Files\Node\Node($root, $view, '/../foo');
$node = new \OC\Files\Node\Node($this->root, $this->view, '/../foo');
$node->getFileInfo();
}
}