diff options
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/AvatarManagerTest.php | 20 | ||||
-rw-r--r-- | tests/lib/AvatarTest.php | 22 | ||||
-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 |
6 files changed, 440 insertions, 20 deletions
diff --git a/tests/lib/AvatarManagerTest.php b/tests/lib/AvatarManagerTest.php index 0ad998af6d5..8ccc51d12e0 100644 --- a/tests/lib/AvatarManagerTest.php +++ b/tests/lib/AvatarManagerTest.php @@ -26,8 +26,8 @@ namespace Test; use OC\Avatar; use OC\AvatarManager; -use OCP\Files\Folder; -use OCP\Files\IRootFolder; +use OCP\Files\IAppData; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; use OCP\IL10N; use OCP\ILogger; @@ -40,8 +40,8 @@ use OCP\IUserManager; class AvatarManagerTest extends \Test\TestCase { /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ private $userManager; - /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ - private $rootFolder; + /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ + private $appData; /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ private $l10n; /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ @@ -55,14 +55,14 @@ class AvatarManagerTest extends \Test\TestCase { parent::setUp(); $this->userManager = $this->createMock(IUserManager::class); - $this->rootFolder = $this->createMock(IRootFolder::class); + $this->appData = $this->createMock(IAppData::class); $this->l10n = $this->createMock(IL10N::class); $this->logger = $this->createMock(ILogger::class); $this->config = $this->createMock(IConfig::class); $this->avatarManager = new AvatarManager( $this->userManager, - $this->rootFolder, + $this->appData, $this->l10n, $this->logger, $this->config @@ -94,11 +94,11 @@ class AvatarManagerTest extends \Test\TestCase { ->method('get') ->with('valid-user') ->willReturn($user); - $folder = $this->createMock(Folder::class); - $this->rootFolder + $folder = $this->createMock(ISimpleFolder::class); + $this->appData ->expects($this->once()) - ->method('get') - ->with('/valid-user') + ->method('getFolder') + ->with('valid-user') ->willReturn($folder); $expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config);; diff --git a/tests/lib/AvatarTest.php b/tests/lib/AvatarTest.php index 7f012c895fd..cea3f9bed1a 100644 --- a/tests/lib/AvatarTest.php +++ b/tests/lib/AvatarTest.php @@ -8,6 +8,8 @@ namespace Test; +use OC\Files\SimpleFS\SimpleFolder; +use OC\User\User; use OCP\Files\File; use OCP\Files\Folder; use OCP\IConfig; @@ -30,11 +32,11 @@ class AvatarTest extends \Test\TestCase { public function setUp() { parent::setUp(); - $this->folder = $this->createMock(Folder::class); + $this->folder = $this->createMock(SimpleFolder::class); /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject $l */ $l = $this->createMock(IL10N::class); $l->method('t')->will($this->returnArgument(0)); - $this->user = $this->getMockBuilder('OC\User\User')->disableOriginalConstructor()->getMock(); + $this->user = $this->createMock(User::class); $this->config = $this->createMock(IConfig::class); $this->avatar = new \OC\Avatar( @@ -51,7 +53,7 @@ class AvatarTest extends \Test\TestCase { } public function testGetAvatarSizeMatch() { - $this->folder->method('nodeExists') + $this->folder->method('fileExists') ->will($this->returnValueMap([ ['avatar.jpg', true], ['avatar.128.jpg', true], @@ -61,13 +63,13 @@ class AvatarTest extends \Test\TestCase { $file = $this->createMock(File::class); $file->method('getContent')->willReturn($expected->data()); - $this->folder->method('get')->with('avatar.128.jpg')->willReturn($file); + $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file); $this->assertEquals($expected->data(), $this->avatar->get(128)->data()); } public function testGetAvatarSizeMinusOne() { - $this->folder->method('nodeExists') + $this->folder->method('fileExists') ->will($this->returnValueMap([ ['avatar.jpg', true], ])); @@ -76,13 +78,13 @@ class AvatarTest extends \Test\TestCase { $file = $this->createMock(File::class); $file->method('getContent')->willReturn($expected->data()); - $this->folder->method('get')->with('avatar.jpg')->willReturn($file); + $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file); $this->assertEquals($expected->data(), $this->avatar->get(-1)->data()); } public function testGetAvatarNoSizeMatch() { - $this->folder->method('nodeExists') + $this->folder->method('fileExists') ->will($this->returnValueMap([ ['avatar.png', true], ['avatar.32.png', false], @@ -95,7 +97,7 @@ class AvatarTest extends \Test\TestCase { $file = $this->createMock(File::class); $file->method('getContent')->willReturn($expected->data()); - $this->folder->method('get') + $this->folder->method('getFile') ->will($this->returnCallback( function($path) use ($file) { if ($path === 'avatar.png') { @@ -126,7 +128,7 @@ class AvatarTest extends \Test\TestCase { } public function testExiststJPG() { - $this->folder->method('nodeExists') + $this->folder->method('fileExists') ->will($this->returnValueMap([ ['avatar.jpg', true], ['avatar.png', false], @@ -135,7 +137,7 @@ class AvatarTest extends \Test\TestCase { } public function testExistsPNG() { - $this->folder->method('nodeExists') + $this->folder->method('fileExists') ->will($this->returnValueMap([ ['avatar.jpg', false], ['avatar.png', true], 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]); + } + +} |