summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-11-03 00:23:05 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-11-30 10:55:05 +0100
commit03ee3b9aec75a1e53e44333744cbbea331c65067 (patch)
treece7afd0d28b0cb70cafe17c27a4a3b7994c2cb15 /tests/lib
parent9c1dbaf0ad73bc84e41db964b319d7b2842ac7ae (diff)
downloadnextcloud-server-03ee3b9aec75a1e53e44333744cbbea331c65067.tar.gz
nextcloud-server-03ee3b9aec75a1e53e44333744cbbea331c65067.zip
A TestCase which is not annotated to be in group DB will not allow access to the database connection.
This is necessary to categorize unit test and avoid duplicate test case execution - it also allows us to closely review unit test implementations to identify unnecessary db calls.
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/testcase.php35
1 files changed, 32 insertions, 3 deletions
diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php
index 8cfa2a72598..f6a4febefc7 100644
--- a/tests/lib/testcase.php
+++ b/tests/lib/testcase.php
@@ -25,14 +25,16 @@ namespace Test;
use OC\Command\QueueBus;
use OC\Files\Filesystem;
use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
abstract class TestCase extends \PHPUnit_Framework_TestCase {
- /**
- * @var \OC\Command\QueueBus
- */
+ /** @var \OC\Command\QueueBus */
private $commandBus;
+ /** @var IDBConnection */
+ static private $realDatabase;
+
protected function getTestTraits() {
$traits = [];
$class = $this;
@@ -49,6 +51,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
}
protected function setUp() {
+ // detect database access
+ if (!$this->IsDatabaseAccessAllowed()) {
+ if (is_null(self::$realDatabase)) {
+ self::$realDatabase = \OC::$server->getDatabaseConnection();
+ }
+ \OC::$server->registerService('DatabaseConnection', function () {
+ $this->fail('Your test case is not allowed to access the database.');
+ });
+ }
+
// overwrite the command bus with one we can run ourselves
$this->commandBus = new QueueBus();
\OC::$server->registerService('AsyncCommandBus', function () {
@@ -65,6 +77,14 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
}
protected function tearDown() {
+ // restore database connection
+ if (!$this->IsDatabaseAccessAllowed()) {
+ \OC::$server->registerService('DatabaseConnection', function () {
+ return self::$realDatabase;
+ });
+ }
+
+ // further cleanup
$hookExceptions = \OC_Hook::$thrownExceptions;
\OC_Hook::$thrownExceptions = [];
\OC::$server->getLockingProvider()->releaseAll();
@@ -316,4 +336,13 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
return true;
}
}
+
+ private function IsDatabaseAccessAllowed() {
+ $annotations = $this->getAnnotations();
+ if (isset($annotations['class']['group']) && in_array('DB', $annotations['class']['group'])) {
+ return true;
+ }
+
+ return false;
+ }
}