From 21641302a97815c2779f65ffd9efc95cf314e48d Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 19 Mar 2017 23:14:47 +0100 Subject: [PATCH] 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 --- .../DependencyInjection/DIContainer.php | 16 ++- lib/private/Server.php | 9 +- .../DIIntergrationTests.php | 136 ++++++++++++++++++ 3 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 939e45dcd3..3bfc64e85b 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -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); } } } diff --git a/lib/private/Server.php b/lib/private/Server.php index 57cc93f447..8aaecf1d25 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -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( diff --git a/tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php b/tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php new file mode 100644 index 0000000000..e77f2021d9 --- /dev/null +++ b/tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php @@ -0,0 +1,136 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +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)); + } +}