Exemplo n.º 1
0
    def testEnterScopeRecursivelyWithInactiveChildScopes(self):

        container = Container()
        container.addScope(Scope('foo'))
        container.addScope(Scope('bar', 'foo'))

        self.assertFalse(container.isScopeActive('foo'))

        container.enterScope('foo')

        self.assertTrue(container.isScopeActive('foo'))
        self.assertFalse(container.isScopeActive('bar'))
        self.assertFalse(container.has('a'))

        a = Object()
        container.set('a', a, 'foo')

        services = self._getField(container, '_scopedServices')
        self.assertTrue('a' in services['foo'])
        self.assertEqual(a, services['foo']['a'])

        self.assertTrue(container.has('a'))
        container.enterScope('foo')

        services = self._getField(container, '_scopedServices')
        self.assertFalse('a' in services)

        self.assertTrue(container.isScopeActive('foo'))
        self.assertFalse(container.isScopeActive('bar'))
        self.assertFalse(container.has('a'))
Exemplo n.º 2
0
    def testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict(self):

        container = ContainerBuilder()
        container.addScope(Scope('a'))
        container.addScope(Scope('b'))

        container.register('a').setScope('a').addArgument(
            Reference('b', ContainerInterface.EXCEPTION_ON_INVALID_REFERENCE,
                      False))
        container.register('b').setScope('b')

        self._process(container)
Exemplo n.º 3
0
    def testAddScope(self):

        c = Container()
        c.addScope(Scope('foo'))
        c.addScope(Scope('bar', 'foo'))

        self.assertEqual({
            'foo': 'container',
            'bar': 'foo'
        }, self._getField(c, '_scopes'))
        self.assertEqual({
            'foo': ['bar'],
            'bar': []
        }, self._getField(c, '_scopeChildren'))
Exemplo n.º 4
0
    def testAddScopeDoesNotAllowExistingScope(self):
        """
        @expectedException InvalidArgumentException

        """

        try:
            container = Container()
            container.addScope(Scope('foo'))
            container.addScope(Scope('foo'))

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, InvalidArgumentException))
Exemplo n.º 5
0
    def testHasScope(self):

        c = Container()

        self.assertFalse(c.hasScope('foo'))
        c.addScope(Scope('foo'))
        self.assertTrue(c.hasScope('foo'))
Exemplo n.º 6
0
    def testLeaveScopeNotActive(self):

        container = Container()
        container.addScope(Scope('foo'))

        try:
            container.leaveScope('foo')
            self.fail(
                '->leaveScope() raise a LogicException if the scope is not active yet'
            )
        except Exception as e:
            self.assertTrue(
                isinstance(e, LogicException),
                '->leaveScope() raise a LogicException if the scope is not active yet'
            )
            self.assertEqual(
                'The scope "foo" is not active.', e.getMessage(),
                '->leaveScope() raise a LogicException if the scope is not active yet'
            )

        try:
            container.leaveScope('bar')
            self.fail(
                '->leaveScope() raise a LogicException if the scope does not exist'
            )
        except Exception as e:
            self.assertTrue(
                isinstance(e, LogicException),
                '->leaveScope() raise a LogicException if the scope does not exist'
            )
            self.assertEqual(
                'The scope "bar" is not active.', e.getMessage(),
                '->leaveScope() raise a LogicException if the scope does not exist'
            )
Exemplo n.º 7
0
    def testReEnteringAScope(self):

        event = Event();

        service1 = Service();

        scope = Scope('scope');
        container = Container();
        container.addScope(scope);
        container.enterScope('scope');

        container.set('service.listener', service1, 'scope');

        dispatcher = ContainerAwareEventDispatcher(container);
        dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']);
        dispatcher.dispatch('onEvent', event);

        service2 = Service();

        container.enterScope('scope');
        container.set('service.listener', service2, 'scope');

        dispatcher.dispatch('onEvent', event);

        container.leaveScope('scope');

        dispatcher.dispatch('onEvent');
Exemplo n.º 8
0
        def test(scope):
            try:
                c = Container()
                c.addScope(Scope('foo', scope))

                self.fail()
            except Exception as e:
                self.assertTrue(isinstance(e, InvalidArgumentException))
Exemplo n.º 9
0
    def testProcessDetectsCrossScopeHierarchyReference(self):
        """@expectedException RuntimeException

        """
        try:

            container = ContainerBuilder()
            container.addScope(Scope('a'))
            container.addScope(Scope('b'))

            container.register('a').setScope('a').addArgument(Reference('b'))
            container.register('b').setScope('b')

            self._process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
Exemplo n.º 10
0
    def testSetAlsoSetsScopedService(self):

        c = Container()
        c.addScope(Scope('foo'))
        c.enterScope('foo')
        foo = Object()
        c.set('foo', foo, 'foo')

        services = self._getField(c, '_scopedServices')
        self.assertTrue('foo' in services['foo'])
        self.assertEqual(foo, services['foo']['foo'])
Exemplo n.º 11
0
    def testIsScopeActive(self):

        c = Container()

        self.assertFalse(c.isScopeActive('foo'))
        c.addScope(Scope('foo'))

        self.assertFalse(c.isScopeActive('foo'))
        c.enterScope('foo')

        self.assertTrue(c.isScopeActive('foo'))
        c.leaveScope('foo')

        self.assertFalse(c.isScopeActive('foo'))
Exemplo n.º 12
0
    def testSetDoesNotAllowInactiveScope(self):
        """
        @expectedException RuntimeException

        """

        try:
            c = Container()
            c.addScope(Scope('foo'))
            c.set('foo', Object(), 'foo')

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
Exemplo n.º 13
0
    def build(self, container):
        assert isinstance(container, ContainerBuilder)

        Bundle.build(self, container)

        container.addScope(Scope('request'))

        container.addCompilerPass(ConsoleRoutingResolverPass())
        container.addCompilerPass(RegisterKernelListenersPass(),
                                  PassConfig.TYPE_AFTER_REMOVING)
        container.addCompilerPass(AddCacheWarmerPass())
        container.addCompilerPass(AddCacheClearerPass())

        if container.getParameter('kernel.debug'):
            container.addCompilerPass(CompilerDebugDumpPass(),
                                      PassConfig.TYPE_AFTER_REMOVING)
Exemplo n.º 14
0
    def testEnterLeaveCurrentScope(self):

        container = ProjectServiceContainer()
        container.addScope(Scope('foo'))

        container.enterScope('foo')
        scoped1 = container.get('scoped')
        scopedFoo1 = container.get('scoped_foo')

        container.enterScope('foo')
        scoped2 = container.get('scoped')
        scoped3 = container.get('scoped')
        scopedFoo2 = container.get('scoped_foo')

        container.leaveScope('foo')
        scoped4 = container.get('scoped')
        scopedFoo3 = container.get('scoped_foo')

        self.assertNotEqual(scoped1, scoped2)
        self.assertEqual(scoped2, scoped3)
        self.assertEqual(scoped1, scoped4)
        self.assertNotEqual(scopedFoo1, scopedFoo2)
        self.assertEqual(scopedFoo1, scopedFoo3)
Exemplo n.º 15
0
    def testTriggerAListenerServiceOutOfScope(self):
        """@expectedException InvalidArgumentException

        """

        try:
            service = Service();

            scope = Scope('scope');
            container = Container();
            container.addScope(scope);
            container.enterScope('scope');

            container.set('service.listener', service, 'scope');

            dispatcher = ContainerAwareEventDispatcher(container);
            dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']);

            container.leaveScope('scope');
            dispatcher.dispatch('onEvent');

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, InvalidArgumentException));