Extend DI tests

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2017-03-19 22:20:56 +01:00
parent 246e9ce547
commit 7cece61ff6
No known key found for this signature in database
GPG Key ID: F941078878347C0C
2 changed files with 26 additions and 9 deletions

View File

@ -51,6 +51,7 @@ use OCP\AppFramework\QueryException;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\Mount\IMountManager; use OCP\Files\Mount\IMountManager;
use OCP\IServerContainer;
use OCP\RichObjectStrings\IValidator; use OCP\RichObjectStrings\IValidator;
use OCP\Util; use OCP\Util;
@ -61,18 +62,25 @@ class DIContainer extends SimpleContainer implements IAppContainer {
*/ */
private $middleWares = array(); private $middleWares = array();
/** @var IServerContainer */
private $server;
/** /**
* Put your class dependencies in here * Put your class dependencies in here
* @param string $appName the name of the app * @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(); parent::__construct();
$this['AppName'] = $appName; $this['AppName'] = $appName;
$this['urlParams'] = $urlParams; $this['urlParams'] = $urlParams;
/** @var \OC\ServerContainer $server */ /** @var \OC\ServerContainer $server */
$server = $this->getServer(); if ($server === null) {
$server->registerAppContainer($appName, $this); $this->server = \OC::$server;
}
$this->server->registerAppContainer($appName, $this);
// aliases // aliases
$this->registerAlias('appName', 'AppName'); $this->registerAlias('appName', 'AppName');
@ -481,7 +489,7 @@ class DIContainer extends SimpleContainer implements IAppContainer {
*/ */
function getServer() function getServer()
{ {
return OC::$server; return $this->server;
} }
/** /**

View File

@ -27,23 +27,29 @@
namespace Test\AppFramework\DependencyInjection; namespace Test\AppFramework\DependencyInjection;
use OC\AppFramework\Core\API;
use OC\AppFramework\DependencyInjection\DIContainer;
use \OC\AppFramework\Http\Request; use \OC\AppFramework\Http\Request;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
/** /**
* @group DB * @group DB
*/ */
class DIContainerTest extends \Test\TestCase { class DIContainerTest extends \Test\TestCase {
/** @var DIContainer|\PHPUnit_Framework_MockObject_MockObject */
private $container; private $container;
private $api; private $api;
protected function setUp(){ protected function setUp(){
parent::setUp(); parent::setUp();
$this->container = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer') $this->container = $this->getMockBuilder(DIContainer::class)
->setMethods(['isAdminUser']) ->setMethods(['isAdminUser'])
->setConstructorArgs(['name']) ->setConstructorArgs(['name'])
->getMock(); ->getMock();
$this->api = $this->getMockBuilder('OC\AppFramework\Core\API') $this->api = $this->getMockBuilder(API::class)
->setConstructorArgs(['hi']) ->setConstructorArgs(['hi'])
->getMock(); ->getMock();
} }
@ -80,10 +86,10 @@ class DIContainerTest extends \Test\TestCase {
public function testMiddlewareDispatcherIncludesSecurityMiddleware(){ public function testMiddlewareDispatcherIncludesSecurityMiddleware(){
$this->container['Request'] = new Request( $this->container['Request'] = new Request(
['method' => 'GET'], ['method' => 'GET'],
$this->getMockBuilder('\OCP\Security\ISecureRandom') $this->getMockBuilder(ISecureRandom::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(), ->getMock(),
$this->getMockBuilder('\OCP\IConfig') $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
); );
@ -93,5 +99,8 @@ class DIContainerTest extends \Test\TestCase {
$this->assertContains($security, $dispatcher->getMiddlewares()); $this->assertContains($security, $dispatcher->getMiddlewares());
} }
public function testInvalidAppClass() {
$this->expectException(QueryException::class);
$this->container->query('\OCA\Name\Foo');
}
} }