Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ExceptionMessage constraint strict, expectedException can handle only expected class, not message nor code #3326

10 changes: 3 additions & 7 deletions src/Framework/Constraint/ExceptionMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function __construct($expected)
*/
protected function matches($other)
{
if ($this->expectedMessage === '') {
return $other->getMessage() === '';
}

return \strpos($other->getMessage(), $this->expectedMessage) !== false;
return $other->getMessage() === $this->expectedMessage;
}

/**
Expand All @@ -63,7 +59,7 @@ protected function failureDescription($other)
}

return \sprintf(
"exception message '%s' contains '%s'",
"exception message '%s' equals '%s'",
$other->getMessage(),
$this->expectedMessage
);
Expand All @@ -78,6 +74,6 @@ public function toString()
return 'exception message is empty';
}

return 'exception message contains ';
return 'exception message equals ';
}
}
10 changes: 3 additions & 7 deletions src/Util/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Test
{
const REGEX_DATA_PROVIDER = '/@dataProvider\s+([a-zA-Z0-9._:-\\\\x7f-\xff]+)/';
const REGEX_TEST_WITH = '/@testWith\s+/';
const REGEX_EXPECTED_EXCEPTION = '(@expectedException\s+([:.\w\\\\x7f-\xff]+)(?:[\t ]+(\S*))?(?:[\t ]+(\S*))?\s*$)m';
const REGEX_EXPECTED_EXCEPTION = '(@expectedException\s+([:.\w\\\\x7f-\xff]+)\s*$)m';
const REGEX_REQUIRES_VERSION = '/@requires\s+(?P<name>PHP(?:Unit)?)\s+(?P<operator>[<>=!]{0,2})\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m';
const REGEX_REQUIRES_VERSION_CONSTRAINT = '/@requires\s+(?P<name>PHP(?:Unit)?)\s+(?P<constraint>[\d\t -.|~^]+)[ \t]*\r?$/m';
const REGEX_REQUIRES_OS = '/@requires\s+(?P<name>OS(?:FAMILY)?)\s+(?P<value>.+?)[ \t]*\r?$/m';
Expand Down Expand Up @@ -374,9 +374,7 @@ public static function getExpectedException($className, $methodName)
$message = '';
$messageRegExp = '';

if (isset($matches[2])) {
$message = \trim($matches[2]);
} elseif (isset($annotations['method']['expectedExceptionMessage'])) {
if (isset($annotations['method']['expectedExceptionMessage'])) {
$message = self::parseAnnotationContent(
$annotations['method']['expectedExceptionMessage'][0]
);
Expand All @@ -388,9 +386,7 @@ public static function getExpectedException($className, $methodName)
);
}

if (isset($matches[3])) {
$code = $matches[3];
} elseif (isset($annotations['method']['expectedExceptionCode'])) {
if (isset($annotations['method']['expectedExceptionCode'])) {
$code = self::parseAnnotationContent(
$annotations['method']['expectedExceptionCode'][0]
);
Expand Down
7 changes: 0 additions & 7 deletions tests/_files/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ public function testFour()
{
}

/**
* @expectedException Class Message 1234
*/
public function testFive()
{
}

/**
* @expectedException Class
* @expectedExceptionMessage Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function testMessageXdebugScreamCompatibility()
}

/**
* @expectedException \Exception variadic
* @expectedException \Exception
* @expectedExceptionMessage A variadic exception message
* @expectedExceptionMessageRegExp /^A variadic \w+ message/
*/
public function testSimultaneousLiteralAndRegExpExceptionMessage()
Expand Down
27 changes: 0 additions & 27 deletions tests/unit/Framework/Constraint/ExceptionMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,4 @@ public function testLiteralMessage()
{
throw new \Exception('A literal exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage A partial
*/
public function testPartialMessageBegin()
{
throw new \Exception('A partial exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage partial exception
*/
public function testPartialMessageMiddle()
{
throw new \Exception('A partial exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage exception message
*/
public function testPartialMessageEnd()
{
throw new \Exception('A partial exception message');
}
}
2 changes: 1 addition & 1 deletion tests/unit/Framework/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function testExceptionWithWrongMessage()
$this->assertEquals(1, $result->failureCount());
$this->assertCount(1, $result);
$this->assertEquals(
"Failed asserting that exception message 'A runtime error occurred' contains 'A logic error occurred'.",
"Failed asserting that exception message 'A runtime error occurred' equals 'A logic error occurred'.",
$test->getStatusMessage()
);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/Util/TestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public function testGetExpectedException()
Test::getExpectedException(\ExceptionTest::class, 'testFour')
);

$this->assertArraySubset(
['class' => 'Class', 'code' => 1234, 'message' => 'Message'],
Test::getExpectedException(\ExceptionTest::class, 'testFive')
);

$this->assertArraySubset(
['class' => 'Class', 'code' => 1234, 'message' => 'Message'],
Test::getExpectedException(\ExceptionTest::class, 'testSix')
Expand Down