Esempio n. 1
0
 /**
  * test every new and overrided method provided by BackBee\DependencyInjection\Container.
  *
  * @covers ::get
  * @covers ::getContainerValues
  * @covers ::getContainerParameters
  * @covers ::getContainerServices
  * @covers ::isLoaded
  */
 public function testContainerOverridedAndNewMethods()
 {
     // build container to apply every tests on
     $container = new Container();
     // setting random parameters
     $container->setParameter('random_parameter', 'this is a test');
     // creating a definition without any tag (definition of service with id `date_without_tag`)
     $definition = new Definition('DateTime');
     $container->setDefinition('date_without_tag', $definition);
     // creating a definition with one tag, `test` (definition of service with id `date_with_tag`)
     $definition = new Definition('BackBee\\DependencyInjection\\Tests\\ContainerTest_Resources\\DateTime');
     $definition->addTag('test');
     $container->setDefinition('date_with_tag', $definition);
     // creating a definition for a synthetic service (definition of service with id `synthetic_service`)
     $definition = new Definition();
     $definition->setSynthetic(true);
     $container->setDefinition('synthetic_service', $definition);
     // setting into container a service as listener with id `listener`
     $container->set('listener', new TestListener());
     // creating a new event dispatcher and prepare it for testing the dispatch of event on get of tagged service
     // from the container
     $event_dispatcher = new Dispatcher();
     $event_dispatcher->setContainer($container);
     $event_dispatcher->addListener('service.tagged.test', array('@listener', 'onGetServiceTaggedTestEvent'));
     $container->set('event.dispatcher', $event_dispatcher);
     // basic test for the listener
     $this->assertEquals('bar', $container->get('listener')->getFoo());
     // tests of Container::isLoaded() method
     $this->assertFalse($container->isLoaded('date_without_tag'));
     $container->get('date_without_tag');
     $this->assertTrue($container->isLoaded('date_without_tag'));
     // checks that getting a service without the `test` tag won't change listener's foo value
     $this->assertEquals('bar', $container->get('listener')->getFoo());
     // tests to get a service with tag to check if the value of foo from listener has changed
     // and if the timestamp of the service `date_with_tag` has changed correctly
     $container->get('date_with_tag');
     $this->assertEquals('foo', $container->get('listener')->getFoo());
     $this->assertEquals(self::NEW_DATE_WITH_TAG_VALUE, $container->get('date_with_tag')->getTimestamp());
     // tests that if we get a synthetic service which we didn't define it yet Container::get() will return null
     $this->assertEquals(null, $container->get('synthetic_service'));
     // test for Container::getContainerValues()
     $paramter_key_string = '%random_parameter%';
     $this->assertEquals('this is a test', $container->getContainerValues($paramter_key_string));
     $service_string_id = '@listener';
     $this->assertEquals($container->get('listener')->getFoo(), $container->getContainerValues($service_string_id)->getFoo());
     // Test that there is no events to dispatch if the tag name is `dumpable`
     $container->set('listener', new TestListener());
     $event_dispatcher->addListener('service.tagged.dumpable', array('@listener', 'onGetServiceTaggedTestEvent'));
     $definition = new Definition('DateTime');
     $definition->addTag('dumpable', array('dispatch_event' => false));
     $container->setDefinition('dumpable_date', $definition);
     $container->get('dumpable_date');
     $this->assertEquals('bar', $container->get('listener')->getFoo());
 }
 /**
  * @see Symfony\Component\DependencyInjection\ContainerBuilder::set
  */
 public function set($id, $service, $scope = self::SCOPE_CONTAINER)
 {
     $definition = $this->hasDefinition($id) ? $this->getDefinition($id) : null;
     if (null !== $definition && $definition->isSynthetic()) {
         foreach ($definition->getMethodCalls() as $method) {
             $arguments = [];
             foreach ($method[1] as $argument) {
                 if ($argument instanceof Reference) {
                     if (!$this->has($argument->__toString())) {
                         continue;
                     }
                     $argument = $this->get($argument->__toString());
                 }
                 $arguments[] = $argument;
             }
             call_user_func_array([$service, $method[0]], $arguments);
         }
     }
     parent::set($id, $service, $scope);
 }