diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2016-09-12 14:57:15 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2016-10-05 11:00:16 +0200 |
commit | ac38a3a654df909d2c0c9d7c4d84e8e5ea2c587a (patch) | |
tree | 3986315ba299b88f1952cf44358e49ea031d919d /tests | |
parent | 5d8b941fea23f09586b825324d0dccd39284bc26 (diff) | |
download | nextcloud-server-ac38a3a654df909d2c0c9d7c4d84e8e5ea2c587a.tar.gz nextcloud-server-ac38a3a654df909d2c0c9d7c4d84e8e5ea2c587a.zip |
Add Tests
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Files/AppData/AppDataTest.php | 121 | ||||
-rw-r--r-- | tests/lib/Files/AppData/FactoryTest.php | 55 | ||||
-rw-r--r-- | tests/lib/Files/SimpleFS/SimpleFileTest.php | 104 | ||||
-rw-r--r-- | tests/lib/Files/SimpleFS/SimpleFolderTest.php | 138 |
4 files changed, 418 insertions, 0 deletions
diff --git a/tests/lib/Files/AppData/AppDataTest.php b/tests/lib/Files/AppData/AppDataTest.php new file mode 100644 index 00000000000..3247ce7ba99 --- /dev/null +++ b/tests/lib/Files/AppData/AppDataTest.php @@ -0,0 +1,121 @@ +<?php +/** + * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\Files\AppData; + +use OC\Files\AppData\AppData; +use OC\SystemConfig; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IAppData; +use OCP\Files\IRootFolder; +use OCP\Files\Node; +use OCP\Files\SimpleFS\ISimpleFolder; + +class AppDataTest extends \Test\TestCase { + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $systemConfig; + + /** @var IAppData */ + private $appData; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->systemConfig = $this->createMock(SystemConfig::class); + $this->appData = new AppData($this->rootFolder, $this->systemConfig, 'myApp'); + + $this->systemConfig->expects($this->any()) + ->method('getValue') + ->with('instanceid', null) + ->willReturn('iid'); + } + + private function setupAppFolder() { + $dataFolder = $this->createMock(Folder::class); + $appFolder = $this->createMock(Folder::class); + + $this->rootFolder->expects($this->once()) + ->method('get') + ->with($this->equalTo('appdata_iid')) + ->willReturn($dataFolder); + $dataFolder->expects($this->once()) + ->method('get') + ->with($this->equalTo('myApp')) + ->willReturn($appFolder); + + return [$dataFolder, $appFolder]; + } + + public function testGetFolder() { + $folders = $this->setupAppFolder(); + $appFolder = $folders[1]; + + $folder = $this->createMock(Folder::class); + + $appFolder->expects($this->once()) + ->method('get') + ->with($this->equalTo('folder')) + ->willReturn($folder); + + $result = $this->appData->getFolder('folder'); + $this->assertInstanceOf(ISimpleFolder::class, $result); + } + + public function testNewFolder() { + $folders = $this->setupAppFolder(); + $appFolder = $folders[1]; + + $folder = $this->createMock(Folder::class); + + $appFolder->expects($this->once()) + ->method('newFolder') + ->with($this->equalTo('folder')) + ->willReturn($folder); + + $result = $this->appData->newFolder('folder'); + $this->assertInstanceOf(ISimpleFolder::class, $result); + } + + public function testGetDirectoryListing() { + $folders = $this->setupAppFolder(); + $appFolder = $folders[1]; + + $file = $this->createMock(File::class); + $folder = $this->createMock(Folder::class); + $node = $this->createMock(Node::class); + + $appFolder->expects($this->once()) + ->method('getDirectoryListing') + ->willReturn([$file, $folder, $node]); + + $result = $this->appData->getDirectoryListing(); + + $this->assertCount(1, $result); + $this->assertInstanceOf(ISimpleFolder::class, $result[0]); + } + +} diff --git a/tests/lib/Files/AppData/FactoryTest.php b/tests/lib/Files/AppData/FactoryTest.php new file mode 100644 index 00000000000..75999c8c7da --- /dev/null +++ b/tests/lib/Files/AppData/FactoryTest.php @@ -0,0 +1,55 @@ +<?php +/** + * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\Files\AppData; + +use OC\Files\AppData\Factory; +use OC\SystemConfig; +use OCP\Files\IRootFolder; + +class FactoryTest extends \Test\TestCase { + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $systemConfig; + + /** @var Factory */ + private $factory; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->systemConfig = $this->createMock(SystemConfig::class); + $this->factory = new Factory($this->rootFolder, $this->systemConfig); + } + + public function testGet() { + $this->rootFolder->expects($this->never()) + ->method($this->anything()); + $this->systemConfig->expects($this->never()) + ->method($this->anything()); + + $this->factory->get('foo'); + } +} diff --git a/tests/lib/Files/SimpleFS/SimpleFileTest.php b/tests/lib/Files/SimpleFS/SimpleFileTest.php new file mode 100644 index 00000000000..4e623eafa22 --- /dev/null +++ b/tests/lib/Files/SimpleFS/SimpleFileTest.php @@ -0,0 +1,104 @@ +<?php +/** + * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\File\SimpleFS; + +use OC\Files\SimpleFS\SimpleFile; +use OCP\Files\File; + +class SimpleFileTest extends \Test\TestCase { + /** @var File|\PHPUnit_Framework_MockObject_MockObject */ + private $file; + + /** @var SimpleFile */ + private $simpleFile; + + public function setUp() { + parent::setUp(); + + $this->file = $this->createMock(File::class); + $this->simpleFile = new SimpleFile($this->file); + } + + public function testGetName() { + $this->file->expects($this->once()) + ->method('getName') + ->willReturn('myname'); + + $this->assertEquals('myname', $this->simpleFile->getName()); + } + + public function testGetSize() { + $this->file->expects($this->once()) + ->method('getSize') + ->willReturn(42); + + $this->assertEquals(42, $this->simpleFile->getSize()); + } + + public function testGetETag() { + $this->file->expects($this->once()) + ->method('getETag') + ->willReturn('etag'); + + $this->assertEquals('etag', $this->simpleFile->getETag()); + } + + public function testGetMTime() { + $this->file->expects($this->once()) + ->method('getMTime') + ->willReturn(101); + + $this->assertEquals(101, $this->simpleFile->getMTime()); + } + + public function testGetContent() { + $this->file->expects($this->once()) + ->method('getContent') + ->willReturn('foo'); + + $this->assertEquals('foo', $this->simpleFile->getContent()); + } + + public function testPutContent() { + $this->file->expects($this->once()) + ->method('putContent') + ->with($this->equalTo('bar')); + + $this->simpleFile->putContent('bar'); + } + + public function testDelete() { + $this->file->expects($this->once()) + ->method('delete'); + + $this->simpleFile->delete(); + } + + public function testGetMimeType() { + $this->file->expects($this->once()) + ->method('getMimeType') + ->willReturn('app/awesome'); + + $this->assertEquals('app/awesome', $this->simpleFile->getMimeType()); + } +} diff --git a/tests/lib/Files/SimpleFS/SimpleFolderTest.php b/tests/lib/Files/SimpleFS/SimpleFolderTest.php new file mode 100644 index 00000000000..d86c705d880 --- /dev/null +++ b/tests/lib/Files/SimpleFS/SimpleFolderTest.php @@ -0,0 +1,138 @@ +<?php +/** + * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\File\SimpleFS; + +use OC\Files\SimpleFS\SimpleFolder; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\Node; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; + +class SimpleFolderTest extends \Test\TestCase { + /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */ + private $folder; + + /** @var SimpleFolder */ + private $simpleFolder; + + public function setUp() { + parent::setUp(); + + $this->folder = $this->createMock(Folder::class); + $this->simpleFolder = new SimpleFolder($this->folder); + } + + public function testGetName() { + $this->folder->expects($this->once()) + ->method('getName') + ->willReturn('myname'); + + $this->assertEquals('myname', $this->simpleFolder->getName()); + } + + public function testDelete() { + $this->folder->expects($this->once()) + ->method('delete'); + + $this->simpleFolder->delete(); + } + + public function dataFileExists() { + return [ + [true], + [false], + ]; + } + + /** + * @dataProvider dataFileExists + * @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')); + } + + 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); + } catch (NotFoundException $e) { + $this->assertTrue($exception); + } + } + + 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'); + $this->assertInstanceOf(ISimpleFile::class, $result); + } + + public function testGetDirectoryListing() { + $file = $this->createMock(File::class); + $folder = $this->createMock(Folder::class); + $node = $this->createMock(Node::class); + + $this->folder->expects($this->once()) + ->method('getDirectoryListing') + ->willReturn([$file, $folder, $node]); + + $result = $this->simpleFolder->getDirectoryListing(); + + $this->assertCount(1, $result); + $this->assertInstanceOf(ISimpleFile::class, $result[0]); + } + +} |