From ac38a3a654df909d2c0c9d7c4d84e8e5ea2c587a Mon Sep 17 00:00:00 2001
From: Roeland Jago Douma <roeland@famdouma.nl>
Date: Mon, 12 Sep 2016 14:57:15 +0200
Subject: [PATCH] Add Tests

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
---
 lib/private/Files/AppData/AppData.php         |  23 +--
 lib/private/Files/SimpleFS/SimpleFolder.php   |  15 +-
 tests/lib/Files/AppData/AppDataTest.php       | 121 +++++++++++++++
 tests/lib/Files/AppData/FactoryTest.php       |  55 +++++++
 tests/lib/Files/SimpleFS/SimpleFileTest.php   | 104 +++++++++++++
 tests/lib/Files/SimpleFS/SimpleFolderTest.php | 138 ++++++++++++++++++
 6 files changed, 444 insertions(+), 12 deletions(-)
 create mode 100644 tests/lib/Files/AppData/AppDataTest.php
 create mode 100644 tests/lib/Files/AppData/FactoryTest.php
 create mode 100644 tests/lib/Files/SimpleFS/SimpleFileTest.php
 create mode 100644 tests/lib/Files/SimpleFS/SimpleFolderTest.php

diff --git a/lib/private/Files/AppData/AppData.php b/lib/private/Files/AppData/AppData.php
index f2180a48e61..270e834b8e5 100644
--- a/lib/private/Files/AppData/AppData.php
+++ b/lib/private/Files/AppData/AppData.php
@@ -28,10 +28,11 @@ use OCP\Files\IAppData;
 use OCP\Files\IRootFolder;
 use OCP\Files\Folder;
 use OC\SystemConfig;
+use OCP\Files\Node;
 use OCP\Files\NotFoundException;
 use OCP\Files\NotPermittedException;
 
-class AppData extends SimpleRoot implements IAppData {
+class AppData implements IAppData {
 
 	/** @var IRootFolder */
 	private $rootFolder;
@@ -42,6 +43,9 @@ class AppData extends SimpleRoot implements IAppData {
 	/** @var string */
 	private $appId;
 
+	/** @var Folder */
+	private $folder;
+
 	/**
 	 * AppData constructor.
 	 *
@@ -97,9 +101,6 @@ class AppData extends SimpleRoot implements IAppData {
 		return $this->folder;
 	}
 
-	/**
-	 * @inheritdoc
-	 */
 	public function getFolder($name) {
 		$node = $this->getAppDataFolder()->get($name);
 
@@ -107,9 +108,6 @@ class AppData extends SimpleRoot implements IAppData {
 		return new SimpleFolder($node);
 	}
 
-	/**
-	 * @inheritdoc
-	 */
 	public function newFolder($name) {
 		$folder = $this->getAppDataFolder()->newFolder($name);
 
@@ -119,10 +117,15 @@ class AppData extends SimpleRoot implements IAppData {
 	public function getDirectoryListing() {
 		$listing = $this->getAppDataFolder()->getDirectoryListing();
 
-		$fileListing = array_map(function(Node $file) {
-			return new SimpleFolder($file);
+		$fileListing = array_map(function(Node $folder) {
+			if ($folder instanceof Folder) {
+				return new SimpleFolder($folder);
+			}
+			return null;
 		}, $listing);
 
-		return $fileListing;
+		$fileListing = array_filter($fileListing);
+
+		return array_values($fileListing);
 	}
 }
diff --git a/lib/private/Files/SimpleFS/SimpleFolder.php b/lib/private/Files/SimpleFS/SimpleFolder.php
index 4ee61f0eec9..8ce6c013c1f 100644
--- a/lib/private/Files/SimpleFS/SimpleFolder.php
+++ b/lib/private/Files/SimpleFS/SimpleFolder.php
@@ -22,8 +22,10 @@
  */
 namespace OC\Files\SimpleFS;
 
+use OCP\Files\File;
 use OCP\Files\Folder;
 use OCP\Files\Node;
+use OCP\Files\NotFoundException;
 use OCP\Files\SimpleFS\ISimpleFolder;
 
 class SimpleFolder implements ISimpleFolder   {
@@ -48,10 +50,15 @@ class SimpleFolder implements ISimpleFolder   {
 		$listing = $this->folder->getDirectoryListing();
 
 		$fileListing = array_map(function(Node $file) {
-			return new SimpleFile($file);
+			if ($file instanceof File) {
+				return new SimpleFile($file);
+			}
+			return null;
 		}, $listing);
 
-		return $fileListing;
+		$fileListing = array_filter($fileListing);
+
+		return array_values($fileListing);
 	}
 
 	public function delete() {
@@ -61,6 +68,10 @@ class SimpleFolder implements ISimpleFolder   {
 	public function getFile($name) {
 		$file = $this->folder->get($name);
 
+		if (!($file instanceof File)) {
+			throw new NotFoundException();
+		}
+
 		return new SimpleFile($file);
 	}
 
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]);
+	}
+
+}
-- 
GitLab