From 517e4ca5435106ab5304849248cbea4e9dffd4b0 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 19 Apr 2014 13:20:54 +0200 Subject: split up api class for easier testing and clearer code --- utility/config.php | 12 +++++----- utility/controllertestutility.php | 2 +- utility/mappertestutility.php | 23 ++++++++++---------- utility/testutility.php | 46 --------------------------------------- 4 files changed, 19 insertions(+), 64 deletions(-) delete mode 100644 utility/testutility.php (limited to 'utility') diff --git a/utility/config.php b/utility/config.php index d415595b7..b2416ce9a 100644 --- a/utility/config.php +++ b/utility/config.php @@ -25,7 +25,7 @@ namespace OCA\News\Utility; -use \OCA\News\Core\API; +use \OCA\News\Core\Logger; class Config { @@ -43,17 +43,17 @@ class Config { private $proxyPort; private $proxyUser; private $proxyPassword; - private $api; + private $logger; - public function __construct($fileSystem, API $api) { + public function __construct($fileSystem, Logger $logger) { $this->fileSystem = $fileSystem; $this->autoPurgeMinimumInterval = 60; $this->autoPurgeCount = 200; $this->simplePieCacheDuration = 30*60; $this->feedFetcherTimeout = 60; $this->useCronUpdates = true; - $this->api = $api; + $this->logger = $logger; $this->proxyHost = ''; $this->proxyPort = 8080; $this->proxyUser = ''; @@ -162,7 +162,7 @@ class Config { $configValues = parse_ini_string($content); if($configValues === false || count($configValues) === 0) { - $this->api->log('Configuration invalid. Ignoring values.' , 'warn'); + $this->logger->log('Configuration invalid. Ignoring values.' , 'warn'); } else { foreach($configValues as $key => $value) { @@ -171,7 +171,7 @@ class Config { settype($value, $type); $this->$key = $value; } else { - $this->api->log('Configuration value "' . $key . + $this->logger->log('Configuration value "' . $key . '" does not exist. Ignored value.' , 'warn'); } } diff --git a/utility/controllertestutility.php b/utility/controllertestutility.php index d3cc435e3..ab534bfc6 100644 --- a/utility/controllertestutility.php +++ b/utility/controllertestutility.php @@ -31,7 +31,7 @@ use OCP\AppFramework\Http\Response; /** * Simple utility class for testing controllers */ -abstract class ControllerTestUtility extends TestUtility { +abstract class ControllerTestUtility extends \PHPUnit_Framework_TestCase { /** diff --git a/utility/mappertestutility.php b/utility/mappertestutility.php index d78e717aa..27b513f15 100644 --- a/utility/mappertestutility.php +++ b/utility/mappertestutility.php @@ -24,16 +24,16 @@ namespace OCA\News\Utility; -use OCA\News\Core\Api; +use OCA\News\Core\Db; /** * Simple utility class for testing mappers */ -abstract class MapperTestUtility extends TestUtility { +abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { - protected $api; + protected $db; private $query; private $pdoResult; private $queryAt; @@ -44,12 +44,13 @@ abstract class MapperTestUtility extends TestUtility { /** * 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 + * db. After this the db can be accessed by using $this->db */ protected function beforeEach(){ - $this->api = $this->getMock('\OCA\News\Core\API', - array('prepareQuery', 'getInsertId'), - array('a')); + $this->db = $this->getMockBuilder( + '\OCA\News\Core\Db') + ->disableOriginalConstructor() + ->getMock(); $this->query = $this->getMock('Query', array('execute', 'bindValue')); $this->pdoResult = $this->getMock('Result', array('fetchRow')); @@ -127,24 +128,24 @@ abstract class MapperTestUtility extends TestUtility { $this->queryAt++; if($limit === null && $offset === null) { - $this->api->expects($this->at($this->prepareAt)) + $this->db->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)) + $this->db->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)) + $this->db->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)) + $this->db->expects($this->at($this->prepareAt)) ->method('prepareQuery') ->with($this->equalTo($sql), $this->equalTo($limit), diff --git a/utility/testutility.php b/utility/testutility.php deleted file mode 100644 index 8e79147d9..000000000 --- a/utility/testutility.php +++ /dev/null @@ -1,46 +0,0 @@ -. - * - */ - - -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 -- cgit v1.2.3