From 7cece61ff66df60e2df258285049b6009e921197 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 19 Mar 2017 22:20:56 +0100 Subject: [PATCH] Extend DI tests Signed-off-by: Roeland Jago Douma --- .../DependencyInjection/DIContainer.php | 16 ++++++++++++---- .../DependencyInjection/DIContainerTest.php | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 354b93873e..939e45dcd3 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -51,6 +51,7 @@ use OCP\AppFramework\QueryException; use OCP\Federation\ICloudIdManager; use OCP\Files\IAppData; use OCP\Files\Mount\IMountManager; +use OCP\IServerContainer; use OCP\RichObjectStrings\IValidator; use OCP\Util; @@ -61,18 +62,25 @@ class DIContainer extends SimpleContainer implements IAppContainer { */ private $middleWares = array(); + /** @var IServerContainer */ + private $server; + /** * Put your class dependencies in here * @param string $appName the name of the app + * @param array $urlParams + * @param IServerContainer $server */ - public function __construct($appName, $urlParams = array()){ + public function __construct($appName, $urlParams = array(), IServerContainer $server = null){ parent::__construct(); $this['AppName'] = $appName; $this['urlParams'] = $urlParams; /** @var \OC\ServerContainer $server */ - $server = $this->getServer(); - $server->registerAppContainer($appName, $this); + if ($server === null) { + $this->server = \OC::$server; + } + $this->server->registerAppContainer($appName, $this); // aliases $this->registerAlias('appName', 'AppName'); @@ -481,7 +489,7 @@ class DIContainer extends SimpleContainer implements IAppContainer { */ function getServer() { - return OC::$server; + return $this->server; } /** diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php index 2e450d897b..fd6fe84b87 100644 --- a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php +++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php @@ -27,23 +27,29 @@ namespace Test\AppFramework\DependencyInjection; +use OC\AppFramework\Core\API; +use OC\AppFramework\DependencyInjection\DIContainer; use \OC\AppFramework\Http\Request; +use OCP\AppFramework\QueryException; +use OCP\IConfig; +use OCP\Security\ISecureRandom; /** * @group DB */ class DIContainerTest extends \Test\TestCase { + /** @var DIContainer|\PHPUnit_Framework_MockObject_MockObject */ private $container; private $api; protected function setUp(){ parent::setUp(); - $this->container = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer') + $this->container = $this->getMockBuilder(DIContainer::class) ->setMethods(['isAdminUser']) ->setConstructorArgs(['name']) ->getMock(); - $this->api = $this->getMockBuilder('OC\AppFramework\Core\API') + $this->api = $this->getMockBuilder(API::class) ->setConstructorArgs(['hi']) ->getMock(); } @@ -80,10 +86,10 @@ class DIContainerTest extends \Test\TestCase { public function testMiddlewareDispatcherIncludesSecurityMiddleware(){ $this->container['Request'] = new Request( ['method' => 'GET'], - $this->getMockBuilder('\OCP\Security\ISecureRandom') + $this->getMockBuilder(ISecureRandom::class) ->disableOriginalConstructor() ->getMock(), - $this->getMockBuilder('\OCP\IConfig') + $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock() ); @@ -93,5 +99,8 @@ class DIContainerTest extends \Test\TestCase { $this->assertContains($security, $dispatcher->getMiddlewares()); } - + public function testInvalidAppClass() { + $this->expectException(QueryException::class); + $this->container->query('\OCA\Name\Foo'); + } }