Skip to content

Commit

Permalink
Closes #3332
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 19, 2018
1 parent 41d73a1 commit 17c09b3
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 229 deletions.
1 change: 1 addition & 0 deletions ChangeLog-8.0.md
Expand Up @@ -7,6 +7,7 @@ All notable changes of the PHPUnit 8.0 release series are documented in this fil
### Changed

* Implemented [#3288](https://github.com/sebastianbergmann/phpunit/issues/3288): The `void_return` fixer of php-cs-fixer is now in effect
* Implemented [#3332](https://github.com/sebastianbergmann/phpunit/issues/3332): Deprecate annotation(s) for expecting exceptions
* Implemented [#3338](https://github.com/sebastianbergmann/phpunit/issues/3338): Deprecate assertions (and helper methods) that operate on (non-public) attributes
* Implemented [#3341](https://github.com/sebastianbergmann/phpunit/issues/3341): Deprecate optional parameters of `assertEquals()` and `assertNotEquals()`
* Implemented [#3369](https://github.com/sebastianbergmann/phpunit/issues/3369): Deprecate `assertInternalType()` and `assertNotInternalType()`
Expand Down
2 changes: 2 additions & 0 deletions src/Framework/TestCase.php
Expand Up @@ -1599,6 +1599,8 @@ private function setExpectedExceptionFromAnnotation(): void
);

if ($expectedException !== false) {
$this->addWarning('The @expectedException, @expectedExceptionCode, @expectedExceptionMessage, and @expectedExceptionMessageRegExp annotations are deprecated. They will be removed in PHPUnit 9. Refactor your test to use expectException(), expectExceptionCode(), expectExceptionMessage(), or expectExceptionMessageRegExp() instead.');

$this->expectException($expectedException['class']);

if ($expectedException['code'] !== null) {
Expand Down
3 changes: 3 additions & 0 deletions src/Util/Test.php
Expand Up @@ -344,6 +344,9 @@ public static function getMissingRequirements(string $className, string $methodN
* Returns the expected exception for a test.
*
* @return array|false
*
* @deprecated
* @codeCoverageIgnore
*/
public static function getExpectedException(string $className, ?string $methodName)
{
Expand Down
32 changes: 0 additions & 32 deletions tests/end-to-end/regression/GitHub/244.phpt

This file was deleted.

65 changes: 0 additions & 65 deletions tests/end-to-end/regression/GitHub/244/Issue244Test.php

This file was deleted.

28 changes: 9 additions & 19 deletions tests/unit/Framework/Constraint/ExceptionMessageRegExpTest.php
Expand Up @@ -13,43 +13,33 @@

class ExceptionMessageRegExpTest extends TestCase
{
/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /^A polymorphic \w+ message/
*/
public function testRegexMessage(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessageRegExp('/^A polymorphic \w+ message/');

throw new \Exception('A polymorphic exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /^a poly[a-z]+ [a-zA-Z0-9_]+ me(s){2}age$/i
*/
public function testRegexMessageExtreme(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessageRegExp('/^a poly[a-z]+ [a-zA-Z0-9_]+ me(s){2}age$/i');

throw new \Exception('A polymorphic exception message');
}

/**
* @runInSeparateProcess
* @requires extension xdebug
* @expectedException \Exception
* @expectedExceptionMessageRegExp #Screaming preg_match#
*/
public function testMessageXdebugScreamCompatibility(): void
{
\ini_set('xdebug.scream', '1');

throw new \Exception('Screaming preg_match');
}
$this->expectException(\Exception::class);
$this->expectExceptionMessageRegExp('#Screaming preg_match#');

/**
* @expectedException \Exception variadic
* @expectedExceptionMessageRegExp /^A variadic \w+ message/
*/
public function testSimultaneousLiteralAndRegExpExceptionMessage(): void
{
throw new \Exception('A variadic exception message');
throw new \Exception('Screaming preg_match');
}
}
28 changes: 12 additions & 16 deletions tests/unit/Framework/Constraint/ExceptionMessageTest.php
Expand Up @@ -13,39 +13,35 @@

class ExceptionMessageTest extends TestCase
{
/**
* @expectedException \Exception
* @expectedExceptionMessage A literal exception message
*/
public function testLiteralMessage(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('A literal exception message');

throw new \Exception('A literal exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage A partial
*/
public function testPartialMessageBegin(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('A partial');

throw new \Exception('A partial exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage partial exception
*/
public function testPartialMessageMiddle(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('partial exception');

throw new \Exception('A partial exception message');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage exception message
*/
public function testPartialMessageEnd(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('exception message');

throw new \Exception('A partial exception message');
}
}
3 changes: 2 additions & 1 deletion tests/unit/Framework/TestCaseTest.php
Expand Up @@ -644,10 +644,11 @@ public function testCurrentWorkingDirectoryIsRestored(): void

/**
* @requires PHP 7
* @expectedException \TypeError
*/
public function testTypeErrorCanBeExpected(): void
{
$this->expectException(\TypeError::class);

$o = new \ClassWithScalarTypeDeclarations;
$o->foo(null, null);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/Framework/TestSuiteTest.php
Expand Up @@ -201,12 +201,11 @@ public function testDoNotSkipInheritedClass(): void
$this->assertCount(2, $result);
}

/**
* @expectedException PHPUnit\Framework\Exception
* @expectedExceptionMessage No valid test provided.
*/
public function testCreateTestForConstructorlessTestClass(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('No valid test provided.');

$reflection = $this->getMockBuilder(\ReflectionClass::class)
->setConstructorArgs([$this])
->getMock();
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/Util/JsonTest.php
Expand Up @@ -9,6 +9,7 @@
*/
namespace PHPUnit\Util;

use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;

class JsonTest extends TestCase
Expand Down Expand Up @@ -60,11 +61,12 @@ public function prettifyProvider(): array

/**
* @dataProvider prettifyExceptionProvider
* @expectedException \PHPUnit\Framework\Exception
* @expectedExceptionMessage Cannot prettify invalid json
*/
public function testPrettifyException($json): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Cannot prettify invalid json');

Json::prettify($json);
}

Expand Down
90 changes: 0 additions & 90 deletions tests/unit/Util/TestTest.php
Expand Up @@ -17,96 +17,6 @@

class TestTest extends TestCase
{
/**
* @todo Split up in separate tests
*/
public function testGetExpectedException(): void
{
$this->assertArraySubset(
['class' => 'FooBarBaz', 'code' => null, 'message' => ''],
Test::getExpectedException(\ExceptionTest::class, 'testOne')
);

$this->assertArraySubset(
['class' => 'Foo_Bar_Baz', 'code' => null, 'message' => ''],
Test::getExpectedException(\ExceptionTest::class, 'testTwo')
);

$this->assertArraySubset(
['class' => 'Foo\Bar\Baz', 'code' => null, 'message' => ''],
Test::getExpectedException(\ExceptionTest::class, 'testThree')
);

$this->assertArraySubset(
['class' => 'ほげ', 'code' => null, 'message' => ''],
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')
);

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

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

$this->assertArraySubset(
['class' => 'Class', 'code' => \ExceptionTest::ERROR_CODE, 'message' => \ExceptionTest::ERROR_MESSAGE],
Test::getExpectedException(\ExceptionTest::class, 'testNine')
);

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

$this->assertArraySubset(
['class' => 'Class', 'code' => \My\Space\ExceptionNamespaceTest::ERROR_CODE, 'message' => \My\Space\ExceptionNamespaceTest::ERROR_MESSAGE],
Test::getExpectedException(\My\Space\ExceptionNamespaceTest::class, 'testConstants')
);

// Ensure the Class::CONST expression is only evaluated when the constant really exists
$this->assertArraySubset(
['class' => 'Class', 'code' => 'ExceptionTest::UNKNOWN_CODE_CONSTANT', 'message' => 'ExceptionTest::UNKNOWN_MESSAGE_CONSTANT'],
Test::getExpectedException(\ExceptionTest::class, 'testUnknownConstants')
);

$this->assertArraySubset(
['class' => 'Class', 'code' => 'My\Space\ExceptionNamespaceTest::UNKNOWN_CODE_CONSTANT', 'message' => 'My\Space\ExceptionNamespaceTest::UNKNOWN_MESSAGE_CONSTANT'],
Test::getExpectedException(\My\Space\ExceptionNamespaceTest::class, 'testUnknownConstants')
);
}

public function testGetExpectedRegExp(): void
{
$this->assertArraySubset(
['message_regex' => '#regex#'],
Test::getExpectedException(\ExceptionTest::class, 'testWithRegexMessage')
);

$this->assertArraySubset(
['message_regex' => '#regex#'],
Test::getExpectedException(\ExceptionTest::class, 'testWithRegexMessageFromClassConstant')
);

$this->assertArraySubset(
['message_regex' => 'ExceptionTest::UNKNOWN_MESSAGE_REGEX_CONSTANT'],
Test::getExpectedException(\ExceptionTest::class, 'testWithUnknowRegexMessageFromClassConstant')
);
}

/**
* @dataProvider requirementsProvider
*
Expand Down

0 comments on commit 17c09b3

Please sign in to comment.