You are here

public function XmlFileLoaderTest::testExtensions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/dependency-injection/Tests/Loader/XmlFileLoaderTest.php \Symfony\Component\DependencyInjection\Tests\Loader\XmlFileLoaderTest::testExtensions()

File

vendor/symfony/dependency-injection/Tests/Loader/XmlFileLoaderTest.php, line 302

Class

XmlFileLoaderTest

Namespace

Symfony\Component\DependencyInjection\Tests\Loader

Code

public function testExtensions() {
  $container = new ContainerBuilder();
  $container
    ->registerExtension(new \ProjectExtension());
  $container
    ->registerExtension(new \ProjectWithXsdExtension());
  $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));

  // extension without an XSD
  $loader
    ->load('extensions/services1.xml');
  $container
    ->compile();
  $services = $container
    ->getDefinitions();
  $parameters = $container
    ->getParameterBag()
    ->all();
  $this
    ->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  $this
    ->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  $this
    ->assertEquals('BAR', $services['project.service.foo']
    ->getClass(), '->load() parses extension elements');
  $this
    ->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');

  // extension with an XSD
  $container = new ContainerBuilder();
  $container
    ->registerExtension(new \ProjectExtension());
  $container
    ->registerExtension(new \ProjectWithXsdExtension());
  $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
  $loader
    ->load('extensions/services2.xml');
  $container
    ->compile();
  $services = $container
    ->getDefinitions();
  $parameters = $container
    ->getParameterBag()
    ->all();
  $this
    ->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
  $this
    ->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
  $this
    ->assertEquals('BAR', $services['project.service.foo']
    ->getClass(), '->load() parses extension elements');
  $this
    ->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
  $container = new ContainerBuilder();
  $container
    ->registerExtension(new \ProjectExtension());
  $container
    ->registerExtension(new \ProjectWithXsdExtension());
  $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));

  // extension with an XSD (does not validate)
  try {
    $loader
      ->load('extensions/services3.xml');
    $this
      ->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
    $this
      ->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services3.xml'), $e
      ->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
    $e = $e
      ->getPrevious();
    $this
      ->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
    $this
      ->assertContains('The attribute \'bar\' is not allowed', $e
      ->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
  }

  // non-registered extension
  try {
    $loader
      ->load('extensions/services4.xml');
    $this
      ->fail('->load() throws an InvalidArgumentException if the tag is not valid');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the tag is not valid');
    $this
      ->assertStringStartsWith('There is no extension able to load the configuration for "project:bar" (in', $e
      ->getMessage(), '->load() throws an InvalidArgumentException if the tag is not valid');
  }
}