Add DI intergration tests

* Moved some interface definitions to Server.php (more to come)
* Build/Query only for existing classes in the AppContainer
* Build/Query only for classes of the App in the AppContainer
* Offload other stuff to the servercontainer

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2017-03-19 23:14:47 +01:00
parent 7cece61ff6
commit 21641302a9
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 152 additions and 9 deletions

View File

@ -45,6 +45,7 @@ use OC\AppFramework\Middleware\SessionMiddleware;
use OC\AppFramework\Utility\SimpleContainer; use OC\AppFramework\Utility\SimpleContainer;
use OC\Core\Middleware\TwoFactorMiddleware; use OC\Core\Middleware\TwoFactorMiddleware;
use OC\RichObjectStrings\Validator; use OC\RichObjectStrings\Validator;
use OC\ServerContainer;
use OCP\AppFramework\IApi; use OCP\AppFramework\IApi;
use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException; use OCP\AppFramework\QueryException;
@ -62,24 +63,25 @@ class DIContainer extends SimpleContainer implements IAppContainer {
*/ */
private $middleWares = array(); private $middleWares = array();
/** @var IServerContainer */ /** @var ServerContainer */
private $server; private $server;
/** /**
* Put your class dependencies in here * Put your class dependencies in here
* @param string $appName the name of the app * @param string $appName the name of the app
* @param array $urlParams * @param array $urlParams
* @param IServerContainer $server * @param ServerContainer $server
*/ */
public function __construct($appName, $urlParams = array(), IServerContainer $server = null){ public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
parent::__construct(); parent::__construct();
$this['AppName'] = $appName; $this['AppName'] = $appName;
$this['urlParams'] = $urlParams; $this['urlParams'] = $urlParams;
/** @var \OC\ServerContainer $server */ /** @var \OC\ServerContainer $server */
if ($server === null) { if ($server === null) {
$this->server = \OC::$server; $server = \OC::$server;
} }
$this->server = $server;
$this->server->registerAppContainer($appName, $this); $this->server->registerAppContainer($appName, $this);
// aliases // aliases
@ -570,13 +572,13 @@ class DIContainer extends SimpleContainer implements IAppContainer {
public function query($name) { public function query($name) {
$name = $this->sanitizeName($name); $name = $this->sanitizeName($name);
try { if ($this->offsetExists($name)) {
return parent::query($name); return parent::query($name);
} catch (QueryException $e) { } else {
if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
$segments = explode('\\', $name); $segments = explode('\\', $name);
if (strtolower($segments[1]) === strtolower($this['AppName'])) { if (strtolower($segments[1]) === strtolower($this['AppName'])) {
throw new QueryException(); return parent::query($name);
} }
} }
} }

View File

@ -93,6 +93,7 @@ use OC\Tagging\TagMapper;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\Authentication\LoginCredentials\IStore; use OCP\Authentication\LoginCredentials\IStore;
use OCP\IDBConnection;
use OCP\IL10N; use OCP\IL10N;
use OCP\IServerContainer; use OCP\IServerContainer;
use OCP\RichObjectStrings\IValidator; use OCP\RichObjectStrings\IValidator;
@ -485,7 +486,7 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerService('CredentialsManager', function (Server $c) { $this->registerService('CredentialsManager', function (Server $c) {
return new CredentialsManager($c->getCrypto(), $c->getDatabaseConnection()); return new CredentialsManager($c->getCrypto(), $c->getDatabaseConnection());
}); });
$this->registerService('DatabaseConnection', function (Server $c) { $this->registerService(IDBConnection::class, function (Server $c) {
$systemConfig = $c->getSystemConfig(); $systemConfig = $c->getSystemConfig();
$factory = new \OC\DB\ConnectionFactory($systemConfig); $factory = new \OC\DB\ConnectionFactory($systemConfig);
$type = $systemConfig->getValue('dbtype', 'sqlite'); $type = $systemConfig->getValue('dbtype', 'sqlite');
@ -497,6 +498,7 @@ class Server extends ServerContainer implements IServerContainer {
$connection->getConfiguration()->setSQLLogger($c->getQueryLogger()); $connection->getConfiguration()->setSQLLogger($c->getQueryLogger());
return $connection; return $connection;
}); });
$this->registerAlias('DatabaseConnection', IDBConnection::class);
$this->registerService('HTTPHelper', function (Server $c) { $this->registerService('HTTPHelper', function (Server $c) {
$config = $c->getConfig(); $config = $c->getConfig();
return new HTTPHelper( return new HTTPHelper(
@ -740,9 +742,12 @@ class Server extends ServerContainer implements IServerContainer {
} }
return new \OC_Defaults(); return new \OC_Defaults();
}); });
$this->registerService('EventDispatcher', function () { $this->registerService(EventDispatcher::class, function () {
return new EventDispatcher(); return new EventDispatcher();
}); });
$this->registerAlias('EventDispatcher', EventDispatcher::class);
$this->registerAlias(EventDispatcherInterface::class, EventDispatcher::class);
$this->registerService('CryptoWrapper', function (Server $c) { $this->registerService('CryptoWrapper', function (Server $c) {
// FIXME: Instantiiated here due to cyclic dependency // FIXME: Instantiiated here due to cyclic dependency
$request = new Request( $request = new Request(

View File

@ -0,0 +1,136 @@
<?php
/**
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Test\AppFramework\DependencyInjection;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Utility\SimpleContainer;
use OC\ServerContainer;
use Test\TestCase;
interface Interface1 {}
class ClassA1 implements Interface1 {}
class ClassA2 implements Interface1 {}
class ClassB {
/** @var Interface1 */
public $interface1;
/**
* ClassB constructor.
*
* @param Interface1 $interface1
*/
public function __construct(Interface1 $interface1) {
$this->interface1 = $interface1;
}
}
class DIIntergrationTests extends TestCase {
/** @var DIContainer */
private $container;
/** @var ServerContainer */
private $server;
public function setUp() {
parent::setUp();
$this->server = new ServerContainer();
$this->container = new DIContainer('App1', [], $this->server);
}
public function testInjectFromServer() {
$this->server->registerService(Interface1::class, function () {
return new ClassA1();
});
$this->server->registerService(ClassB::class, function (SimpleContainer $c) {
return new ClassB(
$c->query(Interface1::class)
);
});
/** @var ClassB $res */
$res = $this->container->query(ClassB::class);
$this->assertSame(ClassA1::class, get_class($res->interface1));
}
public function testInjectDepFromServer() {
$this->server->registerService(Interface1::class, function () {
return new ClassA1();
});
$this->container->registerService(ClassB::class, function (SimpleContainer $c) {
return new ClassB(
$c->query(Interface1::class)
);
});
/** @var ClassB $res */
$res = $this->container->query(ClassB::class);
$this->assertSame(ClassA1::class, get_class($res->interface1));
}
public function testOverwriteDepFromServer() {
$this->server->registerService(Interface1::class, function () {
return new ClassA1();
});
$this->container->registerService(Interface1::class, function () {
return new ClassA2();
});
$this->container->registerService(ClassB::class, function (SimpleContainer $c) {
return new ClassB(
$c->query(Interface1::class)
);
});
/** @var ClassB $res */
$res = $this->container->query(ClassB::class);
$this->assertSame(ClassA2::class, get_class($res->interface1));
}
public function testIgnoreOverwriteInServerClass() {
$this->server->registerService(Interface1::class, function () {
return new ClassA1();
});
$this->container->registerService(Interface1::class, function () {
return new ClassA2();
});
$this->server->registerService(ClassB::class, function (SimpleContainer $c) {
return new ClassB(
$c->query(Interface1::class)
);
});
/** @var ClassB $res */
$res = $this->container->query(ClassB::class);
$this->assertSame(ClassA1::class, get_class($res->interface1));
}
}