Skip to content

[WIP] Added Config Testing Case #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/" }
Expand Down
150 changes: 150 additions & 0 deletions src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Symfony\Cmf\Component\Testing\Unit;

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 <[email protected]>
*/
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these methods are what users implement in their testing code, so i think they really should have phpdoc on expected return values and semantics.


public function testYamlConfig()
{
$filenames = $this->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 testPhpConfig()
{
$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());
}
}

/**
* 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');
}

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());
}
}