[Pkg-owncloud-commits] [owncloud-doc] 83/270: add testing documentation

David Prévot taffit at moszumanska.debian.org
Thu Jul 31 03:53:04 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud-doc.

commit 7a884d470dccd1840c80f13f8ed2599539a21be5
Author: Bernhard Posselt <dev at bernhard-posselt.com>
Date:   Mon May 12 17:03:48 2014 +0200

    add testing documentation
---
 developer_manual/app/filesystem.rst | 10 ++++-
 developer_manual/app/testing.rst    | 80 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/developer_manual/app/filesystem.rst b/developer_manual/app/filesystem.rst
index 3cc0aad..85ee088 100644
--- a/developer_manual/app/filesystem.rst
+++ b/developer_manual/app/filesystem.rst
@@ -26,11 +26,16 @@ Filesystem classes can be injected from the ServerContainer by calling the metho
             $container = $this->getContainer();
 
             /**
-             * Database Layer
+             * Storage Layer
              */
             $container->registerService('AuthorStorage', function($c) {
-                return new AuthorStorage($c->query('ServerContainer')->getRootFolder());
+                return new AuthorStorage($c->query('RootStorage'));
             });
+
+            $container->registerService('RootStorage', function($c) {
+                return $c->query('ServerContainer')->getRootFolder();
+            });
+
         }
     }
 
@@ -62,6 +67,7 @@ All methods return a Folder object on which files and folders can be accessed, o
                     $file = $this->storage->get('/myfile.txt');
                 }
 
+                // the id can be accessed by $file->getId(); 
                 $file->putContent($content);
         
             } catch(\OCP\Files\NotPermittedException $e) {
diff --git a/developer_manual/app/testing.rst b/developer_manual/app/testing.rst
index 9ad6748..820b4b3 100644
--- a/developer_manual/app/testing.rst
+++ b/developer_manual/app/testing.rst
@@ -2,4 +2,82 @@
 Testing
 =======
 
-.. sectionauthor:: Bernhard Posselt <dev at bernhard-posselt.com>
\ No newline at end of file
+.. sectionauthor:: Bernhard Posselt <dev at bernhard-posselt.com>
+
+All PHP classes can be tested with `PHPUnit <http://phpunit.de/>`_, JavaScript can be tested by using `Karma <http://karma-runner.github.io/0.12/index.html>`_. 
+
+
+
+PHP
+===
+The PHP tests go into the **tests/** directory. Unfortunately the classloader in core requires a running server (as in fully configured and setup up with a database connection). This is unfortunately too complicated and slow so a seperate classloader has to be provided. If the app has been generated with the **ocdev startapp** command, the classloader is already present in the the **tests/** directory and PHPUnit can be run with::
+
+    phpunit tests/
+
+PHP classes should be tested by accessing them from the container to ensure that the container is wired up properly. Services that should be mocked can be replaced directly in the container.
+
+A test for the **AuthorStorage** class in :doc:`filesystem`:
+
+.. code-block:: php
+
+    <?php
+    namespace OCA\MyApp\Storage;
+
+    class AuthorStorage {
+
+        private $storage;
+
+        public function __construct($storage){
+            $this->storage = $storage;
+        }
+
+        public function getContent($id) {
+            // check if file exists and write to it if possible
+            try {
+                $file = $this->storage->getById($id);
+                if($file instanceof \OCP\Files\File) {
+                    return $file->getContent();
+                } else {
+                    throw new StorageException('Can not read from folder');
+                }
+            } catch(\OCP\Files\NotFoundException $e) {
+                throw new StorageException('File does not exist');
+            }
+        }
+    }
+
+would look like this:
+
+.. code-block:: php
+
+    <?php
+    // tests/unit/storage/AuthorStorageTest.php
+    namespace OCA\MyApp\Storage;
+    
+    class AuthorStorageTest extends \PHPUnit_Framework_TestCase {
+
+        private $container;
+
+        protected function setUp() {
+            $app = new \OCA\MyApp\AppInfo\Application();
+            $this->container = $notes->getContainer();
+            $this->container['RootStorage'] = $this->
+                getMockBuilder('\OCP\Files\Folder')->
+                disableOriginalConstructor()->
+                getMock();
+        }
+
+        /**
+         * @expectedException \OCA\MyApp\Storage\StorageException
+         */
+        public function testFileNotFound() {
+            $this->container['RootStorage']->
+                expects($this->once())->
+                method('get')->
+                with($this->equalTo(3))->
+                will($this->throwException(new \OCP\Files\NotFoundException()));
+
+            $this->container['AuthorStorage']->getContent(3);
+        }
+
+    }
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-doc.git



More information about the Pkg-owncloud-commits mailing list