def testProcessHandlesMultipleInheritance(self):

        container = ContainerBuilder()

        container\
            .register('parent', 'foo')\
            .setArguments(['foo', 'bar', 'c'])\


        container\
            .setDefinition('child2', DefinitionDecorator('child1'))\
            .replaceArgument(1, 'b')\


        container\
            .setDefinition('child1', DefinitionDecorator('parent'))\
            .replaceArgument(0, 'a')\

        self._process(container)

        defi = container.getDefinition('child2')
        self.assertEqual(['a', 'b', 'c'], defi.getArguments())
        self.assertEqual('foo', defi.getClass())
    def testProcessDoesNotCopyTags(self):

        container = ContainerBuilder()

        container\
            .register('parent')\
            .addTag('foo')\


        container\
            .setDefinition('child', DefinitionDecorator('parent'))\

        self._process(container)

        defi = container.getDefinition('child')
        self.assertEqual({}, defi.getTags())
    def testProcessDoesNotCopyScope(self):

        container = ContainerBuilder()

        container\
            .register('parent')\
            .setScope('foo')\


        container\
            .setDefinition('child', DefinitionDecorator('parent'))\

        self._process(container)

        defi = container.getDefinition('child')
        self.assertEqual(ContainerInterface.SCOPE_CONTAINER, defi.getScope())
    def testProcessDoesNotCopyAbstract(self):

        container = ContainerBuilder()

        container\
            .register('parent')\
            .setAbstract(True)\


        container\
            .setDefinition('child', DefinitionDecorator('parent'))\

        self._process(container)

        defi = container.getDefinition('child')
        self.assertFalse(defi.isAbstract())
    def testProcess(self):

        container = ContainerBuilder()
        container.register('parent',
                           'foo').setArguments(['moo', 'b'
                                                ]).setProperty('foo', 'moo')
        container.setDefinition('child', DefinitionDecorator('parent'))\
            .replaceArgument(0, 'a')\
            .setProperty('foo', 'bar')\
            .setClass('bar')\

        self._process(container)

        definition = container.getDefinition('child')
        self.assertFalse(isinstance(object, DefinitionDecorator))
        self.assertEqual('bar', definition.getClass())
        self.assertEqual(['a', 'b'], definition.getArguments())
        self.assertEqual({'foo': 'bar'}, definition.getProperties())
    def testProcessAppendsMethodCallsAlways(self):

        container = ContainerBuilder()

        container\
            .register('parent')\
            .addMethodCall('foo', ['bar'])\


        container\
            .setDefinition('child', DefinitionDecorator('parent'))\
            .addMethodCall('bar', ['foo'])\

        self._process(container)

        definition = container.getDefinition('child')
        self.assertEqual([
            ['foo', ['bar']],
            ['bar', ['foo']],
        ], definition.getMethodCalls())
Example #7
0
    def __parseDefinition(self, identifier, service, resource):
        """Parses a definition.

        @param identifier: string
        @param service:    dict
        @param resource:   string

        @raise InvalidArgumentException: When tags are invalid

        """

        if isinstance(service, String) and service.startswith('@'):
            self._container.setAlias(identifier, service[1:])

            return
        elif 'alias' in service:
            public = 'public' not in service or bool(service['public'])
            self._container.setAlias(identifier, Alias(service['alias'],
                                                       public))

            return

        if 'parent' in service:
            definition = DefinitionDecorator(service['parent'])
        else:
            definition = Definition()

        if 'class' in service:
            definition.setClass(service['class'])

        if 'scope' in service:
            definition.setScope(service['scope'])

        if 'synthetic' in service:
            definition.setSynthetic(service['synthetic'])

        if 'public' in service:
            definition.setPublic(service['public'])

        if 'abstract' in service:
            definition.setAbstract(service['abstract'])

        if 'factory_class' in service:
            definition.setFactoryClass(service['factory_class'])

        if 'factory_method' in service:
            definition.setFactoryMethod(service['factory_method'])

        if 'factory_service' in service:
            definition.setFactoryService(service['factory_service'])

        if 'file' in service:
            definition.setFile(service['file'])

        if 'arguments' in service:
            definition.setArguments(
                self.__resolveServices(service['arguments']))

        if 'properties' in service:
            definition.setProperties(
                self.__resolveServices(service['properties']))

        if 'configurator' in service:
            if isinstance(service['configurator'], String):
                definition.setConfigurator(service['configurator'])
            else:
                definition.setConfigurator([
                    self.__resolveServices(service['configurator'][0]),
                    service['configurator'][1]
                ])

        if 'calls' in service:
            for call in service['calls']:
                args = self.__resolveServices(
                    call[1]) if len(call) >= 2 else []
                definition.addMethodCall(call[0], args)

        if 'tags' in service:
            if not isinstance(service['tags'], list):
                raise InvalidArgumentException(
                    'Parameter "tags" must be a list for service '
                    '"{0}" in {1}.'.format(identifier, resource))

            for tag in service['tags']:
                if not isinstance(tag, dict) or 'name' not in tag:
                    raise InvalidArgumentException(
                        'A "tags" entry is missing a "name" key for service '
                        '"{0}" in {1}.'.format(identifier, resource))

                name = tag['name']
                del tag['name']

                for value in tag.values():
                    if not isinstance(value,
                                      (type(None), String, int, float, bool)):
                        raise InvalidArgumentException(
                            'A "tags" attribute must be of a scalar-type '
                            'for service "{0}", tag "{1}" in {2}.'
                            ''.format(identifier, name, resource))

                definition.addTag(name, tag)

        self._container.setDefinition(identifier, definition)