summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2015-07-24 13:43:50 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2015-07-25 01:59:30 +0200
commitd8673dabe37f72c4925d945ac1e225efa91440b9 (patch)
treee1c628edfafcb2595fb7174191e216af8e484de0
parent1f8ee61006a7b3dcc740cb16e5d826fa7c1646c6 (diff)
downloadnextcloud-server-d8673dabe37f72c4925d945ac1e225efa91440b9.tar.gz
nextcloud-server-d8673dabe37f72c4925d945ac1e225efa91440b9.zip
add test for factories
use ref for factory test use a factory for registerAlias Ensure we construct SimpleContainer Use single instance of DIContainer in routing tests
-rw-r--r--lib/private/appframework/dependencyinjection/dicontainer.php1
-rw-r--r--lib/private/appframework/utility/simplecontainer.php2
-rw-r--r--lib/private/server.php1
-rw-r--r--tests/lib/appframework/routing/RoutingTest.php30
-rw-r--r--tests/lib/appframework/utility/SimpleContainerTest.php31
5 files changed, 50 insertions, 15 deletions
diff --git a/lib/private/appframework/dependencyinjection/dicontainer.php b/lib/private/appframework/dependencyinjection/dicontainer.php
index c7ce6545972..e38077e838f 100644
--- a/lib/private/appframework/dependencyinjection/dicontainer.php
+++ b/lib/private/appframework/dependencyinjection/dicontainer.php
@@ -57,6 +57,7 @@ class DIContainer extends SimpleContainer implements IAppContainer {
* @param string $appName the name of the app
*/
public function __construct($appName, $urlParams = array()){
+ parent::__construct();
$this['AppName'] = $appName;
$this['urlParams'] = $urlParams;
diff --git a/lib/private/appframework/utility/simplecontainer.php b/lib/private/appframework/utility/simplecontainer.php
index c1fc96d1975..efe1b0812a6 100644
--- a/lib/private/appframework/utility/simplecontainer.php
+++ b/lib/private/appframework/utility/simplecontainer.php
@@ -148,7 +148,7 @@ class SimpleContainer extends Container implements IContainer {
public function registerAlias($alias, $target) {
$this->registerService($alias, function (IContainer $container) use ($target) {
return $container->query($target);
- });
+ }, false);
}
}
diff --git a/lib/private/server.php b/lib/private/server.php
index 53949b53df7..3503ff70be4 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -74,6 +74,7 @@ class Server extends SimpleContainer implements IServerContainer {
* @param string $webRoot
*/
public function __construct($webRoot) {
+ parent::__construct();
$this->webRoot = $webRoot;
$this->registerService('ContactsManager', function ($c) {
diff --git a/tests/lib/appframework/routing/RoutingTest.php b/tests/lib/appframework/routing/RoutingTest.php
index 4ee3ed58807..51c191fdfb7 100644
--- a/tests/lib/appframework/routing/RoutingTest.php
+++ b/tests/lib/appframework/routing/RoutingTest.php
@@ -120,7 +120,8 @@ class RoutingTest extends \Test\TestCase
}
// route mocks
- $route = $this->mockRoute($verb, $controllerName, $actionName, $requirements, $defaults);
+ $container = new DIContainer('app1');
+ $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
// router mock
$router = $this->getMock("\OC\Route\Router", array('create'));
@@ -133,7 +134,6 @@ class RoutingTest extends \Test\TestCase
->will($this->returnValue($route));
// load route configuration
- $container = new DIContainer('app1');
$config = new RouteConfig($container, $router, $routes);
$config->register();
@@ -151,11 +151,12 @@ class RoutingTest extends \Test\TestCase
$router = $this->getMock("\OC\Route\Router", array('create'));
// route mocks
- $indexRoute = $this->mockRoute('GET', $controllerName, 'index');
- $showRoute = $this->mockRoute('GET', $controllerName, 'show');
- $createRoute = $this->mockRoute('POST', $controllerName, 'create');
- $updateRoute = $this->mockRoute('PUT', $controllerName, 'update');
- $destroyRoute = $this->mockRoute('DELETE', $controllerName, 'destroy');
+ $container = new DIContainer('app1');
+ $indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
+ $showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
+ $createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
+ $updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
+ $destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
$urlWithParam = $url . '/{' . $paramName . '}';
@@ -191,21 +192,28 @@ class RoutingTest extends \Test\TestCase
->will($this->returnValue($destroyRoute));
// load route configuration
- $container = new DIContainer('app1');
$config = new RouteConfig($container, $router, $yaml);
$config->register();
}
/**
+ * @param DIContainer $container
* @param string $verb
* @param string $controllerName
* @param string $actionName
+ * @param array $requirements
+ * @param array $defaults
* @return \PHPUnit_Framework_MockObject_MockObject
*/
- private function mockRoute($verb, $controllerName, $actionName, array $requirements=array(), array $defaults=array())
- {
- $container = new DIContainer('app1');
+ private function mockRoute(
+ DIContainer $container,
+ $verb,
+ $controllerName,
+ $actionName,
+ array $requirements=array(),
+ array $defaults=array()
+ ) {
$route = $this->getMock("\OC\Route\Route", array('method', 'action', 'requirements', 'defaults'), array(), '', false);
$route
->expects($this->exactly(1))
diff --git a/tests/lib/appframework/utility/SimpleContainerTest.php b/tests/lib/appframework/utility/SimpleContainerTest.php
index 09857808b9f..5fe3f2f5b5d 100644
--- a/tests/lib/appframework/utility/SimpleContainerTest.php
+++ b/tests/lib/appframework/utility/SimpleContainerTest.php
@@ -161,10 +161,15 @@ class SimpleContainerTest extends \Test\TestCase {
public function testRegisterAliasService() {
$this->container->registerService('test', function() {
- return 'abc';
- });
+ return new \StdClass;
+ }, true);
$this->container->registerAlias('test1', 'test');
- $this->assertEquals('abc', $this->container->query('test1'));
+ $this->assertSame(
+ $this->container->query('test'), $this->container->query('test'));
+ $this->assertSame(
+ $this->container->query('test1'), $this->container->query('test1'));
+ $this->assertSame(
+ $this->container->query('test'), $this->container->query('test1'));
}
/**
@@ -176,5 +181,25 @@ class SimpleContainerTest extends \Test\TestCase {
);
}
+ public function testRegisterFactory() {
+ $this->container->registerService('test', function() {
+ return new \StdClass();
+ }, false);
+ $this->assertNotSame(
+ $this->container->query('test'), $this->container->query('test'));
+ }
+
+ public function testRegisterAliasFactory() {
+ $this->container->registerService('test', function() {
+ return new \StdClass();
+ }, false);
+ $this->container->registerAlias('test1', 'test');
+ $this->assertNotSame(
+ $this->container->query('test'), $this->container->query('test'));
+ $this->assertNotSame(
+ $this->container->query('test1'), $this->container->query('test1'));
+ $this->assertNotSame(
+ $this->container->query('test'), $this->container->query('test1'));
+ }
}