Browse Source

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>
tags/v12.0.0beta1
Roeland Jago Douma 7 years ago
parent
commit
21641302a9
No account linked to committer's email address

+ 9
- 7
lib/private/AppFramework/DependencyInjection/DIContainer.php View File

@@ -45,6 +45,7 @@ use OC\AppFramework\Middleware\SessionMiddleware;
use OC\AppFramework\Utility\SimpleContainer;
use OC\Core\Middleware\TwoFactorMiddleware;
use OC\RichObjectStrings\Validator;
use OC\ServerContainer;
use OCP\AppFramework\IApi;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
@@ -62,24 +63,25 @@ class DIContainer extends SimpleContainer implements IAppContainer {
*/
private $middleWares = array();

/** @var IServerContainer */
/** @var ServerContainer */
private $server;

/**
* Put your class dependencies in here
* @param string $appName the name of the app
* @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();
$this['AppName'] = $appName;
$this['urlParams'] = $urlParams;

/** @var \OC\ServerContainer $server */
if ($server === null) {
$this->server = \OC::$server;
$server = \OC::$server;
}
$this->server = $server;
$this->server->registerAppContainer($appName, $this);

// aliases
@@ -570,13 +572,13 @@ class DIContainer extends SimpleContainer implements IAppContainer {
public function query($name) {
$name = $this->sanitizeName($name);

try {
if ($this->offsetExists($name)) {
return parent::query($name);
} catch (QueryException $e) {
} else {
if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
$segments = explode('\\', $name);
if (strtolower($segments[1]) === strtolower($this['AppName'])) {
throw new QueryException();
return parent::query($name);
}
}
}

+ 7
- 2
lib/private/Server.php View File

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

$this->registerService('CryptoWrapper', function (Server $c) {
// FIXME: Instantiiated here due to cyclic dependency
$request = new Request(

+ 136
- 0
tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php 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));
}
}

Loading…
Cancel
Save