$t->ok($builder->getService('foo2')->configured, '->createService() calls the configurator');
$builder->register('baz', 'BazClass');
$builder->register('foo3', 'FooClass')->setConfigurator(array(new sfServiceReference('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()');
$builder = new sfServiceContainerBuilder();
$t->is($builder->resolveValue('foo'), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found');
$builder->setParameter('foo', 'bar');
$t->is($builder->resolveValue('I\'m a %foo%'), 'I\'m a bar', '->resolveValue() replaces placeholders by their values');
$builder->setParameter('foo', true);
$t->ok($builder->resolveValue('%foo%') === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
$builder->setParameter('foo', 'bar');
$t->is($builder->resolveValue(array('%foo%' => '%foo%')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays');
$t->is($builder->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), array('bar' => array('bar' => array('bar' => 'bar'))), '->resolveValue() replaces placeholders in nested arrays');
$t->is($builder->resolveValue('I\'m a %%foo%%'), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it');
$t->is($builder->resolveValue('I\'m a %foo% %%foo %foo%'), 'I\'m a bar %foo bar', '->resolveValue() supports % escaping by doubling it');
try {
    $builder->resolveValue('%foobar%');
    $t->fail('->resolveValue() throws a InvalidArgumentException if a placeholder references a non-existant parameter');
} catch (InvalidArgumentException $e) {
    $t->pass('->resolveValue() throws a InvalidArgumentException if a placeholder references a non-existant parameter');
}
try {
Esempio n. 2
0
// 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');
foreach ($fixtures as $fixture => $type) {
    $loaderClass = 'sfServiceContainerLoaderFile' . ucfirst($type);
    $dumperClass = 'sfServiceContainerDumper' . ucfirst($type);
    $container1 = new sfServiceContainerBuilder();
    $loader1 = new $loaderClass($container1);
    $loader1->load(dirname(__FILE__) . '/fixtures/' . $type . '/' . $fixture);
    $container1->setParameter('path', dirname(__FILE__) . '/fixtures/includes');
    $dumper = new $dumperClass($container1);
    $tmp = tempnam('sf_service_container', 'sf');
    file_put_contents($tmp, $dumper->dump());
    $container2 = new sfServiceContainerBuilder();
    $loader2 = new $loaderClass($container2);
    $loader2->load($tmp);
    $container2->setParameter('path', dirname(__FILE__) . '/fixtures/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');
}