Skip to content

Commit 8d3a62a

Browse files
Merge pull request #14 from fvdb/add-regexp-exception-message
Ensured exception message regex matching keeps working with PHPUnit >=4.3.0
2 parents d9ccdb6 + 83f1b12 commit 8d3a62a

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ env:
1919
- SYMFONY_VERSION=2.3.* PHPUNIT_VERSION=~4.0
2020
- SYMFONY_VERSION=2.4.* PHPUNIT_VERSION=~4.0
2121
- SYMFONY_VERSION=2.5.* PHPUNIT_VERSION=~4.0
22+
- SYMFONY_VERSION=2.5.* PHPUNIT_VERSION=~4.2
23+
- SYMFONY_VERSION=2.5.* PHPUNIT_VERSION=~4.3
2224

2325
before_script:
2426
- composer require --no-update "symfony/config:${SYMFONY_VERSION}"

PhpUnit/AbstractConfigurationTestCase.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ abstract protected function getConfiguration();
2121
*
2222
* Optionally provide (part of) the exception message that you expect to receive.
2323
*
24+
* When running PHPUnit >=4.3.0, you need to set useRegExp to true if you'd like
25+
* to match the exception message using a regular expression.
26+
*
2427
* @param array $configurationValues
2528
* @param string|null $expectedMessage
29+
* @param bool $useRegExp
2630
*/
27-
protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null)
31+
protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null, $useRegExp = false)
2832
{
2933
self::assertThat(
3034
$configurationValues,
3135
new ConfigurationValuesAreInvalidConstraint(
3236
$this->getConfiguration(),
33-
$expectedMessage
37+
$expectedMessage,
38+
$useRegExp
3439
)
3540
);
3641
}

PhpUnit/ConfigurationValuesAreInvalidConstraint.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
class ConfigurationValuesAreInvalidConstraint extends AbstractConfigurationConstraint
99
{
1010
private $expectedMessage;
11+
private $useRegExp;
1112

12-
public function __construct(ConfigurationInterface $configuration, $expectedMessage = null)
13+
public function __construct(ConfigurationInterface $configuration, $expectedMessage = null, $useRegExp = false)
1314
{
1415
parent::__construct($configuration);
1516

1617
$this->expectedMessage = $expectedMessage;
18+
$this->useRegExp = $useRegExp;
1719
}
1820

1921
public function evaluate($other, $description = '', $returnResult = false)
@@ -50,9 +52,22 @@ private function evaluateException(\Exception $exception, $description, $returnR
5052
return true;
5153
}
5254

53-
// reuse the exception message constraint from PHPUnit itself
54-
$constraint = new \PHPUnit_Framework_Constraint_ExceptionMessage($this->expectedMessage);
55+
return $this->createPhpUnitConstraint()
56+
->evaluate($exception, $description, $returnResult);
57+
}
58+
59+
private function createPhpUnitConstraint()
60+
{
61+
// Matching by regular expression was added in PHPUnit 4.2.0
62+
if ($this->useRegExp && version_compare(\PHPUnit_Runner_Version::id(), '4.2.0', '<')) {
63+
throw new \InvalidArgumentException('Currently installed PHPUnit version does not support matching exception messages by regular expression.');
64+
}
65+
66+
// Matching by regular expression was moved to a separate constraint in PHPUnit 4.3.0
67+
if ($this->useRegExp && class_exists('PHPUnit_Framework_Constraint_ExceptionMessageRegExp')) {
68+
return new \PHPUnit_Framework_Constraint_ExceptionMessageRegExp($this->expectedMessage);
69+
}
5570

56-
return $constraint->evaluate($exception, $description, $returnResult);
71+
return new \PHPUnit_Framework_Constraint_ExceptionMessage($this->expectedMessage);
5772
}
5873
}

Tests/PhpUnit/ConfigurationValuesAreInvalidConstraintTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ public function if_configuration_values_are_invalid_it_matches_when_exception_me
7878
$this->assertTrue($constraint->evaluate(array(array()), '', true));
7979
}
8080

81+
/**
82+
* @test
83+
*/
84+
public function if_configuration_values_are_invalid_it_matches_when_exception_message_is_right_according_to_regexp()
85+
{
86+
$constraint = new ConfigurationValuesAreInvalidConstraint(
87+
new ConfigurationWithRequiredValue(),
88+
'/required[_]{1}value/',
89+
true // use regular expressions
90+
);
91+
92+
if (version_compare(\PHPUnit_Runner_Version::id(), '4.2.0', '<')) {
93+
$this->setExpectedException(
94+
'\InvalidArgumentException',
95+
'does not support matching exception messages by regular expression'
96+
);
97+
}
98+
99+
$this->assertTrue($constraint->evaluate(array(array()), '', true));
100+
}
101+
81102
/**
82103
* @test
83104
*/

0 commit comments

Comments
 (0)