Ejemplo n.º 1
0
    def testGetServiceIds(self):
        """
        @covers Symfony\Component\DependencyInjection\Container.getServiceIds

        """

        sc = Container()
        sc.set('foo', Object())
        sc.set('bar', Object())
        self.assertTrue('service_container' in sc.getServiceIds(),
                        '->getServiceIds() returns all defined service ids')
        self.assertTrue('foo' in sc.getServiceIds(),
                        '->getServiceIds() returns all defined service ids')
        self.assertTrue('bar' in sc.getServiceIds(),
                        '->getServiceIds() returns all defined service ids')

        sc = ProjectServiceContainer()
        ids = sc.getServiceIds()
        message = '->getServiceIds() returns defined service ids by getXXXService() methods'
        self.assertTrue('scoped' in ids, message)
        self.assertTrue('scoped_foo' in ids, message)
        self.assertTrue('bar' in ids, message)
        self.assertTrue('foo_bar' in ids, message)
        self.assertTrue('foo_baz' in ids, message)
        self.assertTrue('circular' in ids, message)
        self.assertTrue('throw_exception' in ids, message)
        self.assertTrue('throws_exception_on_service_configuration' in ids,
                        message)
        self.assertTrue('service_container' in ids, message)
Ejemplo n.º 2
0
    def __init__(self):

        Container.__init__(self)

        self.bar = Object()
        self.foo_bar = Object()
        self.foo_baz = Object()
Ejemplo n.º 3
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'))
Ejemplo n.º 4
0
    def setUp(self):
        """Prepares the environment before running a test.

        """

        self._subject = Object();
        self._event = GenericEvent(self._subject, {'name' : 'Event'});
Ejemplo n.º 5
0
    def getThrowsExceptionOnServiceConfigurationService(self):

        self._services[
            'throws_exception_on_service_configuration'] = instance = Object()

        raise StandardException(
            'Something was terribly wrong while trying to configure the service!'
        )
Ejemplo n.º 6
0
    def getScopedFooService(self):

        if 'foo' not in self._scopedServices:
            raise RuntimeException('Invalid call')

        self._services['scoped_foo'] = self._scopedServices['foo'][
            'scoped_foo'] = Object()
        return self._services['scoped_foo']
Ejemplo n.º 7
0
    def testSet(self):
        """
        @covers Symfony\Component\DependencyInjection\Container.set

        """

        sc = Container()
        foo = Object()
        sc.set('foo', foo)
        self.assertEqual(foo, sc.get('foo'), '->set() sets a service')
Ejemplo n.º 8
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'])
Ejemplo n.º 9
0
    def testGet(self):
        """
        @covers Symfony\Component\DependencyInjection\Container.get

        """

        sc = ProjectServiceContainer()
        foo = Object()
        sc.set('foo', foo)
        self.assertEqual(foo, sc.get('foo'),
                         '->get() returns the service for the given id')
        self.assertEqual(sc.bar, sc.get('bar'),
                         '->get() returns the service for the given id')
        self.assertEqual(
            sc.foo_bar, sc.get('foo_bar'),
            '->get() returns the service if a get*Method() is defined')
        self.assertEqual(
            sc.foo_baz, sc.get('foo.baz'),
            '->get() returns the service if a get*Method() is defined')

        bar = Object()
        sc.set('bar', bar)
        self.assertEqual(
            bar, sc.get('bar'),
            '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()'
        )

        try:
            sc.get('')
            self.fail(
                '->get() raise a InvalidArgumentException exception if the service is empty'
            )
        except Exception as e:
            self.assertTrue(
                isinstance(e, ServiceNotFoundException),
                '->get() raise a ServiceNotFoundException exception if the service is empty'
            )

        self.assertTrue(
            None is sc.get('', ContainerInterface.NULL_ON_INVALID_REFERENCE))
Ejemplo n.º 10
0
    def testSetDoesNotAllowPrototypeScope(self):
        """
        @expectedException InvalidArgumentException

        """

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

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, InvalidArgumentException))
Ejemplo n.º 11
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))
Ejemplo n.º 12
0
    def testInitialized(self):
        """
        @covers Symfony\Component\DependencyInjection\Container.initialized

        """

        sc = ProjectServiceContainer()
        sc.set('foo', Object())
        self.assertTrue(sc.initialized('foo'),
                        '->initialized() returns True if service is loaded')
        self.assertFalse(
            sc.initialized('foo1'),
            '->initialized() returns False if service is not loaded')
        self.assertFalse(
            sc.initialized('bar'),
            '->initialized() returns False if a service is defined, but not currently loaded'
        )
Ejemplo n.º 13
0
    def testHas(self):
        """
        @covers Symfony\Component\DependencyInjection\Container.has

        """

        sc = ProjectServiceContainer()
        sc.set('foo', Object())
        self.assertFalse(
            sc.has('foo1'),
            '->has() returns False if the service does not exist')
        self.assertTrue(sc.has('foo'),
                        '->has() returns True if the service exists')
        self.assertTrue(sc.has('bar'),
                        '->has() returns True if a get*Method() is defined')
        self.assertTrue(sc.has('foo_bar'),
                        '->has() returns True if a get*Method() is defined')
        self.assertTrue(sc.has('foo.baz'),
                        '->has() returns True if a get*Method() is defined')