def testProcessIgnoresMethodCalls(self):

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

        self._process(container);
    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);
Esempio n. 3
0
    def process(self, container):
        assert isinstance(container, ContainerBuilder);

        parameters = container.getParameterBag().all();
        definitions = container.getDefinitions();
        aliases = container.getAliases();

        for extension in container.getExtensions().values():
            if isinstance(extension, PrependExtensionInterface):
                extension.prepend(container);

        for name, extension in container.getExtensions().items():
            config = container.getExtensionConfig(name);
            if not config:
                # this extension was not called
                continue;

            config = container.getParameterBag().resolveValue(config);

            tmpContainer = ContainerBuilder(container.getParameterBag());
            tmpContainer.setResourceTracking(container.isTrackingResources());
            tmpContainer.addObjectResource(extension);

            extension.load(config, tmpContainer);

            container.merge(tmpContainer);

        container.addDefinitions(definitions);
        container.addAliases(aliases);
        container.getParameterBag().add(parameters);
    def testProcess(self):

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

        self._process(container);
    def testProcess(self):

        container = ContainerBuilder()
        container.register('a', 'class')
        container.register('b', 'class').setSynthetic(True).setPublic(True)
        container.register('c', 'class').setAbstract(True)
        container.register('d', 'class').setSynthetic(True)

        self._process(container)
    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())
    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]))
    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());
    def testProcess(self):

        container = ContainerBuilder();
        container.register('a', 'class');
        container.register('b', 'class').setSynthetic(True).setPublic(True);
        container.register('c', 'class').setAbstract(True);
        container.register('d', 'class').setSynthetic(True);

        self._process(container);
Esempio n. 10
0
    def testProcessWithInvalidAlias(self):
        """@expectedException InvalidArgumentException

        """

        try:
            container = ContainerBuilder();
            container.setAlias('a_alias', 'a');
            self._process(container);

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, InvalidArgumentException));
Esempio n. 11
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]));
    def testProcessWithInvalidAlias(self):
        """@expectedException InvalidArgumentException

        """

        try:
            container = ContainerBuilder()
            container.setAlias('a_alias', 'a')
            self._process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, InvalidArgumentException))
    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())
    def testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass(self):
        """@expectedException RuntimeException

        """

        try:
            container = ContainerBuilder()
            container.register('a').setSynthetic(False).setAbstract(False)

            self._process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
    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))
Esempio n. 16
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()));
Esempio n. 17
0
    def testProcessDetectsSyntheticPrototypeDefinitions(self):
        """@expectedException RuntimeException

        """

        try:
            container = ContainerBuilder();
            container.register('a').setSynthetic(True).setScope(ContainerInterface.SCOPE_PROTOTYPE);

            self._process(container);

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException));
Esempio n. 18
0
    def testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass(self):
        """@expectedException RuntimeException

        """

        try:
            container = ContainerBuilder();
            container.register('a').setSynthetic(False).setAbstract(False);

            self._process(container);

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException));
Esempio n. 19
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));
Esempio n. 20
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());
Esempio 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));
    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'))
    def testProcessDetectsSyntheticPrototypeDefinitions(self):
        """@expectedException RuntimeException

        """

        try:
            container = ContainerBuilder()
            container.register('a').setSynthetic(True).setScope(
                ContainerInterface.SCOPE_PROTOTYPE)

            self._process(container)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, RuntimeException))
Esempio n. 24
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));
    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))
    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())
Esempio n. 29
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'));
Esempio n. 30
0
    def getContainerExtension(self):
        """Returns the bundle's container extension.

        @return: ExtensionInterface|null The container extension

        @raise LogicException: When alias not respect the naming convention
        """
        if self._extension is None:
            basename = re.sub(r"Bundle$", "", self.getName())

            moduleName = "{0}.dependency".format(self.getNamespace())
            className = "{0}.{1}Extension".format(moduleName, basename)

            r = ReflectionClass(className)

            if r.exists():
                extension = r.newInstance()
                # check naming convention
                expectedAlias = ContainerBuilder.underscore(basename)
                if expectedAlias != extension.getAlias():
                    raise LogicException(
                        'The extension alias for the default extension of a '
                        'bundle must be the underscored version of the '
                        'bundle name ("{0}" instead of "{1}")'
                        ''.format(expectedAlias, extension.getAlias()))

                self._extension = extension
            else:
                self._extension = False

        if self._extension:
            return self._extension
Esempio n. 31
0
 def getAlias(self):
     className = str(type(self).__name__)
     if not className.endswith("Extension"):
         raise BadMethodCallException(
             'This extension does not follow the naming convention; '
             'you must overwrite the getAlias() method.')
     classBaseName = className[:-9]
     return ContainerBuilder.underscore(classBaseName)
Esempio n. 32
0
    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());
Esempio n. 33
0
    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());
Esempio n. 34
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));
Esempio n. 35
0
    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());
Esempio n. 36
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());
Esempio n. 37
0
 def getAlias(self):
     className = str(type(self).__name__);
     if not className.endswith("Extension"):
         raise BadMethodCallException(
             'This extension does not follow the naming convention; '
             'you must overwrite the getAlias() method.'
         );
     classBaseName = className[:-9];
     return ContainerBuilder.underscore(classBaseName);
    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())
    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'))
    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)
    def testProcessIgnoreNonExistentServices(self):

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

        self._process(container)

        arguments = defi.getArguments()
        self.assertEqual('bar', str(arguments[0]))
    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())
Esempio n. 43
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);
    def testProcess(self):

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

        self._process(container)
Esempio n. 45
0
    def process(self, container):
        assert isinstance(container, ContainerBuilder)

        parameters = container.getParameterBag().all()
        definitions = container.getDefinitions()
        aliases = container.getAliases()

        for extension in container.getExtensions().values():
            if isinstance(extension, PrependExtensionInterface):
                extension.prepend(container)

        for name, extension in container.getExtensions().items():
            config = container.getExtensionConfig(name)
            if not config:
                # this extension was not called
                continue

            config = container.getParameterBag().resolveValue(config)

            tmpContainer = ContainerBuilder(container.getParameterBag())
            tmpContainer.setResourceTracking(container.isTrackingResources())
            tmpContainer.addObjectResource(extension)

            extension.load(config, tmpContainer)

            container.merge(tmpContainer)

        container.addDefinitions(definitions)
        container.addAliases(aliases)
        container.getParameterBag().add(parameters)
    def testProcessIgnoresMethodCalls(self):

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

        self._process(container)
    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'))
    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()))
Esempio n. 49
0
    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()
        );
Esempio n. 50
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'));
    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 testProcessIgnoresScopeWideningIfNonStrictReference(self):

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

        self._process(container)
Esempio n. 53
0
    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 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))
Esempio n. 55
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'));
    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())
    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()))
Esempio n. 58
0
    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());
Esempio n. 59
0
    def testProcess(self):

        container = ContainerBuilder();

        container.register('a', 'object').addArgument(Reference('b'));
        container.register('b', 'object');
Esempio n. 60
0
    def testProcess(self):

        container = ContainerBuilder();

        container.register('a', 'object');

        bDefinition = Definition('object');
        bDefinition.setPublic(False);
        container.setDefinition('b', bDefinition);

        container.setAlias('a_alias', 'a');
        container.setAlias('b_alias', 'b');

        self._process(container);

        self.assertTrue(container.has('a'), '->process() does nothing to public definitions.');
        self.assertTrue(container.hasAlias('a_alias'));
        self.assertFalse(container.has('b'), '->process() removes non-public definitions.');
        self.assertTrue(
            container.has('b_alias') and not container.hasAlias('b_alias'),
            '->process() replaces alias to actual.'
        );