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\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;
}
/**

View File

@ -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');
}
}