diff --git a/.travis.yml b/.travis.yml index 7823b33..36f1a97 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,8 @@ env: - SYMFONY_VERSION=2.3.* PHPUNIT_VERSION=~4.0 - SYMFONY_VERSION=2.4.* PHPUNIT_VERSION=~4.0 - SYMFONY_VERSION=2.5.* PHPUNIT_VERSION=~4.0 + - SYMFONY_VERSION=2.5.* PHPUNIT_VERSION=~4.2 + - SYMFONY_VERSION=2.5.* PHPUNIT_VERSION=~4.3 before_script: - composer require --no-update "symfony/config:${SYMFONY_VERSION}" diff --git a/PhpUnit/AbstractConfigurationTestCase.php b/PhpUnit/AbstractConfigurationTestCase.php index ddfd020..27994dd 100644 --- a/PhpUnit/AbstractConfigurationTestCase.php +++ b/PhpUnit/AbstractConfigurationTestCase.php @@ -21,16 +21,21 @@ abstract protected function getConfiguration(); * * Optionally provide (part of) the exception message that you expect to receive. * + * When running PHPUnit >=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) + protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null, $useRegExp = false) { self::assertThat( $configurationValues, new ConfigurationValuesAreInvalidConstraint( $this->getConfiguration(), - $expectedMessage + $expectedMessage, + $useRegExp ) ); } diff --git a/PhpUnit/ConfigurationValuesAreInvalidConstraint.php b/PhpUnit/ConfigurationValuesAreInvalidConstraint.php index 51f8388..29e79bf 100644 --- a/PhpUnit/ConfigurationValuesAreInvalidConstraint.php +++ b/PhpUnit/ConfigurationValuesAreInvalidConstraint.php @@ -8,12 +8,14 @@ class ConfigurationValuesAreInvalidConstraint extends AbstractConfigurationConstraint { private $expectedMessage; + private $useRegExp; - public function __construct(ConfigurationInterface $configuration, $expectedMessage = null) + public function __construct(ConfigurationInterface $configuration, $expectedMessage = null, $useRegExp = false) { parent::__construct($configuration); $this->expectedMessage = $expectedMessage; + $this->useRegExp = $useRegExp; } public function evaluate($other, $description = '', $returnResult = false) @@ -50,9 +52,22 @@ private function evaluateException(\Exception $exception, $description, $returnR return true; } - // reuse the exception message constraint from PHPUnit itself - $constraint = new \PHPUnit_Framework_Constraint_ExceptionMessage($this->expectedMessage); + return $this->createPhpUnitConstraint() + ->evaluate($exception, $description, $returnResult); + } + + private function createPhpUnitConstraint() + { + // Matching by regular expression was added in PHPUnit 4.2.0 + if ($this->useRegExp && version_compare(\PHPUnit_Runner_Version::id(), '4.2.0', '<')) { + throw new \InvalidArgumentException('Currently installed PHPUnit version does not support matching exception messages by regular expression.'); + } + + // Matching by regular expression was moved to a separate constraint in PHPUnit 4.3.0 + if ($this->useRegExp && class_exists('PHPUnit_Framework_Constraint_ExceptionMessageRegExp')) { + return new \PHPUnit_Framework_Constraint_ExceptionMessageRegExp($this->expectedMessage); + } - return $constraint->evaluate($exception, $description, $returnResult); + return new \PHPUnit_Framework_Constraint_ExceptionMessage($this->expectedMessage); } } diff --git a/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php b/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php index 1d8e5e4..64724ea 100644 --- a/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php +++ b/Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php @@ -78,6 +78,27 @@ public function if_configuration_values_are_invalid_it_matches_when_exception_me $this->assertTrue($constraint->evaluate(array(array()), '', true)); } + /** + * @test + */ + public function if_configuration_values_are_invalid_it_matches_when_exception_message_is_right_according_to_regexp() + { + $constraint = new ConfigurationValuesAreInvalidConstraint( + new ConfigurationWithRequiredValue(), + '/required[_]{1}value/', + true // use regular expressions + ); + + if (version_compare(\PHPUnit_Runner_Version::id(), '4.2.0', '<')) { + $this->setExpectedException( + '\InvalidArgumentException', + 'does not support matching exception messages by regular expression' + ); + } + + $this->assertTrue($constraint->evaluate(array(array()), '', true)); + } + /** * @test */