Skip to content

Commit

Permalink
Use expectException*() instead of annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 21, 2019
1 parent 5b3488c commit f56cb26
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
26 changes: 12 additions & 14 deletions tests/unit/Framework/Constraint/ExceptionMessageRegExpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,41 @@

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');

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

throw new \Exception('Screaming preg_match');
}

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

throw new \Exception('A variadic exception message');
}
}
28 changes: 12 additions & 16 deletions tests/unit/Framework/Constraint/ExceptionMessageTest.php
Original file line number Diff line number Diff line change
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');
}
}
4 changes: 3 additions & 1 deletion tests/unit/Framework/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,13 @@ public function testCurrentWorkingDirectoryIsRestored(): void

/**
* @requires PHP 7
* @expectedException \TypeError
*/
public function testTypeErrorCanBeExpected(): void
{
$o = new \ClassWithScalarTypeDeclarations;

$this->expectException(\TypeError::class);

$o->foo(null, null);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Framework/TestSuiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ public function testDoNotSkipInheritedClass(): void
$this->assertCount(2, $result);
}

/**
* @expectedException PHPUnit\Framework\Exception
* @expectedExceptionMessage No valid test provided.
*/
public function testCreateTestForConstructorlessTestClass(): void
{
$reflection = $this->getMockBuilder(\ReflectionClass::class)
Expand All @@ -225,6 +221,10 @@ public function testCreateTestForConstructorlessTestClass(): void
$reflection->expects($this->once())
->method('getName')
->willReturn(__CLASS__);

$this->expectException(Exception::class);
$this->expectExceptionMessage('No valid test provided.');

TestSuite::createTest($reflection, 'TestForConstructorlessTestClass');
}

Expand Down
6 changes: 4 additions & 2 deletions tests/unit/Util/JsonTest.php
Original file line number Diff line number Diff line change
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

0 comments on commit f56cb26

Please sign in to comment.