diff --git a/src/Mage/MageApplication.php b/src/Mage/MageApplication.php index fc99b3b..57a8e69 100644 --- a/src/Mage/MageApplication.php +++ b/src/Mage/MageApplication.php @@ -20,7 +20,8 @@ use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Console\Event\ConsoleExceptionEvent; use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\Console\Application; -use Symfony\Component\Yaml\Yaml; +use Symfony\Component\Yaml\Parser; +use Symfony\Component\Yaml\Exception\ParseException; use ReflectionClass; use Mage\Runtime\Exception\RuntimeException; @@ -62,7 +63,13 @@ class MageApplication extends Application throw new RuntimeException(sprintf('The file "%s" does not exists or is not readable.', $file)); } - $config = Yaml::parse(file_get_contents($file)); + try { + $parser = new Parser(); + $config = $parser->parse(file_get_contents($file)); + } catch (ParseException $exception) { + throw new RuntimeException(sprintf('Error parsing the file "%s".', $file)); + } + if (array_key_exists('magephp', $config)) { $config = $config['magephp']; @@ -80,9 +87,10 @@ class MageApplication extends Application $this->runtime->setLogger($logger); $this->loadBuiltInCommands(); - } else { - throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $file)); + return true; } + + throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $file)); } /** diff --git a/src/Mage/Tests/MageApplicationTest.php b/src/Mage/Tests/MageApplicationTest.php index 834ab3f..265b5e9 100644 --- a/src/Mage/Tests/MageApplicationTest.php +++ b/src/Mage/Tests/MageApplicationTest.php @@ -36,6 +36,17 @@ class MageApplicationTest extends TestCase } } + public function testParserError() + { + try { + $application = new MageApplication(); + $application->configure(__DIR__ . '/Resources/invalid-yaml.yml'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof RuntimeException); + $this->assertEquals(sprintf('Error parsing the file "%s".', __DIR__ . '/Resources/invalid-yaml.yml'), $exception->getMessage()); + } + } + public function testInvalidFile() { try { diff --git a/src/Mage/Tests/Resources/invalid-yaml.yml b/src/Mage/Tests/Resources/invalid-yaml.yml new file mode 100644 index 0000000..f46bb13 --- /dev/null +++ b/src/Mage/Tests/Resources/invalid-yaml.yml @@ -0,0 +1,6 @@ +magephp: + environments: + hosts: + - host1 + - host2 + - host3