From 0f030d645c5fc9c2cb0ec129536e53eac58ae516 Mon Sep 17 00:00:00 2001 From: Iltar van der Berg Date: Mon, 30 Mar 2015 09:02:31 +0200 Subject: [PATCH] Added the trait variant of the configuration tester --- PhpUnit/AbstractConfigurationTestCase.php | 3 + PhpUnit/ConfigurationTestCaseTrait.php | 76 ++++++++++ README.md | 29 +++- .../AbstractConfigurationTestCaseTest.php | 3 + .../ConfigurationTestCaseTraitActual.php | 134 ++++++++++++++++++ .../ConfigurationTestCaseTraitTest.php | 6 + 6 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 PhpUnit/ConfigurationTestCaseTrait.php create mode 100644 Tests/PhpUnit/ConfigurationTestCaseTraitActual.php create mode 100644 Tests/PhpUnit/ConfigurationTestCaseTraitTest.php diff --git a/PhpUnit/AbstractConfigurationTestCase.php b/PhpUnit/AbstractConfigurationTestCase.php index 27994dd..cdbe2d8 100644 --- a/PhpUnit/AbstractConfigurationTestCase.php +++ b/PhpUnit/AbstractConfigurationTestCase.php @@ -5,6 +5,9 @@ /** * Extend your test case from this abstract class to test a class that implements * Symfony\Component\Config\Definition\ConfigurationInterface + * + * @deprecated only use this class if you're still running php 5.3. Use + * Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait instead */ abstract class AbstractConfigurationTestCase extends \PHPUnit_Framework_TestCase { diff --git a/PhpUnit/ConfigurationTestCaseTrait.php b/PhpUnit/ConfigurationTestCaseTrait.php new file mode 100644 index 0000000..36fdd63 --- /dev/null +++ b/PhpUnit/ConfigurationTestCaseTrait.php @@ -0,0 +1,76 @@ +=4.3.0, you need to set useRegExp to true if you'd like + * to match the exception message using a regular expression. + * + * @param array $configurationValues + * @param string|null $expectedMessage + * @param bool $useRegExp + */ + protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null, $useRegExp = false) + { + \PHPUnit_Framework_TestCase::assertThat( + $configurationValues, + new ConfigurationValuesAreInvalidConstraint( + $this->getConfiguration(), + $expectedMessage, + $useRegExp + ) + ); + } + + /** + * Assert that the given configuration values are valid. + * + * @param array $configurationValues + */ + protected function assertConfigurationIsValid(array $configurationValues) + { + \PHPUnit_Framework_TestCase::assertThat( + $configurationValues, + new ConfigurationValuesAreValidConstraint( + $this->getConfiguration() + ) + ); + } + + /** + * Assert that the given configuration values, when processed, will equal to the given array + * + * @param array $configurationValues + * @param array $expectedProcessedConfiguration + */ + protected function assertProcessedConfigurationEquals( + array $configurationValues, + array $expectedProcessedConfiguration + ) { + \PHPUnit_Framework_TestCase::assertThat( + $expectedProcessedConfiguration, + new ProcessedConfigurationEqualsConstraint( + $this->getConfiguration(), + $configurationValues + ) + ); + } +} diff --git a/README.md b/README.md index b90844b..9f15796 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,27 @@ Using Composer: ## Usage +### php 5.4 and up + +Create a test case and use the trait from ``Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait``. +Then implement ``getConfiguration()``: + +```php +assertConfigurationIsInvalid( @@ -85,8 +108,10 @@ You may also want to verify that after processing an array of configuration valu ```php assertProcessedConfigurationEquals(array( diff --git a/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php b/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php index 52d3a09..d751a19 100644 --- a/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php +++ b/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php @@ -5,6 +5,9 @@ use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase; use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue; +/** + * @deprecated should be removed once the minimum php version is elevated to at least 5.4 + */ class AbstractConfigurationTestCaseTest extends AbstractConfigurationTestCase { protected function getConfiguration() diff --git a/Tests/PhpUnit/ConfigurationTestCaseTraitActual.php b/Tests/PhpUnit/ConfigurationTestCaseTraitActual.php new file mode 100644 index 0000000..fd706db --- /dev/null +++ b/Tests/PhpUnit/ConfigurationTestCaseTraitActual.php @@ -0,0 +1,134 @@ +assertConfigurationIsInvalid( + array( + array() // no configuration values + ), + 'required_value' + ); + } + + /** + * @test + */ + public function it_fails_when_a_configuration_is_valid_when_it_should_have_been_invalid() + { + $this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'invalid'); + + $this->assertConfigurationIsInvalid( + array( + array('required_value' => 'some value') + ) + ); + } + + /** + * @test + */ + public function it_can_assert_that_a_configuration_is_valid() + { + $this->assertConfigurationIsValid( + array( + array('required_value' => 'some value') + ) + ); + } + + /** + * @test + */ + public function it_fails_when_a_configuration_is_invalid_when_it_should_have_been_valid() + { + $this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'valid'); + + $this->assertConfigurationIsValid( + array( + array() + ) + ); + } + + /** + * @test + */ + public function it_can_assert_that_a_processed_configuration_matches_the_expected_array_of_values() + { + $value = 'some value'; + + $this->assertProcessedConfigurationEquals( + array( + array(), + array('required_value' => $value) + ), + array( + 'required_value' => $value + ) + ); + } + + /** + * @test + */ + public function it_fails_when_a_processed_configuration_does_not_match_the_expected_array_of_values() + { + $value = 'some value'; + + $this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'equal'); + $this->assertProcessedConfigurationEquals( + array( + array('required_value' => $value) + ), + array( + 'invalid_key' => 'invalid_value' + ) + ); + } + + /** + * @test + */ + public function it_throws_a_comparison_failed_exception_with_the_values_in_the_right_order() + { + $value = 'some value'; + + //$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'equal'); + $configurationValues = array( + array('required_value' => $value) + ); + + $expectedProcessedConfigurationValues = array( + 'invalid_key' => 'invalid_value' + ); + + try { + $this->assertProcessedConfigurationEquals( + $configurationValues, + $expectedProcessedConfigurationValues + ); + } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { + $this->assertSame( + $expectedProcessedConfigurationValues, + $exception->getComparisonFailure()->getExpected() + ); + } + } +} diff --git a/Tests/PhpUnit/ConfigurationTestCaseTraitTest.php b/Tests/PhpUnit/ConfigurationTestCaseTraitTest.php new file mode 100644 index 0000000..ec9c12b --- /dev/null +++ b/Tests/PhpUnit/ConfigurationTestCaseTraitTest.php @@ -0,0 +1,6 @@ + 50400) { + require_once 'ConfigurationTestCaseTraitActual.php'; +}