From fac9c269d4ebaac5f7eef79cbf415ec11d3738cb Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 28 Nov 2012 15:24:39 +0100 Subject: [PATCH] adjusted tutorial to apptemplate_advanced app and added unittests doc --- developer_manual/tutorial.rst | 84 +++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/developer_manual/tutorial.rst b/developer_manual/tutorial.rst index beb7aff3c..cfdc02441 100644 --- a/developer_manual/tutorial.rst +++ b/developer_manual/tutorial.rst @@ -30,14 +30,14 @@ To disable JavaScript and CSS caching you'll have to turn on debugging in :file: Create your app --------------- -The best way to create your application is to simply modify the apptemplate app. +The best way to create your application is to simply modify the apptemplate_advanced app. To do that execute: .. code-block:: bash cd /var/www/apps - sudo cp -r apptemplate yourappname + sudo cp -r apptemplate_advanced yourappname sudo chown -R youruser:yourgroup yourappname To enable your app, simply link it into the apps directory: @@ -67,8 +67,8 @@ Certain things are still apptemplate specific and you will have to convert them The following things will need to be changed: * AGPL Header: author and copyright -* **\\OC_App::getAppPath('apptemplate')** to **\\OC_App::getAppPath('yourappname')** -* **namespace OCA\\AppTemplate** to **namespace OCA\\YourAppName** +* **\\OC_App::getAppPath('apptemplate_advanced')** to **\\OC_App::getAppPath('yourappname')** +* **namespace OCA\\AppTemplateAdvanced** to **namespace OCA\\YourAppName** * The Classpaths in :file:`appinfo/bootstrap.php` @@ -432,6 +432,82 @@ If you have to include an image in your CSS, use %appswebroot% and %webroot% for } +Unittests +--------- +Unittests go into your **tests/** directory. Create the same folder structure in the tests directory like on your app to make it easier to find tests for certain classes. + +Owncloud uses `PHPUnit `_ + +Because of Dependency Injection, unittesting has become very easy: you can easily substitute complex classes with mocks by simply passing a different object to the constructor. + +Also using a container like Pimple frees us from doing complex instantiation and object passing in our application by hand. + + +A simple test for a controller would look like this + +.. code-block:: php + + 'this is a test'); + $request = new \OCA\AppTemplateAdvanced\Request(null, $post); + $api = new APIMock(); + + $controller = new \OCA\AppTemplateAdvanced\AjaxController($api, $request); + $controller->setSystemValue(); + + $this->assertEquals($post['somesetting'], $api->setSystemValueData['somesetting']); + } + + + } + + +This test uses a mock of the API class. You can define the behaviour of the class in an own file + +.. code-block:: php + + setSystemValueData = array(); + } + + + public function getAppName(){ + return 'apptemplate_advanced'; + } + + + public function setSystemValue($key, $value){ + $this->setSystemValueData[$key] = $value; + } + + + } + +You can now execute the test by chaning into its directory and calling phpunit on it:: + + cd tests/controllers/ + phpunit ajax.controller.test.php + + +**See also** :doc:`unittests` + Publish your app ---------------- At `apps.owncloud.com `_ for other ownCloud users \ No newline at end of file