public function XmlFileLoaderTest::testParseFile

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php, line 58

Class

XmlFileLoaderTest

Namespace

Symfony\Component\DependencyInjection\Tests\Loader

Code

public function testParseFile() {
  $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath . '/ini'));
  $r = new \ReflectionObject($loader);
  $m = $r
    ->getMethod('parseFile');
  $m
    ->setAccessible(true);
  try {
    $m
      ->invoke($loader, self::$fixturesPath . '/ini/parameters.ini');
    $this
      ->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
    $this
      ->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'parameters.ini'), $e
      ->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
    $e = $e
      ->getPrevious();
    $this
      ->assertInstanceOf('InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
    $this
      ->assertStringStartsWith('[ERROR 4] Start tag expected, \'<\' not found (in', $e
      ->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
  }
  $loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath . '/xml'));
  try {
    $m
      ->invoke($loader, self::$fixturesPath . '/xml/nonvalid.xml');
    $this
      ->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
    $this
      ->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'nonvalid.xml'), $e
      ->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
    $e = $e
      ->getPrevious();
    $this
      ->assertInstanceOf('InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
    $this
      ->assertStringStartsWith('[ERROR 1845] Element \'nonvalid\': No matching global declaration available for the validation root. (in', $e
      ->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
  }
  $xml = $m
    ->invoke($loader, self::$fixturesPath . '/xml/services1.xml');
  $this
    ->assertEquals('Symfony\\Component\\DependencyInjection\\SimpleXMLElement', get_class($xml), '->parseFile() returns an SimpleXMLElement object');
}