Ejemplo n.º 1
0
    def testProcessIgnoresMethodCalls(self):

        container = ContainerBuilder()
        container.register('a').addArgument(Reference('b'))
        container.register('b').addMethodCall('setA', [Reference('a')])

        self._process(container)
Ejemplo n.º 2
0
    def testProcess(self):

        container = ContainerBuilder()
        defi = container\
            .register('foo')\
            .setArguments([Reference('bar', ContainerInterface.NULL_ON_INVALID_REFERENCE)])\
            .addMethodCall('foo', [Reference('moo', ContainerInterface.IGNORE_ON_INVALID_REFERENCE)])\

        self._process(container)

        arguments = defi.getArguments()
        self.assertTrue(arguments[0] is None)
        self.assertEqual(0, len(defi.getMethodCalls()))
Ejemplo n.º 3
0
    def testProcess(self):
        """@expectedException RuntimeException

        """

        try:
            container = ContainerBuilder()
            container.register('a').addArgument(Reference('b'))
            container.register('b').addArgument(Reference('a'))

            self._process(container)
            self.fail("")
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
Ejemplo n.º 4
0
    def testProcessDoesNotSaveDuplicateReferences(self):

        container = ContainerBuilder()

        container.register('a')

        ref1 = Reference('a')
        ref2 = Reference('a')
        container.register('b').addArgument(Definition(None, [ref1]))\
            .addArgument(Definition(None, [ref2]))

        graph = self._process(container)

        self.assertEqual(2, len(graph.getNode('a').getInEdges()))
Ejemplo n.º 5
0
    def testProcess(self):

        container = ContainerBuilder()
        container.register('a').addArgument(Reference('b'))
        container.register('b')

        self._process(container)
Ejemplo n.º 6
0
    def __processArguments(self, arguments):
        """Processes the arguments to replace aliases.

        @param list arguments An list of References

        @return array An array of References

        """
        assert isinstance(arguments, (list, dict))

        if isinstance(arguments, dict):
            keys = arguments.keys()
        else:
            keys = range(len(arguments))

        for k in keys:
            argument = arguments[k]
            if isinstance(argument, (list, dict)):
                arguments[k] = self.__processArguments(argument)
            elif (isinstance(argument, Reference)):
                identifier = str(argument)
                defId = self.__getDefinitionId(identifier)

                if (defId != identifier):
                    arguments[k] = Reference(defId,
                                             argument.getInvalidBehavior(),
                                             argument.isStrict())

        return arguments
Ejemplo n.º 7
0
    def process(self, container):
        assert isinstance(container, ContainerBuilder)

        if not container.hasDefinition('cache_warmer'):
            return

        warmers = dict()
        for identifier, attributes in container.findTaggedServiceIds(
                'kernel.cache_warmer').items():
            priority = attributes[0][
                'priority'] if attributes and 'priority' in attributes[
                    0] else '0'
            if priority not in warmers:
                warmers[priority] = list()
            warmers[priority].append(Reference(identifier))

        if not warmers:
            return

        # sort by priority and flatten
        krsortWarmers = self.__krsort(warmers)
        warmers = list()
        for warmerList in krsortWarmers.values():
            for warmer in warmerList:
                warmers.append(warmer)

        container.getDefinition('cache_warmer').replaceArgument(0, warmers)
Ejemplo n.º 8
0
    def __updateArgumentReferences(self, arguments, currentId, newId):
        """Updates argument references.

        @param array  arguments An array of Arguments
        @param string currentId The alias identifier:
        @param string newId     The identifier the alias points to:

        @return array

        """
        assert isinstance(arguments, (list, dict))

        if isinstance(arguments, dict):
            keys = arguments.keys()
        else:
            keys = range(len(arguments))

        for k in keys:
            argument = arguments[k]
            if isinstance(argument, (list, dict)):
                arguments[k] = self.__updateArgumentReferences(
                    argument, currentId, newId)
            elif (isinstance(argument, Reference)):
                if currentId == str(argument):
                    arguments[k] = Reference(newId,
                                             argument.getInvalidBehavior())
                    self.__compiler.addLogMessage(
                        self.__formatter.formatUpdateReference(
                            self, self.__sourceId, currentId, newId))

        return arguments
Ejemplo n.º 9
0
    def testProcessIgnoresScopeWideningIfNonStrictReference(self):

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

        self._process(container)
Ejemplo n.º 10
0
    def testProcessRemovesPropertiesOnInvalid(self):

        container = ContainerBuilder()
        defi = container\
            .register('foo')\
            .setProperty('foo', Reference('bar', ContainerInterface.IGNORE_ON_INVALID_REFERENCE))\

        self._process(container)

        self.assertEqual(dict(), defi.getProperties())
Ejemplo n.º 11
0
    def testProcessRemovesUnusedDefinitionsRecursively(self):

        container = ContainerBuilder()
        container.register('foo').setPublic(False)
        container.register('bar').setArguments([Reference('foo')])\
            .setPublic(False)

        self._process(container)

        self.assertFalse(container.hasDefinition('foo'))
        self.assertFalse(container.hasDefinition('bar'))
Ejemplo n.º 12
0
    def testStrictFlagIsPreserved(self):

        container = ContainerBuilder()
        container.register('bar')
        defi = container\
            .register('foo')\
            .addArgument(Reference('bar', ContainerInterface.NULL_ON_INVALID_REFERENCE, False))\

        self._process(container)

        self.assertFalse(defi.getArgument(0).isStrict())
Ejemplo n.º 13
0
    def testProcessIgnoreNonExistentServices(self):

        container = ContainerBuilder()
        defi = container\
            .register('foo')\
            .setArguments([Reference('bar')])\

        self._process(container)

        arguments = defi.getArguments()
        self.assertEqual('bar', str(arguments[0]))
Ejemplo n.º 14
0
    def process(self, container):
        assert isinstance(container, ContainerBuilder)

        if (False is container.hasDefinition('routing.resolver')):
            return

        definition = container.getDefinition('routing.resolver')

        for identifier, attributes in container.findTaggedServiceIds(
                'routing.loader').items():
            definition.addMethodCall('addLoader', [Reference(identifier)])
Ejemplo n.º 15
0
    def testProcess(self):

        container = ContainerBuilder()
        container.register('foo').setPublic(False)
        container.register('bar').setPublic(False)
        container.register('moo').setArguments([Reference('bar')])

        self._process(container)

        self.assertFalse(container.hasDefinition('foo'))
        self.assertTrue(container.hasDefinition('bar'))
        self.assertTrue(container.hasDefinition('moo'))
Ejemplo n.º 16
0
    def testProcess(self):

        container = ContainerBuilder()
        container.setAlias('bar', 'foo')
        defi = container\
            .register('moo')\
            .setArguments([Reference('bar')])\

        self._process(container)

        arguments = defi.getArguments()
        self.assertEqual('foo', str(arguments[0]))
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
    def testProcess(self):

        container = ContainerBuilder()
        container._ContainerBuilder__definitions = OrderedDict()
        ref1 = Reference('b')
        a = container.register('a').addArgument(ref1)

        ref2 = Reference('a')
        b = container.register('b').addMethodCall('setA', [ref2])

        ref3 = Reference('a')
        ref4 = Reference('b')
        c = container.register('c').addArgument(ref3).addArgument(ref4)

        ref5 = Reference('b')
        d = container.register('d').setProperty('foo', ref5)

        ref6 = Reference('b')
        e = container.register('e').setConfigurator([ref6, 'methodName'])

        graph = self._process(container)

        edges = graph.getNode('b').getInEdges()
        self.assertEqual(4, len(edges))
        self.assertEqual(ref1, edges[0].getValue())
        self.assertEqual(ref4, edges[1].getValue())
        self.assertEqual(ref5, edges[2].getValue())
        self.assertEqual(ref6, edges[3].getValue())
Ejemplo n.º 19
0
    def testProcessDetectsReferencesFromInlinedDefinitions(self):

        container = ContainerBuilder()

        container.register('a')

        ref = Reference('a')
        container.register('b').addArgument(Definition(None, [ref]))

        graph = self._process(container)

        refs = graph.getNode('a').getInEdges()
        self.assertEqual(1, len(refs))
        self.assertEqual(ref, refs[0].getValue())
Ejemplo n.º 20
0
    def process(self, container):
        """@inheritDoc

        """
        assert isinstance(container, ContainerBuilder)

        if not container.hasDefinition('cache_clearer'):
            return

        clearers = list()
        for identifier in container.findTaggedServiceIds(
                'kernel.cache_clearer').keys():
            clearers.append(Reference(identifier))

        container.getDefinition('cache_clearer').replaceArgument(0, clearers)
Ejemplo n.º 21
0
    def testProcessThrowsExceptionOnInvalidReference(self):
        """@expectedException Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException

        """

        try:
            container = ContainerBuilder()

            container.register('a', 'object').addArgument(Reference('b'))

            self.__process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, ServiceNotFoundException))
Ejemplo n.º 22
0
    def testProcessDetectsScopeWidening(self):
        """@expectedException RuntimeException

        """

        try:
            container = ContainerBuilder()
            container.register('a').addArgument(Reference('b'))
            container.register('b').setScope('prototype')

            self._process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
Ejemplo n.º 23
0
    def testProcessWorksWithInlinedDefinitions(self):

        container = ContainerBuilder()
        container\
            .register('foo')\
            .setPublic(False)\

        container\
            .register('bar')\
            .setArguments([Definition(None, [Reference('foo')])])\

        self._process(container)

        self.assertTrue(container.hasDefinition('foo'))
        self.assertTrue(container.hasDefinition('bar'))
Ejemplo n.º 24
0
    def __processArguments(self, arguments, inMethodCall=False):
        """Processes arguments to determine invalid references.

        @param array   arguments    An array of Reference objects
        @param Boolean inMethodCall

        @return array

        @raise RuntimeException When the config is invalid

        """
        assert isinstance(arguments, (list, dict))

        if isinstance(arguments, dict):
            keys = arguments.keys()
        else:
            keys = range(len(arguments))

        for k in keys:
            argument = arguments[k]
            if isinstance(argument, (list, dict)):
                arguments[k] = self.__processArguments(argument, inMethodCall)
            elif isinstance(argument, Reference):
                identifier = str(argument)

                invalidBehavior = argument.getInvalidBehavior()
                exists = self.__container.has(identifier)

                # resolve invalid behavior
                if exists and ContainerInterface.EXCEPTION_ON_INVALID_REFERENCE != invalidBehavior:
                    arguments[k] = Reference(
                        identifier,
                        ContainerInterface.EXCEPTION_ON_INVALID_REFERENCE,
                        argument.isStrict())
                elif (not exists
                      and ContainerInterface.NULL_ON_INVALID_REFERENCE
                      == invalidBehavior):
                    arguments[k] = None
                elif (not exists
                      and ContainerInterface.IGNORE_ON_INVALID_REFERENCE
                      == invalidBehavior):
                    if (inMethodCall):
                        raise RuntimeException("Method shouldn't be called.")

                    arguments[k] = None

        return arguments
Ejemplo n.º 25
0
    def testProcessDetectsReferenceToAbstractDefinition(self):
        """@expectedException RuntimeException

        """

        try:

            container = ContainerBuilder()

            container.register('a').setAbstract(True)
            container.register('b').addArgument(Reference('a'))

            self._process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
Ejemplo n.º 26
0
    def testProcess(self):

        container = ContainerBuilder()

        container.register('a', 'object').addArgument(Reference('b'))
        container.register('b', 'object')