diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-24 00:22:00 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-24 00:22:00 +0300 |
commit | 716e03ea5d7d2ee07b7af9bc818c6339f4a40afe (patch) | |
tree | 2184d421bdce9d76c374a84631921656fa379f89 /tests | |
parent | 6d2397b67e3901609fb065e96269a6bda422ca9d (diff) |
Consolidate common test methods higher up
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/service/DownloadServiceTest.php | 55 | ||||
-rw-r--r-- | tests/unit/service/FilesServiceTest.php | 112 | ||||
-rw-r--r-- | tests/unit/service/SearchFolderServiceTest.php | 61 | ||||
-rw-r--r-- | tests/unit/service/SearchMediaServiceTest.php | 149 | ||||
-rw-r--r-- | tests/unit/service/ServiceTest.php | 61 |
5 files changed, 336 insertions, 102 deletions
diff --git a/tests/unit/service/DownloadServiceTest.php b/tests/unit/service/DownloadServiceTest.php index 7d226e49..99dcf164 100644 --- a/tests/unit/service/DownloadServiceTest.php +++ b/tests/unit/service/DownloadServiceTest.php @@ -10,29 +10,21 @@ * @copyright Olivier Paroz 2015 */ namespace OCA\Gallery\Service; +include_once 'FilesServiceTest.php'; -use OCP\ILogger; use OCP\Files\File; -use OCA\Gallery\Environment\Environment; - /** * Class DownloadServiceTest * * @package OCA\Gallery\Controller */ -class DownloadServiceTest extends \Test\TestCase { +class DownloadServiceTest extends FilesServiceTest { use Base64Encode; /** @var DownloadService */ protected $service; - /** @var string */ - protected $appName = 'gallery'; - /** @var Environment */ - private $environment; - /** @var ILogger */ - protected $logger; /** * Test set up @@ -40,12 +32,6 @@ class DownloadServiceTest extends \Test\TestCase { public function setUp() { parent::setUp(); - $this->environment = $this->getMockBuilder('\OCA\Gallery\Environment\Environment') - ->disableOriginalConstructor() - ->getMock(); - $this->logger = $this->getMockBuilder('\OCP\ILogger') - ->disableOriginalConstructor() - ->getMock(); $this->service = new DownloadService ( $this->appName, $this->environment, @@ -91,41 +77,4 @@ class DownloadServiceTest extends \Test\TestCase { $this->assertFalse($downloadResponse); } - /** - * Mocks OCP\Files\File - * - * Duplicate of PreviewControllerTest->mockFile - * - * Contains a JPG - * - * @param $fileId - * - * @return object|\PHPUnit_Framework_MockObject_MockObject - */ - private function mockFile($fileId) { - $file = $this->getMockBuilder('OCP\Files\File') - ->disableOriginalConstructor() - ->getMock(); - $file->method('getId') - ->willReturn($fileId); - $file->method('getContent') - ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); - $file->method('getName') - ->willReturn('testimage.jpg'); - $file->method('getMimeType') - ->willReturn('image/jpeg'); - - return $file; - } - - private function mockBadFile() { - $file = $this->getMockBuilder('OCP\Files\File') - ->disableOriginalConstructor() - ->getMock(); - $file->method('getContent') - ->willThrowException(new ServiceException("Can't read file")); - - return $file; - } - } diff --git a/tests/unit/service/FilesServiceTest.php b/tests/unit/service/FilesServiceTest.php new file mode 100644 index 00000000..d615dd54 --- /dev/null +++ b/tests/unit/service/FilesServiceTest.php @@ -0,0 +1,112 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ +namespace OCA\Gallery\Service; +include_once 'ServiceTest.php'; + +/** + * Class FilesServiceTest + * + * @package OCA\Gallery\Controller + */ +abstract class FilesServiceTest extends ServiceTest { + + /** + * Mocks OCP\Files\File + * + * Duplicate of PreviewControllerTest->mockFile + * + * Contains a JPG + * + * @param int $fileId + * @param string $storageId + * + * @return object|\PHPUnit_Framework_MockObject_MockObject + */ + protected function mockFile($fileId, $storageId = 'home::user', $isReadable = true) { + $storage = $this->mockGetStorage($storageId); + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $file->method('getId') + ->willReturn($fileId); + $file->method('getType') + ->willReturn('file'); + $file->method('getContent') + ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); + $file->method('getName') + ->willReturn('testimage.jpg'); + $file->method('getMimeType') + ->willReturn('image/jpeg'); + $file->method('getStorage') + ->willReturn($storage); + $file->method('isReadable') + ->willReturn($isReadable); + + return $file; + } + + protected function mockBadFile() { + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $file->method('getContent') + ->willThrowException(new ServiceException("Can't read file")); + + return $file; + } + + protected function mockGetFolder( + $storageId, + $nodeId, + $files, + $isReadable = true, + $mounted = false, + $mount = null, + $query = '', + $queryResult = false + ) { + $storage = $this->mockGetStorage($storageId); + $folder = $this->getMockBuilder('OCP\Files\Folder') + ->disableOriginalConstructor() + ->getMock(); + $folder->method('getType') + ->willReturn('dir'); + $folder->method('getId') + ->willReturn($nodeId); + $folder->method('getDirectoryListing') + ->willReturn($files); + $folder->method('getStorage') + ->willReturn($storage); + $folder->method('isReadable') + ->willReturn($isReadable); + $folder->method('isMounted') + ->willReturn($mounted); + $folder->method('getMountPoint') + ->willReturn($mount); + $folder->method('nodeExists') + ->with($query) + ->willReturn($queryResult); + + return $folder; + } + + protected function mockGetStorage($storageId) { + $storage = $this->getMockBuilder('OCP\Files\Storage') + ->disableOriginalConstructor() + ->getMock(); + $storage->method('getId') + ->willReturn($storageId); + + return $storage; + } + +} diff --git a/tests/unit/service/SearchFolderServiceTest.php b/tests/unit/service/SearchFolderServiceTest.php index 6f067f25..c60e6493 100644 --- a/tests/unit/service/SearchFolderServiceTest.php +++ b/tests/unit/service/SearchFolderServiceTest.php @@ -10,27 +10,17 @@ * @copyright Olivier Paroz 2015 */ namespace OCA\Gallery\Service; - -use OCP\ILogger; -use OCP\Files\File; - -use OCA\Gallery\Environment\Environment; +include_once 'ServiceTest.php'; /** * Class SearchFolderServiceTest * * @package OCA\Gallery\Controller */ -class SearchFolderServiceTest extends \Test\TestCase { +class SearchFolderServiceTest extends FilesServiceTest { /** @var SearchFolderService */ protected $service; - /** @var string */ - protected $appName = 'gallery'; - /** @var Environment */ - private $environment; - /** @var ILogger */ - protected $logger; /** * Test set up @@ -38,12 +28,6 @@ class SearchFolderServiceTest extends \Test\TestCase { public function setUp() { parent::setUp(); - $this->environment = $this->getMockBuilder('\OCA\Gallery\Environment\Environment') - ->disableOriginalConstructor() - ->getMock(); - $this->logger = $this->getMockBuilder('\OCP\ILogger') - ->disableOriginalConstructor() - ->getMock(); $this->service = new SearchFolderService ( $this->appName, $this->environment, @@ -144,6 +128,16 @@ class SearchFolderServiceTest extends \Test\TestCase { $this->assertFalse($response); } + public function testIsAllowedAndAvailableWithBrokenSetup() { + $node = $this->mockGetFolder('home::user', 909090, []); + $node->method('isReadable') + ->willThrowException(new \Exception('Boom')); + + $response = self::invokePrivate($this->service, 'isAllowedAndAvailable', [$node]); + + $this->assertFalse($response); + } + public function providesIsPreviewAllowedData() { return [ // Mounted, so looking at options @@ -178,37 +172,6 @@ class SearchFolderServiceTest extends \Test\TestCase { $this->assertSame($expectedResult, $response); } - - private function mockGetFolder( - $storageId, $nodeId, $files, $isReadable = true, $mounted = false, $mount = null - ) { - $storage = $this->getMockBuilder('OCP\Files\Storage') - ->disableOriginalConstructor() - ->getMock(); - $storage->method('getId') - ->willReturn($storageId); - - $folder = $this->getMockBuilder('OCP\Files\Folder') - ->disableOriginalConstructor() - ->getMock(); - $folder->method('getType') - ->willReturn('folder'); - $folder->method('getId') - ->willReturn($nodeId); - $folder->method('getDirectoryListing') - ->willReturn($files); - $folder->method('getStorage') - ->willReturn($storage); - $folder->method('isReadable') - ->willReturn($isReadable); - $folder->method('isMounted') - ->willReturn($mounted); - $folder->method('getMountPoint') - ->willReturn($mount); - - return $folder; - } - private function mockBrokenDirectoryListing() { $folder = $this->getMockBuilder('OCP\Files\Folder') ->disableOriginalConstructor() diff --git a/tests/unit/service/SearchMediaServiceTest.php b/tests/unit/service/SearchMediaServiceTest.php new file mode 100644 index 00000000..c5b8db11 --- /dev/null +++ b/tests/unit/service/SearchMediaServiceTest.php @@ -0,0 +1,149 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ +namespace OCA\Gallery\Service; +include_once 'FilesServiceTest.php'; + +/** + * Class SearchMediaServiceTest + * + * @package OCA\Gallery\Controller + */ +class SearchMediaServiceTest extends FilesServiceTest { + + /** @var SearchMediaService */ + protected $service; + + /** + * Test set up + */ + public function setUp() { + parent::setUp(); + + $this->service = new SearchMediaService ( + $this->appName, + $this->environment, + $this->logger + ); + } + + public function testIsPreviewAvailable() { + $file = $this->mockBadFile(); + + $result = self::invokePrivate($this->service, 'isPreviewAvailable', [$file]); + + $this->assertFalse($result); + } + + public function testGetMediaFilesWithUnavailableFolder() { + $isReadable = false; + $files = []; + $topFolder = $this->mockGetFolder( + 'home::user', 545454, $files, $isReadable + ); + $supportedMediaTypes = []; + $features = []; + $response = $this->service->getMediaFiles($topFolder, $supportedMediaTypes, $features); + + $this->assertSame([], $response); + } + + public function providesTopFolderData() { + $isReadable = true; + $mounted = false; + $mount = null; + $query = '.nomedia'; + $queryResult = false; + + $folder1 = $this->mockGetFolder( + 'home::user', 545454, [ + $this->mockFile(11111), + $this->mockFile(22222), + $this->mockFile(33333) + ], $isReadable, $mounted, $mount, $query, $queryResult + ); + $folder2 = $this->mockGetFolder( + 'home::user', 767676, [ + $this->mockFile(44444), + $this->mockFile(55555), + $this->mockFile(66666) + ], $isReadable, $mounted, $mount, $query, $queryResult + ); + $folder3 = $this->mockGetFolder( + 'home::user', 10101, [$folder1], $isReadable, $mounted, $mount, $query, $queryResult + ); + $folder4 = $this->mockGetFolder( + 'home::user', 10101, [$folder1, $folder2], $isReadable, $mounted, $mount, $query, + $queryResult + ); + + // 2 folders and 3 files, everything is reachable + $config1 = [ + $folder1, + $folder2, + $this->mockFile(77777), + $this->mockFile(88888), + $this->mockFile(99999) + + ]; + // 2 deepfolder and 3 files. Should return the all the files + $config2 = [ + $folder3, + $folder3, + $this->mockFile(77777), + $this->mockFile(88888), + $this->mockFile(99999) + + ]; + // 1 deepfolder (with 2 sub-folders) and 3 files. Should return the files and the content of 1 folder + $config3 = [ + $folder4, + $this->mockFile(77777), + $this->mockFile(88888), + $this->mockFile(99999) + ]; + $topFolder1 = $this->mockGetFolder( + 'home::user', 909090, $config1, $isReadable, $mounted, $mount, $query, $queryResult + ); + $topFolder2 = $this->mockGetFolder( + 'home::user', 909090, $config2, $isReadable, $mounted, $mount, $query, $queryResult + ); + $topFolder3 = $this->mockGetFolder( + 'home::user', 909090, $config3, $isReadable, $mounted, $mount, $query, $queryResult + ); + + return [ + [$topFolder1, 9], + [$topFolder2, 9], + [$topFolder3, 6] + ]; + } + + /** + * @dataProvider providesTopFolderData + * + * @param array $topFolder + * @param int $result + */ + public function testGetMediaFiles($topFolder, $result) { + $supportedMediaTypes = [ + 'image/png', + 'image/jpeg', + 'image/gif' + ]; + $features = []; + + $response = $this->service->getMediaFiles($topFolder, $supportedMediaTypes, $features); + + $this->assertSame($result, sizeof($response)); + } + +} diff --git a/tests/unit/service/ServiceTest.php b/tests/unit/service/ServiceTest.php new file mode 100644 index 00000000..742fbb1f --- /dev/null +++ b/tests/unit/service/ServiceTest.php @@ -0,0 +1,61 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ +namespace OCA\Gallery\Service; + +use OCP\ILogger; + +use OCA\Gallery\Environment\Environment; + +/** + * Class FilesServiceTest + * + * @package OCA\Gallery\Controller + */ +abstract class ServiceTest extends \Test\TestCase { + + /** @var string */ + protected $appName = 'gallery'; + /** @var Environment */ + protected $environment; + /** @var ILogger */ + protected $logger; + + /** + * Test set up + */ + protected function setUp() { + parent::setUp(); + + $this->environment = $this->getMockBuilder('\OCA\Gallery\Environment\Environment') + ->disableOriginalConstructor() + ->getMock(); + $this->logger = $this->getMockBuilder('\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Mocks Environment->getResourceFromId + * + * Needs to pass a mock of a File or Folder + * + * @param int $fileId + * @param object|\PHPUnit_Framework_MockObject_MockObject|bool $answer + */ + protected function mockGetResourceFromId($fileId, $answer) { + $this->environment->expects($this->once()) + ->method('getResourceFromId') + ->with($this->equalTo($fileId)) + ->willReturn($answer); + } + +} |