Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Paroz <github@oparoz.com>2015-08-23 06:16:24 +0300
committerOlivier Paroz <github@oparoz.com>2015-08-23 06:16:24 +0300
commit1f29ab0986c760cb67e238fcf6fae41738a5969d (patch)
treeec9b4c54caaf1fa225ba0f3832f074888d85906c /tests/unit
parent56cc5fd801a449820d1a93d57ba5e3b9c45bc224 (diff)
Some unit tests for the DownloadService class
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/http/ImageResponseTest.php5
-rw-r--r--tests/unit/service/DownloadServiceTest.php131
2 files changed, 132 insertions, 4 deletions
diff --git a/tests/unit/http/ImageResponseTest.php b/tests/unit/http/ImageResponseTest.php
index 62b3d99c..a038f556 100644
--- a/tests/unit/http/ImageResponseTest.php
+++ b/tests/unit/http/ImageResponseTest.php
@@ -9,13 +9,10 @@
*
* @copyright Olivier Paroz 2015
*/
-namespace OCA\Gallery\Controller;
+namespace OCA\Gallery\Http;
use OCP\AppFramework\Http;
-use OCA\Gallery\Http\ImageResponse;
-
-
/**
* Class ImageResponseTest
*
diff --git a/tests/unit/service/DownloadServiceTest.php b/tests/unit/service/DownloadServiceTest.php
new file mode 100644
index 00000000..7d226e49
--- /dev/null
+++ b/tests/unit/service/DownloadServiceTest.php
@@ -0,0 +1,131 @@
+<?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 OCP\Files\File;
+
+use OCA\Gallery\Environment\Environment;
+
+/**
+ * Class DownloadServiceTest
+ *
+ * @package OCA\Gallery\Controller
+ */
+class DownloadServiceTest extends \Test\TestCase {
+
+ use Base64Encode;
+
+ /** @var DownloadService */
+ protected $service;
+ /** @var string */
+ protected $appName = 'gallery';
+ /** @var Environment */
+ private $environment;
+ /** @var ILogger */
+ protected $logger;
+
+ /**
+ * Test set up
+ */
+ 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,
+ $this->logger
+ );
+ }
+
+ public function testDownloadRawFile() {
+ /** @type File $file */
+ $file = $this->mockFile(12345);
+
+ $download = [
+ 'preview' => $file->getContent(),
+ 'mimetype' => $file->getMimeType()
+ ];
+
+ $downloadResponse = $this->service->downloadFile($file);
+
+ $this->assertSame($download['mimetype'], $downloadResponse['mimetype']);
+ $this->assertSame($download['preview'], $downloadResponse['preview']);
+ }
+
+ public function testDownloadBase64EncodedFile() {
+ /** @type File $file */
+ $file = $this->mockFile(12345);
+
+ $download = [
+ 'preview' => $this->encode($file->getContent()),
+ 'mimetype' => $file->getMimeType()
+ ];
+
+ $downloadResponse = $this->service->downloadFile($file, true);
+
+ $this->assertSame($download['mimetype'], $downloadResponse['mimetype']);
+ $this->assertSame($download['preview'], $downloadResponse['preview']);
+ }
+
+ public function testDownloadNonExistentFile() {
+ $file = $this->mockBadFile(12345);
+
+ $downloadResponse = $this->service->downloadFile($file);
+
+ $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;
+ }
+
+}