diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-03-20 01:14:47 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-03-21 10:52:20 +0300 |
commit | 21641302a97815c2779f65ffd9efc95cf314e48d (patch) | |
tree | 2a963b50b2f9220c58882192405e660b02d7d104 /tests/lib | |
parent | 7cece61ff66df60e2df258285049b6009e921197 (diff) |
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>
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php b/tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php new file mode 100644 index 00000000000..e77f2021d9e --- /dev/null +++ b/tests/lib/AppFramework/DependencyInjection/DIIntergrationTests.php @@ -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)); + } +} |