/** * @dataProvider crossCheckLoadersDumpers */ public function testCrossCheck($fixture, $type) { $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\' . ucfirst($type) . 'FileLoader'; $dumperClass = 'Symfony\\Components\\DependencyInjection\\Dumper\\' . ucfirst($type) . 'Dumper'; $container1 = new Builder(); $loader1 = new $loaderClass($container1); $loader1->load(self::$fixturesPath . '/' . $type . '/' . $fixture); $container1->setParameter('path', self::$fixturesPath . '/includes'); $dumper = new $dumperClass($container1); $tmp = tempnam('sf_service_container', 'sf'); file_put_contents($tmp, $dumper->dump()); $container2 = new Builder(); $loader2 = new $loaderClass($container2); $loader2->load($tmp); $container2->setParameter('path', self::$fixturesPath . '/includes'); unlink($tmp); $this->assertEquals(serialize($container2), serialize($container1), 'loading a dump from a previously loaded container returns the same container'); $this->assertEquals($container2->getParameterBag()->all(), $container1->getParameterBag()->all(), '->getParameterBag() returns the same value for both containers'); $services1 = array(); foreach ($container1 as $id => $service) { $services1[$id] = serialize($service); } $services2 = array(); foreach ($container2 as $id => $service) { $services2[$id] = serialize($service); } unset($services1['service_container'], $services2['service_container']); $this->assertEquals($services2, $services1, 'Iterator on the containers returns the same services'); }
$t->is($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() passes the arguments to the constructor'); // ->createService() # method calls $t->diag('->createService() # method calls'); $builder = new Builder(); $builder->register('bar', 'stdClass'); $builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar')))); $builder->setParameter('value', 'bar'); $t->is($builder->getService('foo1')->bar, array('bar', $builder->getService('bar')), '->createService() replaces the values in the method calls arguments'); // ->createService() # configurator require_once $fixturesPath . '/includes/classes.php'; $t->diag('->createService() # configurator'); $builder = new Builder(); $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure'); $t->ok($builder->getService('foo1')->configured, '->createService() calls the configurator'); $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic')); $builder->setParameter('class', 'BazClass'); $t->ok($builder->getService('foo2')->configured, '->createService() calls the configurator'); $builder->register('baz', 'BazClass'); $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure')); $t->ok($builder->getService('foo3')->configured, '->createService() calls the configurator'); $builder->register('foo4', 'FooClass')->setConfigurator('foo'); try { $builder->getService('foo4'); $t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); } catch (\InvalidArgumentException $e) { $t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); } // ::resolveValue() $t->diag('::resolveValue()'); $t->is(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found'); $t->is(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values');
/** * @covers Symfony\Components\DependencyInjection\Builder::createService */ public function testCreateServiceConfigurator() { $builder = new Builder(); $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure'); $this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator'); $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic')); $builder->setParameter('class', 'BazClass'); $this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator'); $builder->register('baz', 'BazClass'); $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure')); $this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator'); $builder->register('foo4', 'FooClass')->setConfigurator('foo'); try { $builder->get('foo4'); $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); $this->assertEquals('The configure callable for class "FooClass" is not a callable.', $e->getMessage(), '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); } }
// cross-check loaders/dumpers $t->diag('cross-check loaders/dumpers'); $fixtures = array('services1.xml' => 'xml', 'services2.xml' => 'xml', 'services6.xml' => 'xml', 'services8.xml' => 'xml', 'services9.xml' => 'xml', 'services1.yml' => 'yaml', 'services2.yml' => 'yaml', 'services6.yml' => 'yaml', 'services8.yml' => 'yaml', 'services9.yml' => 'yaml'); foreach ($fixtures as $fixture => $type) { $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\' . ucfirst($type) . 'FileLoader'; $dumperClass = 'Symfony\\Components\\DependencyInjection\\Dumper\\' . ucfirst($type) . 'Dumper'; $container1 = new Builder(); $loader1 = new $loaderClass($container1); $loader1->load($fixturesPath . '/' . $type . '/' . $fixture); $container1->setParameter('path', $fixturesPath . '/includes'); $dumper = new $dumperClass($container1); $tmp = tempnam('sf_service_container', 'sf'); file_put_contents($tmp, $dumper->dump()); $container2 = new Builder(); $loader2 = new $loaderClass($container2); $loader2->load($tmp); $container2->setParameter('path', $fixturesPath . '/includes'); unlink($tmp); $t->is(serialize($container1), serialize($container2), 'loading a dump from a previously loaded container returns the same container'); $t->is($container1->getParameters(), $container2->getParameters(), '->getParameters() returns the same value for both containers'); $services1 = array(); foreach ($container1 as $id => $service) { $services1[$id] = serialize($service); } $services2 = array(); foreach ($container2 as $id => $service) { $services2[$id] = serialize($service); } unset($services1['service_container'], $services2['service_container']); $t->is($services1, $services2, 'Iterator on the containers returns the same services'); }
public function testCreateServiceConfigurator() { require_once self::$fixturesPath . '/includes/classes.php'; $builder = new Builder(); $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure'); $this->assertTrue($builder->getService('foo1')->configured, '->createService() calls the configurator'); $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic')); $builder->setParameter('class', 'BazClass'); $this->assertTrue($builder->getService('foo2')->configured, '->createService() calls the configurator'); $builder->register('baz', 'BazClass'); $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure')); $this->assertTrue($builder->getService('foo3')->configured, '->createService() calls the configurator'); $builder->register('foo4', 'FooClass')->setConfigurator('foo'); try { $builder->getService('foo4'); $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); } catch (\InvalidArgumentException $e) { } }