From 6bd5c062c9c24be6e7732272cceab3fea0035923 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Mon, 11 Nov 2013 16:57:43 +0100 Subject: [PATCH 1/3] Added SymfonyConfigTest as a dependency --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5d8dffb..80c232b 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "doctrine/phpcr-bundle": "1.0.*", "doctrine/data-fixtures": "1.0.*", "jackalope/jackalope": "1.0.*", - "jackalope/jackalope-doctrine-dbal": "1.0.*" + "jackalope/jackalope-doctrine-dbal": "1.0.*", + "matthiasnoback/symfony-config-test": "0.*" }, "autoload": { "psr-0": { "Symfony\\Cmf\\Component\\Testing": "src/" } From 3e71d93503a892678f977e37a896d1b83d51f4c9 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Mon, 11 Nov 2013 17:08:49 +0100 Subject: [PATCH 2/3] Added ConfigurationTestCase --- .../Testing/Unit/ConfigurationTestCase.php | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php diff --git a/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php b/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php new file mode 100644 index 0000000..5f68d3d --- /dev/null +++ b/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php @@ -0,0 +1,97 @@ +getFilenames(); + + if (!isset($filenames['yaml'])) { + $this->markTestSkipped('No Yaml configuration fixture configured'); + } + + $files = $filenames['yaml']; + if (!is_array($files)) { + $files = array($files); + } + + foreach ($files as $file) { + $this->assertProcessedConfigurationEquals($this->loadYamlFile($file), $this->getExpectedResult()); + } + } + + public function testXmlConfig() + { + $filenames = $this->getFilenames(); + + if (!isset($filenames['xml'])) { + $this->markTestSkipped('No XML configuration fixture configured'); + } + + $files = $filenames['xml']; + if (!is_array($files)) { + $files = array($files); + } + + foreach ($files as $file) { + $this->assertProcessedConfigurationEquals($this->loadXmlFile($file), $this->getExpectedResult()); + } + } + + public function testXmlConfig() + { + $filenames = $this->getFilenames(); + + if (!isset($filenames['php'])) { + $this->markTestSkipped('No PHP configuration fixture configured'); + } + + $files = $filenames['php']; + if (!is_array($files)) { + $files = array($files); + } + + foreach ($files as $file) { + $this->assertProcessedConfigurationEquals($this->loadPhpFile($file), $this->getExpectedResult()); + } + } + + protected function loadXmlFile($file) + { + return $this->doLoadFile($file, 'XmlFileLoader'); + } + + protected function loadYamlFile($file) + { + return $this->doLoadFile($file, 'YamlFileLoader'); + } + + protected function loadPhpFile($file) + { + return $this->doLoadFile($file, 'PhpFileLoader'); + } + + private function doLoadFile($file, $loader) + { + $container = new ContainerBuilder(); + + $extension = $this->getExtension(); + $container->registerExtension($extension); + + $loader = 'Symfony\Component\DependencyInjection\Loader\\'.$loader; + $loader = new $loader($container, new FileLocator(dirname($file))); + $loader->load(basename($file)); + + return $container->getExtensionConfig($extension->getAlias()); + } +} From 48ca0b7c8bf188d71dbdc58171dc9efa99849f3d Mon Sep 17 00:00:00 2001 From: Wouter J Date: Mon, 11 Nov 2013 18:58:09 +0100 Subject: [PATCH 3/3] Fixed comments --- .../Testing/Unit/ConfigurationTestCase.php | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php b/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php index 5f68d3d..cb3db0b 100644 --- a/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php +++ b/src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php @@ -5,11 +5,49 @@ use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; +/** + * A class to test the Configuration class of a bundle. + * + * @author Wouter J + */ abstract class ConfigurationTestCase extends AbstractConfigurationTestCase { + /** + * Returns the filenames to test. + * + * It should return an array with the format and its file: + * + * Array( + * [yaml] => 'path/to/fixtures/config.yml', + * [xml] => 'path/to/fixtures/config.xml', + * ) + * + * Supported formats are yaml, xml and php. + * + * It can also return multiple files per format to test: + * + * Array( + * [yaml] => array('config-1.yml', 'config-2.yml') + * ) + * + * @return array + */ abstract protected function getFilenames(); + + /** + * Returns an array with the expected result after parsing the config. + * + * @return array + */ abstract protected function getExpectedResult(); + + /** + * Returns the Extension class of the component. + * + * @return ExtensionInterface + */ abstract protected function getExtension(); public function testYamlConfig() @@ -48,7 +86,7 @@ public function testXmlConfig() } } - public function testXmlConfig() + public function testPhpConfig() { $filenames = $this->getFilenames(); @@ -66,16 +104,31 @@ public function testXmlConfig() } } + /** + * Loads a XML file. + * + * @param string $file The file name + */ protected function loadXmlFile($file) { return $this->doLoadFile($file, 'XmlFileLoader'); } + /** + * Loads a Yaml file. + * + * @param string $file The file name + */ protected function loadYamlFile($file) { return $this->doLoadFile($file, 'YamlFileLoader'); } + /** + * Loads a PHP file. + * + * @param string $file The file name + */ protected function loadPhpFile($file) { return $this->doLoadFile($file, 'PhpFileLoader');