|
8 | 8 | class ConfigurationValuesAreInvalidConstraint extends AbstractConfigurationConstraint |
9 | 9 | { |
10 | 10 | private $expectedMessage; |
| 11 | + private $useRegExp; |
11 | 12 |
|
12 | | - public function __construct(ConfigurationInterface $configuration, $expectedMessage = null) |
| 13 | + public function __construct(ConfigurationInterface $configuration, $expectedMessage = null, $useRegExp = false) |
13 | 14 | { |
14 | 15 | parent::__construct($configuration); |
15 | 16 |
|
16 | 17 | $this->expectedMessage = $expectedMessage; |
| 18 | + $this->useRegExp = $useRegExp; |
17 | 19 | } |
18 | 20 |
|
19 | 21 | public function evaluate($other, $description = '', $returnResult = false) |
@@ -50,9 +52,22 @@ private function evaluateException(\Exception $exception, $description, $returnR |
50 | 52 | return true; |
51 | 53 | } |
52 | 54 |
|
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 | + } |
55 | 70 |
|
56 | | - return $constraint->evaluate($exception, $description, $returnResult); |
| 71 | + return new \PHPUnit_Framework_Constraint_ExceptionMessage($this->expectedMessage); |
57 | 72 | } |
58 | 73 | } |
0 commit comments