Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2020-06-29 16:52:47 +0300
committerChristoph Wurst <christoph@winzerhof-wurst.at>2020-07-03 15:37:46 +0300
commit4a3ea04baaa67f2cbf23c3d9f776373adad375a9 (patch)
tree148892dbdefd211681d03041bdfc9a35f7e86a33 /tests
parent0825530a1b1cbb98195ec15cbbd46d121bc65373 (diff)
Callable parameter injection
This is like what we have to DI and classes, but for callables. The motivating factor is to get rid of *service locators* in the `boot` method of apps as a new pattern is about to emerge where we have lots of `query` calls on the app or server container in order to fetch some services. With this little helper it's possible to call another (public) method and magically have everything injected. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/AppFramework/Bootstrap/FunctionInjectorTest.php84
-rw-r--r--tests/lib/AppFramework/Utility/SimpleContainerTest.php9
2 files changed, 90 insertions, 3 deletions
diff --git a/tests/lib/AppFramework/Bootstrap/FunctionInjectorTest.php b/tests/lib/AppFramework/Bootstrap/FunctionInjectorTest.php
new file mode 100644
index 00000000000..cd2332b0588
--- /dev/null
+++ b/tests/lib/AppFramework/Bootstrap/FunctionInjectorTest.php
@@ -0,0 +1,84 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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 lib\AppFramework\Bootstrap;
+
+use OC\AppFramework\Bootstrap\FunctionInjector;
+use OC\AppFramework\Utility\SimpleContainer;
+use Test\TestCase;
+
+interface Foo {
+}
+
+class FunctionInjectorTest extends TestCase {
+
+ /** @var SimpleContainer */
+ private $container;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->container = new SimpleContainer();
+ }
+
+ public function testInjectFnNotRegistered(): void {
+ $this->expectException(\OCP\AppFramework\QueryException::class);
+
+ (new FunctionInjector($this->container))->injectFn(static function (Foo $p1): void {
+ });
+ }
+
+ public function testInjectFnNotRegisteredButNullable(): void {
+ (new FunctionInjector($this->container))->injectFn(static function (?Foo $p1): void {
+ });
+
+ // Nothing to assert. No errors means everything is fine.
+ $this->addToAssertionCount(1);
+ }
+
+ public function testInjectFnByType(): void {
+ $this->container->registerService(Foo::class, function () {
+ $this->addToAssertionCount(1);
+ return new class implements Foo {
+ };
+ });
+
+ (new FunctionInjector($this->container))->injectFn(static function (Foo $p1): void {
+ });
+
+ // Nothing to assert. No errors means everything is fine.
+ $this->addToAssertionCount(1);
+ }
+
+ public function testInjectFnByName(): void {
+ $this->container->registerParameter('test', 'abc');
+
+ (new FunctionInjector($this->container))->injectFn(static function ($test): void {
+ });
+
+ // Nothing to assert. No errors means everything is fine.
+ $this->addToAssertionCount(1);
+ }
+}
diff --git a/tests/lib/AppFramework/Utility/SimpleContainerTest.php b/tests/lib/AppFramework/Utility/SimpleContainerTest.php
index f8a43058305..36fa1febfcf 100644
--- a/tests/lib/AppFramework/Utility/SimpleContainerTest.php
+++ b/tests/lib/AppFramework/Utility/SimpleContainerTest.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* ownCloud - App Framework
*
@@ -67,13 +69,14 @@ class SimpleContainerTest extends \Test\TestCase {
}
+
public function testRegister() {
$this->container->registerParameter('test', 'abc');
$this->assertEquals('abc', $this->container->query('test'));
}
-
+
public function testNothingRegistered() {
$this->expectException(\OCP\AppFramework\QueryException::class);
@@ -81,7 +84,7 @@ class SimpleContainerTest extends \Test\TestCase {
}
-
+
public function testNotAClass() {
$this->expectException(\OCP\AppFramework\QueryException::class);
@@ -190,7 +193,7 @@ class SimpleContainerTest extends \Test\TestCase {
$this->assertEquals('abc', $this->container->query($query));
}
-
+
public function testConstructorComplexNoTestParameterFound() {
$this->expectException(\OCP\AppFramework\QueryException::class);