From 36e225fe855c58070f93508573d3e8125eac3743 Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Sat, 16 Dec 2017 16:05:37 +0100 Subject: [PATCH] improve tests --- tests/PHPStan/Parser/PhpDocParserTest.php | 1280 ++++++++++++++++----- 1 file changed, 995 insertions(+), 285 deletions(-) diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index aaa3d86..9aed27f 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -11,7 +11,9 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; +use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; +use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; @@ -36,307 +38,1010 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase /** - * @dataProvider provideParseData + * @dataProvider provideParamTagsData + * @dataProvider provideVarTagsData + * @dataProvider provideReturnTagsData + * @dataProvider provideThrowsTagsData + * @dataProvider providePropertyTagsData + * @dataProvider provideSingleLinePhpDocData + * @dataProvider provideMultiLinePhpDocData + * @param string $label * @param string $input * @param PhpDocNode $expectedPhpDocNode * @param int $nextTokenType */ - public function testParse(string $input, PhpDocNode $expectedPhpDocNode, int $nextTokenType = Lexer::TOKEN_END) + public function testParse(string $label, string $input, PhpDocNode $expectedPhpDocNode, int $nextTokenType = Lexer::TOKEN_END) { $tokens = new TokenIterator($this->lexer->tokenize($input)); $actualPhpDocNode = $this->phpDocParser->parse($tokens); - $this->assertEquals($expectedPhpDocNode, $actualPhpDocNode); + $this->assertEquals($expectedPhpDocNode, $actualPhpDocNode, $label); $this->assertSame((string) $expectedPhpDocNode, (string) $actualPhpDocNode); $this->assertSame($nextTokenType, $tokens->currentTokenType()); } - public function provideParseData(): array + public function provideParamTagsData(): iterable + { + yield [ + 'OK without description', + '/** @param Foo $foo */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new ParamTagValueNode( + new IdentifierTypeNode('Foo'), + false, + '$foo', + '' + ) + ), + ]), + ]; + + yield [ + 'OK with description', + '/** @param Foo $foo optional description */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new ParamTagValueNode( + new IdentifierTypeNode('Foo'), + false, + '$foo', + 'optional description' + ) + ), + ]), + ]; + + yield [ + 'OK variadic without description', + '/** @param Foo ...$foo */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new ParamTagValueNode( + new IdentifierTypeNode('Foo'), + true, + '$foo', + '' + ) + ), + ]), + ]; + + yield [ + 'OK variadic with description', + '/** @param Foo ...$foo optional description */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new ParamTagValueNode( + new IdentifierTypeNode('Foo'), + true, + '$foo', + 'optional description' + ) + ), + ]), + ]; + + yield [ + 'invalid without type, parameter name and description', + '/** @param */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new InvalidTagValueNode( + '', + new \PHPStan\PhpDocParser\Parser\ParserException( + '*/', + Lexer::TOKEN_CLOSE_PHPDOC, + 11, + Lexer::TOKEN_IDENTIFIER + ) + ) + ), + ]), + ]; + + yield [ + 'invalid without type and parameter name and with description (1)', + '/** @param #desc */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new InvalidTagValueNode( + '#desc', + new \PHPStan\PhpDocParser\Parser\ParserException( + '#desc', + Lexer::TOKEN_OTHER, + 11, + Lexer::TOKEN_IDENTIFIER + ) + ) + ), + ]), + ]; + + yield [ + 'invalid without type and parameter name and with description (2)', + '/** @param (Foo */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new InvalidTagValueNode( + '(Foo', + new \PHPStan\PhpDocParser\Parser\ParserException( + '*/', + Lexer::TOKEN_CLOSE_PHPDOC, + 16, + Lexer::TOKEN_CLOSE_PARENTHESES + ) + ) + ), + ]), + ]; + + yield [ + 'invalid with broken type (1)', + '/** @param (Foo $foo */', + new PhpDocNode([ + new PhpDocTagNode( + '@param', + new InvalidTagValueNode( + '(Foo $foo', + new \PHPStan\PhpDocParser\Parser\ParserException( + '$foo', + Lexer::TOKEN_VARIABLE, + 16, + Lexer::TOKEN_CLOSE_PARENTHESES + ) + ) + ), + ]), + ]; + + yield [ + 'invalid with broken type (2)', + '/** @param Foo