diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/appframework/db/mappertest.php | 14 | ||||
-rw-r--r-- | tests/lib/appframework/db/mappertestutility.php | 83 | ||||
-rw-r--r-- | tests/lib/backgroundjob/dummyjoblist.php | 7 | ||||
-rw-r--r-- | tests/lib/command/asyncbus.php | 179 | ||||
-rw-r--r-- | tests/lib/connector/sabre/objecttree.php | 1 | ||||
-rw-r--r-- | tests/lib/files/objectstore/swift.php | 2 | ||||
-rw-r--r-- | tests/lib/tags.php | 12 |
7 files changed, 247 insertions, 51 deletions
diff --git a/tests/lib/appframework/db/mappertest.php b/tests/lib/appframework/db/mappertest.php index 9cbc01883e3..8e585c479bb 100644 --- a/tests/lib/appframework/db/mappertest.php +++ b/tests/lib/appframework/db/mappertest.php @@ -24,7 +24,7 @@ namespace OCP\AppFramework\Db; -use \OCP\IDb; +use \OCP\IDBConnection; use Test\AppFramework\Db\MapperTestUtility; /** @@ -42,7 +42,7 @@ class Example extends Entity { class ExampleMapper extends Mapper { - public function __construct(IDb $db){ parent::__construct($db, 'table'); } + public function __construct(IDBConnection $db){ parent::__construct($db, 'table'); } public function find($table, $id){ return $this->findOneQuery($table, $id); } public function findOneEntity($table, $id){ return $this->findEntity($table, $id); } public function findAllEntities($table){ return $this->findEntities($table); } @@ -137,7 +137,7 @@ class MapperTest extends MapperTestUtility { $sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?'; $params = array(2); - $this->setMapperResult($sql, $params); + $this->setMapperResult($sql, $params, [], null, null, true); $entity = new Example(); $entity->setId($params[0]); @@ -147,7 +147,7 @@ class MapperTest extends MapperTestUtility { public function testCreate(){ $this->db->expects($this->once()) - ->method('getInsertId') + ->method('lastInsertId') ->with($this->equalTo('*PREFIX*table')) ->will($this->returnValue(3)); $this->mapper = new ExampleMapper($this->db); @@ -159,7 +159,7 @@ class MapperTest extends MapperTestUtility { $entity->setPreName($params[0]); $entity->setEmail($params[1]); - $this->setMapperResult($sql, $params); + $this->setMapperResult($sql, $params, [], null, null, true); $this->mapper->insert($entity); } @@ -167,7 +167,7 @@ class MapperTest extends MapperTestUtility { public function testCreateShouldReturnItemWithCorrectInsertId(){ $this->db->expects($this->once()) - ->method('getInsertId') + ->method('lastInsertId') ->with($this->equalTo('*PREFIX*table')) ->will($this->returnValue(3)); $this->mapper = new ExampleMapper($this->db); @@ -200,7 +200,7 @@ class MapperTest extends MapperTestUtility { $entity->setEmail($params[1]); $entity->setId($params[2]); - $this->setMapperResult($sql, $params); + $this->setMapperResult($sql, $params, [], null, null, true); $this->mapper->update($entity); } diff --git a/tests/lib/appframework/db/mappertestutility.php b/tests/lib/appframework/db/mappertestutility.php index 0053b2c682d..c87ad528c03 100644 --- a/tests/lib/appframework/db/mappertestutility.php +++ b/tests/lib/appframework/db/mappertestutility.php @@ -31,7 +31,6 @@ namespace Test\AppFramework\Db; abstract class MapperTestUtility extends \Test\TestCase { protected $db; private $query; - private $pdoResult; private $queryAt; private $prepareAt; private $fetchAt; @@ -46,19 +45,19 @@ abstract class MapperTestUtility extends \Test\TestCase { parent::setUp(); $this->db = $this->getMockBuilder( - '\OCP\IDb') + '\OCP\IDBConnection') ->disableOriginalConstructor() ->getMock(); - $this->query = $this->getMock('Query', array('execute', 'bindValue')); - $this->pdoResult = $this->getMock('Result', array('fetch', 'closeCursor')); + $this->query = $this->getMock('\PDOStatement'); $this->queryAt = 0; $this->prepareAt = 0; - $this->iterators = array(); + $this->iterators = []; $this->fetchAt = 0; } + /** * Create mocks and set expected results for database queries * @param string $sql the sql query that you expect to receive @@ -70,13 +69,38 @@ abstract class MapperTestUtility extends \Test\TestCase { */ protected function setMapperResult($sql, $arguments=array(), $returnRows=array(), $limit=null, $offset=null, $expectClose=false){ + if($limit === null && $offset === null) { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql)) + ->will(($this->returnValue($this->query))); + } elseif($limit !== null && $offset === null) { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql), $this->equalTo($limit)) + ->will(($this->returnValue($this->query))); + } elseif($limit === null && $offset !== null) { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql), + $this->equalTo(null), + $this->equalTo($offset)) + ->will(($this->returnValue($this->query))); + } else { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql), + $this->equalTo($limit), + $this->equalTo($offset)) + ->will(($this->returnValue($this->query))); + } $this->iterators[] = new ArgumentIterator($returnRows); $iterators = $this->iterators; $fetchAt = $this->fetchAt; - $this->pdoResult->expects($this->any()) + $this->query->expects($this->any()) ->method('fetch') ->will($this->returnCallback( function() use ($iterators, $fetchAt){ @@ -87,15 +111,11 @@ abstract class MapperTestUtility extends \Test\TestCase { $fetchAt++; } + $this->queryAt++; + return $result; } )); - if ($expectClose) { - $closing = $this->once(); - } else { - $closing = $this->any(); - } - $this->pdoResult->expects($closing)->method('closeCursor'); $index = 1; foreach($arguments as $argument) { @@ -127,38 +147,23 @@ abstract class MapperTestUtility extends \Test\TestCase { $this->query->expects($this->at($this->queryAt)) ->method('execute') - ->with() - ->will($this->returnValue($this->pdoResult)); + ->will($this->returnCallback(function($sql, $p=null, $o=null, $s=null) { + + })); $this->queryAt++; - if($limit === null && $offset === null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql)) - ->will(($this->returnValue($this->query))); - } elseif($limit !== null && $offset === null) { - $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->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->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql), - $this->equalTo($limit), - $this->equalTo($offset)) - ->will(($this->returnValue($this->query))); + + + if ($expectClose) { + $closing = $this->at($this->queryAt); + } else { + $closing = $this->any(); } + $this->query->expects($closing)->method('closeCursor'); + $this->queryAt++; + $this->prepareAt++; $this->fetchAt++; - } diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php index 7801269b27e..6cc690fd553 100644 --- a/tests/lib/backgroundjob/dummyjoblist.php +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -21,13 +21,18 @@ class DummyJobList extends \OC\BackgroundJob\JobList { private $last = 0; - public function __construct(){} + public function __construct() { + } /** * @param \OC\BackgroundJob\Job|string $job * @param mixed $argument */ public function add($job, $argument = null) { + if (is_string($job)) { + /** @var \OC\BackgroundJob\Job $job */ + $job = new $job; + } $job->setArgument($argument); if (!$this->has($job, null)) { $this->jobs[] = $job; diff --git a/tests/lib/command/asyncbus.php b/tests/lib/command/asyncbus.php new file mode 100644 index 00000000000..183eaa29c37 --- /dev/null +++ b/tests/lib/command/asyncbus.php @@ -0,0 +1,179 @@ +<?php + +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Command; + +use OC\Command\FileAccess; +use OCP\Command\IBus; +use OCP\Command\ICommand; +use Test\BackgroundJob\DummyJobList; +use Test\TestCase; + +class SimpleCommand implements ICommand { + public function handle() { + AsyncBus::$lastCommand = 'SimpleCommand'; + } +} + +class StateFullCommand implements ICommand { + private $state; + + function __construct($state) { + $this->state = $state; + } + + public function handle() { + AsyncBus::$lastCommand = $this->state; + } +} + +class FilesystemCommand implements ICommand { + use FileAccess; + + public function handle() { + AsyncBus::$lastCommand = 'FileAccess'; + } +} + +function basicFunction() { + AsyncBus::$lastCommand = 'function'; +} + +// clean class to prevent phpunit putting closure in $this +class ThisClosureTest { + private function privateMethod() { + AsyncBus::$lastCommand = 'closure-this'; + } + + public function test(IBus $bus) { + $bus->push(function () { + $this->privateMethod(); + }); + } +} + +class AsyncBus extends TestCase { + /** + * Basic way to check output from a command + * + * @var string + */ + public static $lastCommand; + + /** + * @var \OCP\BackgroundJob\IJobList + */ + private $jobList; + + /** + * @var \OCP\Command\IBus + */ + private $bus; + + public static function DummyCommand() { + self::$lastCommand = 'static'; + } + + public function setUp() { + $this->jobList = new DummyJobList(); + $this->bus = new \OC\Command\AsyncBus($this->jobList); + self::$lastCommand = ''; + } + + public function testSimpleCommand() { + $command = new SimpleCommand(); + $this->bus->push($command); + $this->runJobs(); + $this->assertEquals('SimpleCommand', self::$lastCommand); + } + + public function testStateFullCommand() { + $command = new StateFullCommand('foo'); + $this->bus->push($command); + $this->runJobs(); + $this->assertEquals('foo', self::$lastCommand); + } + + public function testStaticCallable() { + $this->bus->push(['\Test\Command\AsyncBus', 'DummyCommand']); + $this->runJobs(); + $this->assertEquals('static', self::$lastCommand); + } + + public function testMemberCallable() { + $command = new StateFullCommand('bar'); + $this->bus->push([$command, 'handle']); + $this->runJobs(); + $this->assertEquals('bar', self::$lastCommand); + } + + public function testFunctionCallable() { + $this->bus->push('\Test\Command\BasicFunction'); + $this->runJobs(); + $this->assertEquals('function', self::$lastCommand); + } + + public function testClosure() { + $this->bus->push(function () { + AsyncBus::$lastCommand = 'closure'; + }); + $this->runJobs(); + $this->assertEquals('closure', self::$lastCommand); + } + + public function testClosureSelf() { + $this->bus->push(function () { + self::$lastCommand = 'closure-self'; + }); + $this->runJobs(); + $this->assertEquals('closure-self', self::$lastCommand); + } + + + public function testClosureThis() { + // clean class to prevent phpunit putting closure in $this + $test = new ThisClosureTest(); + $test->test($this->bus); + $this->runJobs(); + $this->assertEquals('closure-this', self::$lastCommand); + } + + public function testClosureBind() { + $state = 'bar'; + $this->bus->push(function () use ($state) { + self::$lastCommand = 'closure-' . $state; + }); + $this->runJobs(); + $this->assertEquals('closure-bar', self::$lastCommand); + } + + public function testFileFileAccessCommand() { + $this->bus->push(new FilesystemCommand()); + $this->assertEquals('', self::$lastCommand); + $this->runJobs(); + $this->assertEquals('FileAccess', self::$lastCommand); + } + + public function testFileFileAccessCommandSync() { + $this->bus->requireSync('\OC\Command\FileAccess'); + $this->bus->push(new FilesystemCommand()); + $this->assertEquals('FileAccess', self::$lastCommand); + self::$lastCommand = ''; + $this->runJobs(); + $this->assertEquals('', self::$lastCommand); + } + + + private function runJobs() { + $jobs = $this->jobList->getAll(); + foreach ($jobs as $job) { + $job->execute($this->jobList); + } + } +} diff --git a/tests/lib/connector/sabre/objecttree.php b/tests/lib/connector/sabre/objecttree.php index 3c972fe6f0f..0709aa89c63 100644 --- a/tests/lib/connector/sabre/objecttree.php +++ b/tests/lib/connector/sabre/objecttree.php @@ -11,7 +11,6 @@ namespace Test\OC\Connector\Sabre; use OC\Files\FileInfo; use OC\Connector\Sabre\Directory; -use PHPUnit_Framework_TestCase; class TestDoubleFileView extends \OC\Files\View { diff --git a/tests/lib/files/objectstore/swift.php b/tests/lib/files/objectstore/swift.php index 30c60598277..0aaf7d906dd 100644 --- a/tests/lib/files/objectstore/swift.php +++ b/tests/lib/files/objectstore/swift.php @@ -23,8 +23,6 @@ namespace OCA\ObjectStore\Tests\Unit; use OC\Files\ObjectStore\ObjectStoreStorage; use OC\Files\ObjectStore\Swift as ObjectStoreToTest; -use PHPUnit_Framework_TestCase; - //class Swift extends PHPUnit_Framework_TestCase { class Swift extends \Test\Files\Storage\Storage { diff --git a/tests/lib/tags.php b/tests/lib/tags.php index 547cd302d5d..1a13d64679d 100644 --- a/tests/lib/tags.php +++ b/tests/lib/tags.php @@ -49,7 +49,7 @@ class Test_Tags extends \Test\TestCase { ->will($this->returnValue($this->user)); $this->objectType = $this->getUniqueID('type_'); - $this->tagMapper = new OC\Tagging\TagMapper(\OC::$server->getDb()); + $this->tagMapper = new OC\Tagging\TagMapper(\OC::$server->getDatabaseConnection()); $this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession); } @@ -62,6 +62,16 @@ class Test_Tags extends \Test\TestCase { parent::tearDown(); } + public function testTagManagerWithoutUserReturnsNull() { + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue(null)); + $this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession); + $this->assertNull($this->tagMgr->load($this->objectType)); + } + public function testInstantiateWithDefaults() { $defaultTags = array('Friends', 'Family', 'Work', 'Other'); |