diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-04-19 13:20:54 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-04-19 13:21:11 +0200 |
commit | 517e4ca5435106ab5304849248cbea4e9dffd4b0 (patch) | |
tree | 0f00076f012791b39c710994f695abf723546062 /tests | |
parent | bd35b98d2c130f058b182f726636ee971625823b (diff) |
split up api class for easier testing and clearer code
Diffstat (limited to 'tests')
28 files changed, 277 insertions, 556 deletions
diff --git a/tests/integration/db/ItemMapperIntegrationTest.php b/tests/integration/db/ItemMapperIntegrationTest.php deleted file mode 100644 index 186dca14f..000000000 --- a/tests/integration/db/ItemMapperIntegrationTest.php +++ /dev/null @@ -1,81 +0,0 @@ -<?php - -/** -* ownCloud - News -* -* @author Alessandro Cosentino -* @author Bernhard Posselt -* @copyright 2012 Alessandro Cosentino cosenal@gmail.com -* @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\Db; - -use \OCA\News\Utility\MapperTestUtility; - -require_once(__DIR__ . "/../../classloader.php"); - - -class InMemoryDatabase { - - private $db; - - public function __construct(){ - $this->db = new \PDO('sqlite::memory:'); - } - - - public function prepare($sql){ - $count = 1; - $sql = str_replace('*PREFIX*', 'oc', $sql, $count); - var_dump($this->db->prepare($sql)); - return $this->db->prepare($sql); - } - - -} - - - -class ItemMapperIntegrationTest extends MapperTestUtility { - - protected $api; - - private $mapper; - private $db; - - protected function setUp(){ - $db = new InMemoryDatabase(); - $prepare = function($sql) use ($db){ - return $db->prepare($sql); - }; - - $this->api = $this->getMock('OCA\News\Core\API', - array('prepareQuery', 'getInsertId'), array('news')); - $this->api->expects($this->any()) - ->method('prepareQuery') - ->will($this->returnCallback($prepare)); - $this->mapper = new ItemMapper($this->api); - } - - - public function testFind(){ - //$this->mapper->find(3, 'john'); - } - - -}
\ No newline at end of file diff --git a/tests/unit/articleenhancer/EnhancerTest.php b/tests/unit/articleenhancer/EnhancerTest.php index 7f782cf55..663714590 100644 --- a/tests/unit/articleenhancer/EnhancerTest.php +++ b/tests/unit/articleenhancer/EnhancerTest.php @@ -30,7 +30,7 @@ use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); -class EnhancerTest extends \OCA\News\Utility\TestUtility { +class EnhancerTest extends \PHPUnit_Framework_TestCase { private $enhancer; private $articleEnhancer; diff --git a/tests/unit/articleenhancer/RegexArticleEnhancerTest.php b/tests/unit/articleenhancer/RegexArticleEnhancerTest.php index a43f92b1c..38c570cfb 100644 --- a/tests/unit/articleenhancer/RegexArticleEnhancerTest.php +++ b/tests/unit/articleenhancer/RegexArticleEnhancerTest.php @@ -30,7 +30,7 @@ use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); -class RegexArticleEnhancerTest extends \OCA\News\Utility\TestUtility { +class RegexArticleEnhancerTest extends \PHPUnit_Framework_TestCase { public function testRegexEnhancer() { diff --git a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php index dbd752518..dd84c31fd 100644 --- a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php +++ b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php @@ -30,7 +30,7 @@ use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); -class XPathArticleEnhancerTest extends \OCA\News\Utility\TestUtility { +class XPathArticleEnhancerTest extends \PHPUnit_Framework_TestCase { private $testEnhancer; private $fileFactory; diff --git a/tests/unit/businesslayer/BusinessLayerTest.php b/tests/unit/businesslayer/BusinessLayerTest.php index 7f3677d18..54ca11cba 100644 --- a/tests/unit/businesslayer/BusinessLayerTest.php +++ b/tests/unit/businesslayer/BusinessLayerTest.php @@ -39,14 +39,12 @@ class TestBusinessLayer extends BusinessLayer { } } -class BusinessLayerTest extends \OCA\News\Utility\TestUtility { +class BusinessLayerTest extends \PHPUnit_Framework_TestCase { - protected $api; protected $mapper; protected $newsBusinessLayer; protected function setUp(){ - $this->api = $this->getAPIMock(); $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') ->disableOriginalConstructor() ->getMock(); diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php index 19ff7da7f..f856f2ca6 100644 --- a/tests/unit/businesslayer/FeedBusinessLayerTest.php +++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php @@ -34,7 +34,7 @@ use \OCA\News\Db\Item; use \OCA\News\Fetcher\Fetcher; use \OCA\News\Fetcher\FetcherException; -class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { +class FeedBusinessLayerTest extends \PHPUnit_Framework_TestCase { private $feedMapper; private $feedBusinessLayer; @@ -48,9 +48,15 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { private $autoPurgeMinimumInterval; private $enhancer; private $purifier; + private $l10n; + private $logger; protected function setUp(){ - $this->api = $this->getAPIMock(); + $this->logger = $this->getMockBuilder( + '\OCA\News\Core\Logger') + ->disableOriginalConstructor() + ->getMock(); + $this->l10n = $this->getMock('L10N', array('t')); $this->time = 222; $this->autoPurgeMinimumInterval = 10; $timeFactory = $this->getMock('TimeFactory', array('getTime')); @@ -79,7 +85,7 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { ->will($this->returnValue($this->autoPurgeMinimumInterval)); $this->feedBusinessLayer = new FeedBusinessLayer($this->feedMapper, - $this->fetcher, $this->itemMapper, $this->api, + $this->fetcher, $this->itemMapper, $this->logger, $this->l10n, $timeFactory, $config, $this->enhancer, $this->purifier); $this->user = 'jack'; @@ -101,12 +107,8 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { public function testCreateDoesNotFindFeed(){ $ex = new FetcherException('hi'); $url = 'test'; - $trans = $this->getMock('Trans', array('t')); - $trans->expects($this->once()) + $this->l10n->expects($this->once()) ->method('t'); - $this->api->expects($this->once()) - ->method('getTrans') - ->will($this->returnValue($trans)); $this->fetcher->expects($this->once()) ->method('fetch') ->with($this->equalTo($url)) @@ -361,7 +363,7 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { $this->fetcher->expects($this->once()) ->method('fetch') ->will($this->throwException($ex)); - $this->api->expects($this->any()) + $this->logger->expects($this->any()) ->method('log'); $this->feedMapper->expects($this->at(1)) @@ -601,17 +603,13 @@ class FeedBusinessLayerTest extends \OCA\News\Utility\TestUtility { $insertFeed->setPreventUpdate(true); $insertFeed->setFolderId(0); - $trans = $this->getMock('trans', array('t')); - $trans->expects($this->once()) + $this->l10n->expects($this->once()) ->method('t') ->will($this->returnValue('Articles without feed')); $this->feedMapper->expects($this->once()) ->method('findAllFromUser') ->with($this->equalTo($this->user)) ->will($this->returnValue($feeds)); - $this->api->expects($this->once()) - ->method('getTrans') - ->will($this->returnValue($trans)); $this->feedMapper->expects($this->once()) ->method('insert') ->with($this->equalTo($insertFeed)) diff --git a/tests/unit/businesslayer/FolderBusinessLayerTest.php b/tests/unit/businesslayer/FolderBusinessLayerTest.php index 17eb1b8ae..22b9e53d0 100644 --- a/tests/unit/businesslayer/FolderBusinessLayerTest.php +++ b/tests/unit/businesslayer/FolderBusinessLayerTest.php @@ -31,16 +31,17 @@ require_once(__DIR__ . "/../../classloader.php"); use \OCA\News\Db\Folder; -class FolderBusinessLayerTest extends \OCA\News\Utility\TestUtility { +class FolderBusinessLayerTest extends \PHPUnit_Framework_TestCase { private $folderMapper; private $folderBusinessLayer; private $time; private $user; private $autoPurgeMinimumInterval; + private $l10n; protected function setUp(){ - $this->api = $this->getAPIMock(); + $this->l10n = $this->getMock('L10N', array('t')); $this->time = 222; $timeFactory = $this->getMock('TimeFactory', array('getTime')); $timeFactory->expects($this->any()) @@ -59,7 +60,7 @@ class FolderBusinessLayerTest extends \OCA\News\Utility\TestUtility { ->method('getAutoPurgeMinimumInterval') ->will($this->returnValue($this->autoPurgeMinimumInterval)); $this->folderBusinessLayer = new FolderBusinessLayer( - $this->folderMapper, $this->api, $timeFactory, + $this->folderMapper, $this->l10n, $timeFactory, $config); $this->user = 'hi'; } @@ -103,12 +104,8 @@ class FolderBusinessLayerTest extends \OCA\News\Utility\TestUtility { array('id' => 1) ); - $trans = $this->getMock('Trans', array('t')); - $trans->expects($this->once()) + $this->l10n->expects($this->once()) ->method('t'); - $this->api->expects($this->once()) - ->method('getTrans') - ->will($this->returnValue($trans)); $this->folderMapper->expects($this->once()) ->method('findByName') ->with($this->equalTo($folderName)) @@ -179,12 +176,8 @@ class FolderBusinessLayerTest extends \OCA\News\Utility\TestUtility { array('id' => 1) ); - $trans = $this->getMock('Trans', array('t')); - $trans->expects($this->once()) + $this->l10n->expects($this->once()) ->method('t'); - $this->api->expects($this->once()) - ->method('getTrans') - ->will($this->returnValue($trans)); $this->folderMapper->expects($this->once()) ->method('findByName') ->with($this->equalTo($folderName)) diff --git a/tests/unit/businesslayer/ItemBusinessLayerTest.php b/tests/unit/businesslayer/ItemBusinessLayerTest.php index a3e7f7a14..f0f9d1906 100644 --- a/tests/unit/businesslayer/ItemBusinessLayerTest.php +++ b/tests/unit/businesslayer/ItemBusinessLayerTest.php @@ -33,9 +33,8 @@ use \OCA\News\Db\StatusFlag; use \OCA\News\Db\FeedType; -class ItemBusinessLayerTest extends \OCA\News\Utility\TestUtility { +class ItemBusinessLayerTest extends \PHPUnit_Framework_TestCase { - private $api; private $mapper; private $itemBusinessLayer; private $user; @@ -51,7 +50,6 @@ class ItemBusinessLayerTest extends \OCA\News\Utility\TestUtility { $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); - $this->api = $this->getAPIMock(); $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') ->disableOriginalConstructor() ->getMock(); diff --git a/tests/unit/businesslayer/StatusFlagTest.php b/tests/unit/businesslayer/StatusFlagTest.php index d503b6630..d96f40a6b 100644 --- a/tests/unit/businesslayer/StatusFlagTest.php +++ b/tests/unit/businesslayer/StatusFlagTest.php @@ -29,7 +29,7 @@ namespace OCA\News\Db; require_once(__DIR__ . "/../../classloader.php"); -class StatusFlagTest extends \OCA\News\Utility\TestUtility { +class StatusFlagTest extends \PHPUnit_Framework_TestCase { private $statusFlag; diff --git a/tests/unit/controller/ApiControllerTest.php b/tests/unit/controller/ApiControllerTest.php index 722830a23..6aa6ee4ea 100644 --- a/tests/unit/controller/ApiControllerTest.php +++ b/tests/unit/controller/ApiControllerTest.php @@ -36,14 +36,16 @@ require_once(__DIR__ . "/../../classloader.php"); class ApiControllerTest extends ControllerTestUtility { - private $api; + private $settings; private $request; private $newsAPI; private $updater; + private $appName; protected function setUp() { - $this->api = $this->getMockBuilder( - '\OCA\News\Core\API') + $this->appName = 'news'; + $this->settings = $this->getMockBuilder( + '\OCA\News\Core\Settings') ->disableOriginalConstructor() ->getMock(); $this->request = $this->getMockBuilder( @@ -54,7 +56,8 @@ class ApiControllerTest extends ControllerTestUtility { '\OCA\News\Utility\Updater') ->disableOriginalConstructor() ->getMock(); - $this->newsAPI = new ApiController($this->api, $this->request, $this->updater); + $this->newsAPI = new ApiController($this->appName, $this->request, + $this->updater, $this->settings); } @@ -78,7 +81,7 @@ class ApiControllerTest extends ControllerTestUtility { } public function testGetVersion(){ - $this->api->expects($this->once()) + $this->settings->expects($this->once()) ->method('getAppValue') ->with($this->equalTo('installed_version')) ->will($this->returnValue('1.0')); @@ -115,7 +118,8 @@ class ApiControllerTest extends ControllerTestUtility { public function testCors() { $this->request = $this->getRequest(array('server' => array())); - $this->newsAPI = new ApiController($this->api, $this->request, $this->updater); + $this->newsAPI = new ApiController($this->appName, $this->request, + $this->updater, $this->settings); $response = $this->newsAPI->cors(); $headers = $response->getHeaders(); @@ -130,7 +134,8 @@ class ApiControllerTest extends ControllerTestUtility { public function testCorsUsesOriginIfGiven() { $this->request = $this->getRequest(array('server' => array('HTTP_ORIGIN' => 'test'))); - $this->newsAPI = new ApiController($this->api, $this->request, $this->updater); + $this->newsAPI = new ApiController($this->appName, $this->request, + $this->updater, $this->settings); $response = $this->newsAPI->cors(); $headers = $response->getHeaders(); diff --git a/tests/unit/controller/ExportControllerTest.php b/tests/unit/controller/ExportControllerTest.php index b79574ff2..bc3baf4cf 100644 --- a/tests/unit/controller/ExportControllerTest.php +++ b/tests/unit/controller/ExportControllerTest.php @@ -40,7 +40,7 @@ require_once(__DIR__ . "/../../classloader.php"); class ExportControllerTest extends ControllerTestUtility { - private $api; + private $appName; private $request; private $controller; private $user; @@ -53,7 +53,8 @@ class ExportControllerTest extends ControllerTestUtility { * Gets run before each test */ public function setUp(){ - $this->api = $this->getAPIMock(); + $this->appName = 'news'; + $this->user = 'john'; $this->itemBusinessLayer = $this->getMockBuilder('\OCA\News\BusinessLayer\ItemBusinessLayer') ->disableOriginalConstructor() ->getMock(); @@ -65,10 +66,9 @@ class ExportControllerTest extends ControllerTestUtility { ->getMock(); $this->request = $this->getRequest(); $this->opmlExporter = new OPMLExporter(); - $this->controller = new ExportController($this->api, $this->request, + $this->controller = new ExportController($this->appName, $this->request, $this->feedBusinessLayer, $this->folderBusinessLayer, - $this->itemBusinessLayer, $this->opmlExporter); - $this->user = 'john'; + $this->itemBusinessLayer, $this->opmlExporter, $this->user); } @@ -94,9 +94,6 @@ class ExportControllerTest extends ControllerTestUtility { " <body/>\n" . "</opml>\n"; - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('findAll') ->with($this->equalTo($this->user)) @@ -130,9 +127,6 @@ class ExportControllerTest extends ControllerTestUtility { $item1, $item2 ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('findAll') ->with($this->equalTo($this->user)) diff --git a/tests/unit/controller/FeedApiControllerTest.php b/tests/unit/controller/FeedApiControllerTest.php index 8bb8447bd..752eb575b 100644 --- a/tests/unit/controller/FeedApiControllerTest.php +++ b/tests/unit/controller/FeedApiControllerTest.php @@ -45,16 +45,19 @@ class FeedApiControllerTest extends ControllerTestUtility { private $feedBusinessLayer; private $itemBusinessLayer; private $feedAPI; - private $api; + private $appName; private $user; private $request; private $msg; + private $logger; protected function setUp() { - $this->api = $this->getMockBuilder( - '\OCA\News\Core\API') + $this->user = 'tom'; + $this->logger = $this->getMockBuilder( + '\OCA\News\Core\Logger') ->disableOriginalConstructor() ->getMock(); + $this->appName = 'news'; $this->request = $this->getMockBuilder( '\OCP\IRequest') ->disableOriginalConstructor() @@ -72,13 +75,14 @@ class FeedApiControllerTest extends ControllerTestUtility { ->disableOriginalConstructor() ->getMock(); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $this->request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->user = 'tom'; $this->msg = 'hohoho'; } @@ -133,9 +137,6 @@ class FeedApiControllerTest extends ControllerTestUtility { $starredCount = 3; $newestItemId = 2; - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->itemBusinessLayer->expects($this->once()) ->method('starredCount') ->with($this->equalTo($this->user)) @@ -165,9 +166,6 @@ class FeedApiControllerTest extends ControllerTestUtility { ); $starredCount = 3; - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->itemBusinessLayer->expects($this->once()) ->method('starredCount') ->with($this->equalTo($this->user)) @@ -195,16 +193,15 @@ class FeedApiControllerTest extends ControllerTestUtility { 'feedId' => 2 ))); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('delete') ->with( @@ -223,16 +220,15 @@ class FeedApiControllerTest extends ControllerTestUtility { 'feedId' => 2 ))); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('delete') ->will($this->throwException(new BusinessLayerException($this->msg))); @@ -254,16 +250,15 @@ class FeedApiControllerTest extends ControllerTestUtility { 'folderId' => 3 ))); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('purgeDeleted') ->with($this->equalTo($this->user), $this->equalTo(false)); @@ -298,16 +293,15 @@ class FeedApiControllerTest extends ControllerTestUtility { 'folderId' => 3 ))); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('purgeDeleted') ->with($this->equalTo($this->user), $this->equalTo(false)); @@ -334,9 +328,6 @@ class FeedApiControllerTest extends ControllerTestUtility { public function testCreateExists() { - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('purgeDeleted') ->with($this->equalTo($this->user), $this->equalTo(false)); @@ -353,9 +344,6 @@ class FeedApiControllerTest extends ControllerTestUtility { public function testCreateError() { - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('create') ->will($this->throwException(new BusinessLayerException($this->msg))); @@ -378,16 +366,15 @@ class FeedApiControllerTest extends ControllerTestUtility { ) )); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->itemBusinessLayer->expects($this->once()) ->method('readFeed') ->with( @@ -412,16 +399,15 @@ class FeedApiControllerTest extends ControllerTestUtility { ) )); $this->feedAPI = new FeedApiController( - $this->api, + $this->appName, $request, $this->folderBusinessLayer, $this->feedBusinessLayer, - $this->itemBusinessLayer + $this->itemBusinessLayer, + $this->logger, + $this->user ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('move') ->with( @@ -449,16 +435,15 @@ class FeedApiCon |