summaryrefslogtreecommitdiffstats
path: root/utility
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-04-09 01:44:12 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-04-09 22:52:26 +0200
commit21bd539847f33c3889c4f58f14afd672f54a410a (patch)
treed6ff27e38727197b60d1c2c47968b7f64d4f5c2f /utility
parentfcef0800a24818305e8a52761b05f87e13206689 (diff)
ported to owncloud internal appframework classes, confused with how to start the app and define deps
Diffstat (limited to 'utility')
-rw-r--r--utility/config.php2
-rw-r--r--utility/controllertestutility.php115
-rw-r--r--utility/mappertestutility.php180
-rw-r--r--utility/methodannotationreader.php61
-rw-r--r--utility/simplepieapifactory.php2
-rw-r--r--utility/testutility.php46
6 files changed, 404 insertions, 2 deletions
diff --git a/utility/config.php b/utility/config.php
index 54145c993..193411592 100644
--- a/utility/config.php
+++ b/utility/config.php
@@ -25,7 +25,7 @@
namespace OCA\News\Utility;
-use \OCA\AppFramework\Core\API;
+use \OCA\News\Core\API;
class Config {
diff --git a/utility/controllertestutility.php b/utility/controllertestutility.php
new file mode 100644
index 000000000..07de3e4e9
--- /dev/null
+++ b/utility/controllertestutility.php
@@ -0,0 +1,115 @@
+<?php
+
+/**
+ * ownCloud - News
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\News\Utility;
+
+use OCP\IRequest;
+use OCP\AppFramework\Http\Response;
+
+
+/**
+ * Simple utility class for testing controllers
+ */
+abstract class ControllerTestUtility extends TestUtility {
+
+
+ /**
+ * Checks if a controllermethod has the expected annotations
+ * @param Controller/string $controller name or instance of the controller
+ * @param array $expected an array containing the expected annotations
+ * @param array $valid if you define your own annotations, pass them here
+ */
+ protected function assertAnnotations($controller, $method, array $expected,
+ array $valid=array()){
+ $standard = array(
+ 'Ajax',
+ 'CSRFExemption',
+ 'IsAdminExemption',
+ 'IsSubAdminExemption',
+ 'IsLoggedInExemption',
+ 'API'
+ );
+
+ $possible = array_merge($standard, $valid);
+
+ // check if expected annotations are valid
+ foreach($expected as $annotation){
+ $this->assertTrue(in_array($annotation, $possible));
+ }
+
+ $reader = new MethodAnnotationReader($controller, $method);
+ foreach($expected as $annotation){
+ $this->assertTrue($reader->hasAnnotation($annotation));
+ }
+ }
+
+
+ /**
+ * Shortcut for testing expected headers of a response
+ * @param array $expected an array with the expected headers
+ * @param Response $response the response which we want to test for headers
+ */
+ protected function assertHeaders(array $expected=array(), Response $response){
+ $headers = $reponse->getHeaders();
+ foreach($expected as $header){
+ $this->assertTrue(in_array($header, $headers));
+ }
+ }
+
+
+ /**
+ * Instead of using positional parameters this function instantiates
+ * a request by using a hashmap so its easier to only set specific params
+ * @param array $params a hashmap with the parameters for request
+ * @return Request a request instance
+ */
+ protected function getRequest(array $params=array()) {
+ $mock = $this->getMockBuilder('\OCP\IRequest')
+ ->getMock();
+
+ $merged = array();
+
+ foreach ($params as $key => $value) {
+ $merged = array_merge($value, $merged);
+ }
+
+ $mock->expects($this->any())
+ ->method('getParam')
+ ->will($this->returnCallback(function($index, $default) use ($merged) {
+ if (array_key_exists($index, $merged)) {
+ return $merged[$index];
+ } else {
+ return $default;
+ }
+ }));
+
+ // attribute access
+ if(array_key_exists('server', $params)) {
+ $mock->server = $params['server'];
+ }
+
+ return $mock;
+ }
+
+}
diff --git a/utility/mappertestutility.php b/utility/mappertestutility.php
new file mode 100644
index 000000000..d78e717aa
--- /dev/null
+++ b/utility/mappertestutility.php
@@ -0,0 +1,180 @@
+<?php
+
+/**
+ * ownCloud - News
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\News\Utility;
+
+use OCA\News\Core\Api;
+
+
+/**
+ * Simple utility class for testing mappers
+ */
+abstract class MapperTestUtility extends TestUtility {
+
+
+ protected $api;
+ private $query;
+ private $pdoResult;
+ private $queryAt;
+ private $prepareAt;
+ private $fetchAt;
+ private $iterators;
+
+
+ /**
+ * Run this function before the actual test to either set or initialize the
+ * api. After this the api can be accessed by using $this->api
+ */
+ protected function beforeEach(){
+ $this->api = $this->getMock('\OCA\News\Core\API',
+ array('prepareQuery', 'getInsertId'),
+ array('a'));
+
+ $this->query = $this->getMock('Query', array('execute', 'bindValue'));
+ $this->pdoResult = $this->getMock('Result', array('fetchRow'));
+ $this->queryAt = 0;
+ $this->prepareAt = 0;
+ $this->iterators = array();
+ $this->fetchAt = 0;
+ }
+
+
+ /**
+ * Create mocks and set expected results for database queries
+ * @param string $sql the sql query that you expect to receive
+ * @param array $arguments the expected arguments for the prepare query
+ * method
+ * @param array $returnRows the rows that should be returned for the result
+ * of the database query. If not provided, it wont be assumed that fetchRow
+ * will be called on the result
+ */
+ protected function setMapperResult($sql, $arguments=array(), $returnRows=array(),
+ $limit=null, $offset=null){
+
+ $this->iterators[] = new ArgumentIterator($returnRows);
+
+ $iterators = $this->iterators;
+ $fetchAt = $this->fetchAt;
+
+ $this->pdoResult->expects($this->any())
+ ->method('fetchRow')
+ ->will($this->returnCallback(
+ function() use ($iterators, $fetchAt){
+ $iterator = $iterators[$fetchAt];
+ $result = $iterator->next();
+
+ if($result === false) {
+ $fetchAt++;
+ }
+
+ return $result;
+ }
+ ));
+
+ $index = 1;
+ foreach($arguments as $argument) {
+ switch (gettype($argument)) {
+ case 'integer':
+ $pdoConstant = \PDO::PARAM_INT;
+ break;
+
+ case 'NULL':
+ $pdoConstant = \PDO::PARAM_NULL;
+ break;
+
+ case 'boolean':
+ $pdoConstant = \PDO::PARAM_BOOL;
+ break;
+
+ default:
+ $pdoConstant = \PDO::PARAM_STR;
+ break;
+ }
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($index),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $index++;
+ $this->queryAt++;
+ }
+
+ $this->query->expects($this->at($this->queryAt))
+ ->method('execute')
+ ->with()
+ ->will($this->returnValue($this->pdoResult));
+ $this->queryAt++;
+
+ if($limit === null && $offset === null) {
+ $this->api->expects($this->at($this->prepareAt))
+ ->method('prepareQuery')
+ ->with($this->equalTo($sql))
+ ->will(($this->returnValue($this->query)));
+ } elseif($limit !== null && $offset === null) {
+ $this->api->expects($this->at($this->prepareAt))
+ ->method('prepareQuery')
+ ->with($this->equalTo($sql), $this->equalTo($limit))
+ ->will(($this->returnValue($this->query)));
+ } elseif($limit === null && $offset !== null) {
+ $this->api->expects($this->at($this->prepareAt))
+ ->method('prepareQuery')
+ ->with($this->equalTo($sql),
+ $this->equalTo(null),
+ $this->equalTo($offset))
+ ->will(($this->returnValue($this->query)));
+ } else {
+ $this->api->expects($this->at($this->prepareAt))
+ ->method('prepareQuery')
+ ->with($this->equalTo($sql),
+ $this->equalTo($limit),
+ $this->equalTo($offset))
+ ->will(($this->returnValue($this->query)));
+ }
+ $this->prepareAt++;
+ $this->fetchAt++;
+
+ }
+
+
+}
+
+
+class ArgumentIterator {
+
+ private $arguments;
+
+ public function __construct($arguments){
+ $this->arguments = $arguments;
+ }
+
+ public function next(){
+ $result = array_shift($this->arguments);
+ if($result === null){
+ return false;
+ } else {
+ return $result;
+ }
+ }
+}
+
diff --git a/utility/methodannotationreader.php b/utility/methodannotationreader.php
new file mode 100644
index 000000000..dc2347b3d
--- /dev/null
+++ b/utility/methodannotationreader.php
@@ -0,0 +1,61 @@
+<?php
+
+/**
+ * ownCloud - News
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\News\Utility;
+
+
+/**
+ * Reads and parses annotations from doc comments
+ */
+class MethodAnnotationReader {
+
+ private $annotations;
+
+ /**
+ * @param object $object an object or classname
+ * @param string $method the method which we want to inspect for annotations
+ */
+ public function __construct($object, $method){
+ $this->annotations = array();
+
+ $reflection = new \ReflectionMethod($object, $method);
+ $docs = $reflection->getDocComment();
+
+ // extract everythin prefixed by @ and first letter uppercase
+ preg_match_all('/@([A-Z]\w+)/', $docs, $matches);
+ $this->annotations = $matches[1];
+ }
+
+
+ /**
+ * Check if a method contains an annotation
+ * @param string $name the name of the annotation
+ * @return bool true if the annotation is found
+ */
+ public function hasAnnotation($name){
+ return in_array($name, $this->annotations);
+ }
+
+
+} \ No newline at end of file
diff --git a/utility/simplepieapifactory.php b/utility/simplepieapifactory.php
index 70e833a4e..5014838d1 100644
--- a/utility/simplepieapifactory.php
+++ b/utility/simplepieapifactory.php
@@ -1,7 +1,7 @@
<?php
/**
- * ownCloud - App Framework
+ * ownCloud - News
*
* @author Bernhard Posselt
* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
diff --git a/utility/testutility.php b/utility/testutility.php
new file mode 100644
index 000000000..8e79147d9
--- /dev/null
+++ b/utility/testutility.php
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * ownCloud - News
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\News\Utility;
+
+/**
+ * Simple utility class for testing anything using an api
+ */
+abstract class TestUtility extends \PHPUnit_Framework_TestCase {
+
+
+ /**
+ * Boilerplate function for getting an API Mock class
+ * @param string $apiClass the class inclusive namespace of the api that we
+ * want to use
+ * @param array $constructor constructor parameters of the api class
+ */
+ protected function getAPIMock($apiClass='OCA\News\Core\API',
+ array $constructor=array('appname')){
+ $methods = get_class_methods($apiClass);
+ return $this->getMock($apiClass, $methods, $constructor);
+ }
+
+
+} \ No newline at end of file