$t->is($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute'); $t->is($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag'); $t->is($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags'); $t->is($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag'); $t->is($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag'); $t->is($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag'); $t->is($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag'); $t->is($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag'); $aliases = $config->getAliases(); $t->ok(isset($aliases['alias_for_foo']), '->load() parses aliases'); $t->is($aliases['alias_for_foo'], 'foo', '->load() parses aliases'); // extensions $t->diag('extensions'); Loader::registerExtension(new ProjectExtension()); $loader = new ProjectLoader($fixturesPath.'/yaml'); $config = $loader->load('services10.yml'); $services = $config->getDefinitions(); $parameters = $config->getParameters(); $t->ok(isset($services['project.service.bar']), '->load() parses extension elements'); $t->ok(isset($parameters['project.parameter.bar']), '->load() parses extension elements'); try { $config = $loader->load('services11.yml'); $t->fail('->load() throws an InvalidArgumentException if the tag is not valid'); } catch (InvalidArgumentException $e) { $t->pass('->load() throws an InvalidArgumentException if the tag is not valid');
public function getAbsolutePath($file, $currentPath = null) { return parent::getAbsolutePath($file, $currentPath); } } // __construct() $t->diag('__construct()'); $loader = new ProjectLoader($container = new sfServiceContainerBuilder()); $t->is($loader->container, $container, '__construct() takes a container builder instance as its first argument'); $loader = new ProjectLoader(null, dirname(__FILE__)); $t->is($loader->paths, array(dirname(__FILE__)), '__construct() takes a path as its second argument'); $loader = new ProjectLoader(null, array(dirname(__FILE__), dirname(__FILE__))); $t->is($loader->paths, array(dirname(__FILE__), dirname(__FILE__)), '__construct() takes an array of paths as its second argument'); // ->getAbsolutePath() $t->diag('->getAbsolutePath()'); $loader = new ProjectLoader(null, array(dirname(__FILE__) . '/../bin')); $t->is($loader->getAbsolutePath('/foo.xml'), '/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('c:\\\\foo.xml'), 'c:\\\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('c:/foo.xml'), 'c:/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('\\server\\foo.xml'), '\\server\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('sfServiceContainerLoaderFileTest.php', dirname(__FILE__)), dirname(__FILE__) . '/sfServiceContainerLoaderFileTest.php', '->getAbsolutePath() returns an absolute filename if the file exists in the current path'); $t->is($loader->getAbsolutePath('prove.php', dirname(__FILE__)), dirname(__FILE__) . '/../bin/prove.php', '->getAbsolutePath() returns an absolute filename if the file exists in one of the paths given in the constructor'); $t->is($loader->getAbsolutePath('foo.xml', dirname(__FILE__)), 'foo.xml', '->getAbsolutePath() returns the path unmodified if it is unable to find it in the given paths'); class ProjectLoader1 extends sfServiceContainerLoaderFile { public $resource; public function doLoad($resource) { $this->resource = $resource; return array(array(), array()); }
* This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__ . '/../../bootstrap/unit.php'; $t = new lime_test(20); class ProjectLoader extends sfServiceContainerLoaderArray { public function validate($content) { return parent::validate($content); } } $loader = new ProjectLoader(null); // ->validate() try { $loader->validate(sfYaml::load(__DIR__ . '/fixtures/yaml/nonvalid1.yml')); $t->fail('->validate() throws an InvalidArgumentException if the loaded definition is not an array'); } catch (InvalidArgumentException $e) { $t->pass('->validate() throws an InvalidArgumentException if the loaded definition is not an array'); } try { $loader->validate(sfYaml::load(__DIR__ . '/fixtures/yaml/nonvalid2.yml')); $t->fail('->validate() throws an InvalidArgumentException if the loaded definition is not a valid array'); } catch (InvalidArgumentException $e) { $t->pass('->validate() throws an InvalidArgumentException if the loaded definition is not a valid array'); } // ->load() # parameters $t->diag('->load() # parameters');
{ return $resource; } } // __construct() $t->diag('__construct()'); $loader = new ProjectLoader($container = new sfServiceContainerBuilder()); $t->is($loader->container, $container, '__construct() takes a container builder instance as its first argument'); // ->setServiceContainer() $t->diag('->setServiceContainer()'); $loader = new ProjectLoader(); $loader->setServiceContainer($container = new sfServiceContainerBuilder()); $t->is($loader->container, $container, '->setServiceContainer() sets the container builder attached to this loader'); // ->load() $t->diag('->load()'); $loader = new ProjectLoader(); try { $loader->load('foo'); $t->fail('->load() throws a LogicException if no container is attached to the loader'); } catch (LogicException $e) { $t->pass('->load() throws a LogicException if no container is attached to the loader'); } $loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo'))); $loader->load(array(array(), array('foo' => 'bar'))); $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->load() merges current parameters with the loaded ones'); $loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo', 'foo' => 'baz'))); $loader->load(array(array(), array('foo' => 'bar'))); $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->load() does not change the already defined parameters'); $loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo'))); $loader->load(array(array(), array('foo' => '%bar%'))); $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->load() evaluates the values of the parameters towards already defined ones');
$doc = new DOMDocument("1.0"); $doc->loadXML('<foo><foo>bar</foo></foo>'); $t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => 'bar'), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new DOMDocument("1.0"); $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>'); $t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => array('value' => 'bar', 'foo' => 'bar')), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new DOMDocument("1.0"); $doc->loadXML('<foo><foo></foo></foo>'); $t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => null), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new DOMDocument("1.0"); $doc->loadXML('<foo><foo><!-- foo --></foo></foo>'); $t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => null), '::convertDomElementToArray() converts a \\DomElement to an array'); // extensions $t->diag('extensions'); Loader::registerExtension(new ProjectExtension()); $loader = new ProjectLoader($fixturesPath . '/xml'); $config = $loader->load('services10.xml'); $services = $config->getDefinitions(); $parameters = $config->getParameters(); $t->ok(isset($services['project.service.bar']), '->load() parses extension elements'); $t->ok(isset($parameters['project.parameter.bar']), '->load() parses extension elements'); try { $config = $loader->load('services11.xml'); $t->fail('->load() throws an InvalidArgumentException if the tag is not valid'); } catch (InvalidArgumentException $e) { $t->pass('->load() throws an InvalidArgumentException if the tag is not valid'); } try { $config = $loader->load('services12.xml'); $t->fail('->load() throws an InvalidArgumentException if an extension is not loaded'); } catch (InvalidArgumentException $e) {
<?php /* * This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__ . '/../../../../bootstrap.php'; use Symfony\Components\DependencyInjection\Loader\Loader; require_once __DIR__ . '/../../../../../fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php'; class ProjectLoader extends Loader { public function load($resource) { } } $t = new LimeTest(1); // ::registerExtension() ::getExtension() $t->diag('::registerExtension() ::getExtension()'); ProjectLoader::registerExtension($extension = new ProjectExtension()); $t->ok(ProjectLoader::getExtension('project') === $extension, '::registerExtension() registers an extension');
try { $loader->getFilesAsArray(array($fixture . '.yml')); $t->fail('->load() throws an InvalidArgumentException if the loaded file does not validate'); } catch (InvalidArgumentException $e) { $t->pass('->load() throws an InvalidArgumentException if the loaded file does not validate'); } } $yamls = $loader->getFilesAsArray(array('services1.yml')); $t->ok(is_array($yamls), '->getFilesAsArray() returns an array'); $t->is(key($yamls), realpath(dirname(__FILE__) . '/fixtures/yaml/services1.yml'), '->getFilesAsArray() returns an array where the keys are absolutes paths to the original YAML file'); // ->load() # parameters $t->diag('->load() # parameters'); $loader = new ProjectLoader(null, dirname(__FILE__) . '/fixtures/yaml'); list($services, $parameters) = $loader->doLoad(array('services2.yml')); $t->is($parameters, array('foo' => 'bar', 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new sfServiceReference('foo_bar')), '->load() converts YAML keys to lowercase'); $loader = new ProjectLoader(null, dirname(__FILE__) . '/fixtures/yaml'); list($services, $parameters) = $loader->doLoad(array('services2.yml', 'services3.yml')); $t->is($parameters, array('foo' => 'foo', 'values' => array(true, false), 'bar' => 'foo', 'foo_bar' => new sfServiceReference('foo_bar')), '->load() merges the first level of arguments when multiple files are loaded'); // ->load() # imports $t->diag('->load() # imports'); list($services, $parameters) = $loader->doLoad(array('services4.yml')); $t->is($parameters, array('foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new sfServiceReference('foo_bar')), '->load() imports and merges imported files'); // ->load() # services $t->diag('->load() # services'); list($services, $parameters) = $loader->doLoad(array('services6.yml')); $t->ok(isset($services['foo']), '->load() parses service elements'); $t->is(get_class($services['foo']), 'sfServiceDefinition', '->load() converts service element to sfServiceDefinition instances'); $t->is($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute'); $t->ok($services['shared']->isShared(), '->load() parses the shared attribute'); $t->ok(!$services['non_shared']->isShared(), '->load() parses the shared attribute'); $t->is($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute');
require_once __DIR__ . '/../../../../bootstrap.php'; use Symfony\Components\DependencyInjection\Builder; use Symfony\Components\DependencyInjection\Loader\FileLoader; $t = new LimeTest(9); class ProjectLoader extends FileLoader { public $paths; public function load($resource) { } public function getAbsolutePath($file, $currentPath = null) { return parent::getAbsolutePath($file, $currentPath); } } // __construct() $t->diag('__construct()'); $loader = new ProjectLoader(__DIR__); $t->is($loader->paths, array(__DIR__), '__construct() takes a path as its second argument'); $loader = new ProjectLoader(array(__DIR__, __DIR__)); $t->is($loader->paths, array(__DIR__, __DIR__), '__construct() takes an array of paths as its second argument'); // ->getAbsolutePath() $t->diag('->getAbsolutePath()'); $loader = new ProjectLoader(array(__DIR__ . '/../../../../../bin')); $t->is($loader->getAbsolutePath('/foo.xml'), '/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('c:\\\\foo.xml'), 'c:\\\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('c:/foo.xml'), 'c:/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('\\server\\foo.xml'), '\\server\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); $t->is($loader->getAbsolutePath('FileLoaderTest.php', __DIR__), __DIR__ . '/FileLoaderTest.php', '->getAbsolutePath() returns an absolute filename if the file exists in the current path'); $t->is($loader->getAbsolutePath('prove.php', __DIR__), __DIR__ . '/../../../../../bin/prove.php', '->getAbsolutePath() returns an absolute filename if the file exists in one of the paths given in the constructor'); $t->is($loader->getAbsolutePath('foo.xml', __DIR__), 'foo.xml', '->getAbsolutePath() returns the path unmodified if it is unable to find it in the given paths');