Use methods instead of annotations for expected exceptions (#533)

This commit is contained in:
Tomáš Votruba 2018-09-22 10:43:54 +02:00 committed by Nikita Popov
parent c5316487a4
commit d638dd9b25
18 changed files with 94 additions and 188 deletions

View File

@ -123,39 +123,31 @@ DOC;
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
*/
public function testInvalidStmtError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
$this->createClassBuilder('Test')
->addStmt(new Stmt\Echo_([]))
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Doc comment must be a string or an instance of PhpParser\Comment\Doc
*/
public function testInvalidDocComment() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
$this->createClassBuilder('Test')
->setDocComment(new Comment('Test'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Name cannot be empty
*/
public function testEmptyName() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name cannot be empty');
$this->createClassBuilder('Test')
->extend('');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Name must be a string or an instance of Node\Name
*/
public function testInvalidName() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name must be a string or an instance of Node\Name');
$this->createClassBuilder('Test')
->extend(['Foo']);
}

View File

@ -92,29 +92,23 @@ class FunctionTest extends TestCase
], []), $node);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage void type cannot be nullable
*/
public function testInvalidNullableVoidType() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('void type cannot be nullable');
$this->createFunctionBuilder('test')->setReturnType('?void');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected parameter node, got "Name"
*/
public function testInvalidParamError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected parameter node, got "Name"');
$this->createFunctionBuilder('test')
->addParam(new Node\Name('foo'))
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected statement or expression node
*/
public function testAddNonStmt() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected statement or expression node');
$this->createFunctionBuilder('test')
->addStmt(new Node\Name('Test'));
}

View File

@ -79,11 +79,9 @@ class InterfaceTest extends TestCase
]), $node);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Unexpected node of type "Stmt_PropertyProperty"
*/
public function testInvalidStmtError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
$this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
}

View File

@ -135,33 +135,27 @@ class MethodTest extends TestCase
], []), $node);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot add statements to an abstract method
*/
public function testAddStmtToAbstractMethodError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot add statements to an abstract method');
$this->createMethodBuilder('test')
->makeAbstract()
->addStmt(new Print_(new String_('test')))
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot make method with statements abstract
*/
public function testMakeMethodWithStmtsAbstractError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot make method with statements abstract');
$this->createMethodBuilder('test')
->addStmt(new Print_(new String_('test')))
->makeAbstract()
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected parameter node, got "Name"
*/
public function testInvalidParamError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected parameter node, got "Name"');
$this->createMethodBuilder('test')
->addParam(new Node\Name('foo'))
;

View File

@ -129,19 +129,15 @@ class ParamTest extends TestCase
];
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Parameter type cannot be void
*/
public function testVoidTypeError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Parameter type cannot be void');
$this->createParamBuilder('test')->setType('void');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Type must be a string, or an instance of Name, Identifier or NullableType
*/
public function testInvalidTypeError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or NullableType');
$this->createParamBuilder('test')->setType(new \stdClass);
}

View File

@ -37,11 +37,9 @@ class TraitTest extends TestCase
]), $trait);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
*/
public function testInvalidStmtError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
$this->createTraitBuilder('Test')
->addStmt(new Stmt\Echo_([]))
;

View File

@ -55,65 +55,53 @@ class TraitUseAdaptationTest extends TestCase
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot set alias for not alias adaptation buider
*/
public function testAsOnNotAlias() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot set alias for not alias adaptation buider');
$this->createTraitUseAdaptationBuilder('Test', 'foo')
->insteadof('AnotherTrait')
->as('bar')
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot add overwritten traits for not precedence adaptation buider
*/
public function testInsteadofOnNotPrecedence() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot add overwritten traits for not precedence adaptation buider');
$this->createTraitUseAdaptationBuilder('Test', 'foo')
->as('bar')
->insteadof('AnotherTrait')
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Precedence adaptation must have trait
*/
public function testInsteadofWithoutTrait() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Precedence adaptation must have trait');
$this->createTraitUseAdaptationBuilder(null, 'foo')
->insteadof('AnotherTrait')
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot set access modifier for not alias adaptation buider
*/
public function testMakeOnNotAlias() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot set access modifier for not alias adaptation buider');
$this->createTraitUseAdaptationBuilder('Test', 'foo')
->insteadof('AnotherTrait')
->makePublic()
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Multiple access type modifiers are not allowed
*/
public function testMultipleMake() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Multiple access type modifiers are not allowed');
$this->createTraitUseAdaptationBuilder(null, 'foo')
->makePrivate()
->makePublic()
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Type of adaptation is not defined
*/
public function testUndefinedType() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Type of adaptation is not defined');
$this->createTraitUseAdaptationBuilder(null, 'foo')
->getNode()
;

View File

@ -45,11 +45,9 @@ class TraitUseTest extends TestCase
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Adaptation must have type TraitUseAdaptation
*/
public function testInvalidAdaptationNode() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Adaptation must have type TraitUseAdaptation');
$this->createTraitUseBuilder('Test')
->with(new Stmt\Echo_([]))
;

View File

@ -69,19 +69,15 @@ class BuilderFactoryTest extends TestCase
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected at least two expressions
*/
public function testConcatOneError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected at least two expressions');
(new BuilderFactory())->concat("a");
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected string or Expr
*/
public function testConcatInvalidExpr() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected string or Expr');
(new BuilderFactory())->concat("a", 42);
}
@ -218,35 +214,27 @@ class BuilderFactoryTest extends TestCase
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected string or instance of Node\Identifier
*/
public function testInvalidIdentifier() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected string or instance of Node\Identifier');
(new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected string or instance of Node\Identifier or Node\Expr
*/
public function testInvalidIdentifierOrExpr() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected string or instance of Node\Identifier or Node\Expr');
(new BuilderFactory())->staticCall('Foo', new Name('bar'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Name must be a string or an instance of Node\Name or Node\Expr
*/
public function testInvalidNameOrExpr() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name must be a string or an instance of Node\Name or Node\Expr');
(new BuilderFactory())->funcCall(new Node\Stmt\Return_());
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Variable name must be string or Expr
*/
public function testInvalidVar() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Variable name must be string or Expr');
(new BuilderFactory())->var(new Node\Stmt\Return_());
}

View File

@ -73,11 +73,9 @@ class ConstExprEvaluatorTest extends TestCase
];
}
/**
* @expectedException \PhpParser\ConstExprEvaluationException
* @expectedExceptionMessage Expression of type Expr_Variable cannot be evaluated
*/
public function testEvaluateFails() {
$this->expectException(ConstExprEvaluationException::class);
$this->expectExceptionMessage('Expression of type Expr_Variable cannot be evaluated');
$evaluator = new ConstExprEvaluator();
$evaluator->evaluateDirectly(new Expr\Variable('a'));
}

View File

@ -7,11 +7,9 @@ use PHPUnit\Framework\TestCase;
class ThrowingTest extends TestCase
{
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage Test
*/
public function testHandleError() {
$this->expectException(Error::class);
$this->expectExceptionMessage('Test');
$errorHandler = new Throwing();
$errorHandler->handleError(new Error('Test'));
}

View File

@ -94,11 +94,9 @@ class ErrorTest extends TestCase
}
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Invalid position information
*/
public function testInvalidPosInfo() {
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid position information');
$error = new Error('Some error', [
'startFilePos' => 10,
'endFilePos' => 11,

View File

@ -235,11 +235,9 @@ class LexerTest extends TestCase
];
}
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
*/
public function testHandleHaltCompilerError() {
$this->expectException(Error::class);
$this->expectExceptionMessage('__HALT_COMPILER must be followed by "();"');
$lexer = $this->getLexer();
$lexer->startLexing('<?php ... __halt_compiler invalid ();');

View File

@ -51,35 +51,27 @@ class NameTest extends TestCase
$this->assertNull($name->slice(-2, -2));
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Offset 4 is out of bounds
*/
public function testSliceOffsetTooLarge() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Offset 4 is out of bounds');
(new Name('foo\bar\baz'))->slice(4);
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Offset -4 is out of bounds
*/
public function testSliceOffsetTooSmall() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Offset -4 is out of bounds');
(new Name('foo\bar\baz'))->slice(-4);
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Length 4 is out of bounds
*/
public function testSliceLengthTooLarge() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Length 4 is out of bounds');
(new Name('foo\bar\baz'))->slice(0, 4);
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Length -4 is out of bounds
*/
public function testSliceLengthTooSmall() {
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Length -4 is out of bounds');
(new Name('foo\bar\baz'))->slice(0, -4);
}
@ -131,27 +123,21 @@ class NameTest extends TestCase
$this->assertSame('namespace\foo', $name->toCodeString());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected string, array of parts or Name instance
*/
public function testInvalidArg() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected string, array of parts or Name instance');
Name::concat('foo', new \stdClass);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Name cannot be empty
*/
public function testInvalidEmptyString() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Name cannot be empty');
new Name('');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Name cannot be empty
*/
public function testInvalidEmptyArray() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Name cannot be empty');
new Name([]);
}

View File

@ -98,11 +98,9 @@ OUT;
$this->assertSame($this->canonicalize($expected), $this->canonicalize($dump));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Can only dump nodes and arrays.
*/
public function testError() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can only dump nodes and arrays.');
$dumper = new NodeDumper;
$dumper->dump(new \stdClass);
}

View File

@ -117,11 +117,9 @@ class NodeTraverserTest extends TestCase
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Invalid node structure: Contains nested arrays
*/
public function testInvalidDeepArray() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Invalid node structure: Contains nested arrays');
$strNode = new String_('Foo');
$stmts = [[[$strNode]]];

View File

@ -13,29 +13,23 @@ abstract class ParserTest extends TestCase
/** @returns Parser */
abstract protected function getParser(Lexer $lexer);
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage Syntax error, unexpected EOF on line 1
*/
public function testParserThrowsSyntaxError() {
$this->expectException(Error::class);
$this->expectExceptionMessage('Syntax error, unexpected EOF on line 1');
$parser = $this->getParser(new Lexer());
$parser->parse('<?php foo');
}
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage Cannot use foo as self because 'self' is a special class name on line 1
*/
public function testParserThrowsSpecialError() {
$this->expectException(Error::class);
$this->expectExceptionMessage('Cannot use foo as self because \'self\' is a special class name on line 1');
$parser = $this->getParser(new Lexer());
$parser->parse('<?php use foo as self;');
}
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage Unterminated comment on line 1
*/
public function testParserThrowsLexerError() {
$this->expectException(Error::class);
$this->expectExceptionMessage('Unterminated comment on line 1');
$parser = $this->getParser(new Lexer());
$parser->parse('<?php /*');
}
@ -109,11 +103,9 @@ EOC;
], $var->getAttributes());
}
/**
* @expectedException \RangeException
* @expectedExceptionMessage The lexer returned an invalid token (id=999, value=foobar)
*/
public function testInvalidToken() {
$this->expectException(\RangeException::class);
$this->expectExceptionMessage('The lexer returned an invalid token (id=999, value=foobar)');
$lexer = new InvalidTokenLexer;
$parser = $this->getParser($lexer);
$parser->parse('dummy');

View File

@ -184,11 +184,9 @@ class PrettyPrinterTest extends CodeTestAbstract
];
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot pretty-print AST with Error nodes
*/
public function testPrettyPrintWithError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
$stmts = [new Stmt\Expression(
new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())
)];
@ -196,11 +194,9 @@ class PrettyPrinterTest extends CodeTestAbstract
$prettyPrinter->prettyPrint($stmts);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot pretty-print AST with Error nodes
*/
public function testPrettyPrintWithErrorInClassConstFetch() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
$stmts = [new Stmt\Expression(
new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())
)];
@ -208,11 +204,9 @@ class PrettyPrinterTest extends CodeTestAbstract
$prettyPrinter->prettyPrint($stmts);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot directly print EncapsedStringPart
*/
public function testPrettyPrintEncapsedStringPart() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot directly print EncapsedStringPart');
$expr = new Node\Scalar\EncapsedStringPart('foo');
$prettyPrinter = new PrettyPrinter\Standard;
$prettyPrinter->prettyPrintExpr($expr);