Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
9 changes: 7 additions & 2 deletions PhpUnit/AbstractConfigurationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
Expand Down
23 changes: 19 additions & 4 deletions PhpUnit/ConfigurationValuesAreInvalidConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

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

👍 elegant

}

return $constraint->evaluate($exception, $description, $returnResult);
return new \PHPUnit_Framework_Constraint_ExceptionMessage($this->expectedMessage);
Copy link

Choose a reason for hiding this comment

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

leave a line break before the return?

}
}
21 changes: 21 additions & 0 deletions Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down