-
Notifications
You must be signed in to change notification settings - Fork 20
[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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/Symfony/Cmf/Component/Testing/Unit/ConfigurationTestCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.