-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andreas Frömer
committed
Mar 13, 2023
1 parent
07310a9
commit 126397d
Showing
9 changed files
with
382 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\SymbolParser\Test; | ||
|
||
use ComposerUnused\SymbolParser\Parser\PHP\ConsumedSymbolCollector; | ||
use ComposerUnused\SymbolParser\Parser\PHP\DefinedSymbolCollector; | ||
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\StrategyInterface; | ||
use ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser; | ||
use PhpParser\ParserFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ParserTestCase extends TestCase | ||
{ | ||
/** | ||
* @param array<StrategyInterface> $strategies | ||
* @return array<string> | ||
*/ | ||
public function parseConsumedSymbols(array $strategies, string $code): array | ||
{ | ||
$symbolNameParser = new SymbolNameParser( | ||
(new ParserFactory())->create(ParserFactory::ONLY_PHP7), | ||
new ConsumedSymbolCollector($strategies) | ||
); | ||
|
||
return iterator_to_array($symbolNameParser->parseSymbolNames($code)); | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function parseDefinedSymbols(string $code): array | ||
{ | ||
$symbolNameParser = new SymbolNameParser( | ||
(new ParserFactory())->create(ParserFactory::ONLY_PHP7), | ||
new DefinedSymbolCollector() | ||
); | ||
|
||
return iterator_to_array($symbolNameParser->parseSymbolNames($code)); | ||
} | ||
} |
177 changes: 177 additions & 0 deletions
177
tests/Unit/Parser/PHP/Strategy/AnnotationStrategyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\SymbolParser\Test\Unit\Parser\PHP\Strategy; | ||
|
||
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\AnnotationStrategy; | ||
use ComposerUnused\SymbolParser\Test\ParserTestCase; | ||
use PHPStan\PhpDocParser\Lexer\Lexer; | ||
use PHPStan\PhpDocParser\Parser\ConstExprParser; | ||
|
||
final class AnnotationStrategyTest extends ParserTestCase | ||
{ | ||
private AnnotationStrategy $strategy; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->strategy = new AnnotationStrategy( | ||
new ConstExprParser(), | ||
new Lexer() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldParseAnnotations(): void | ||
{ | ||
$code = <<<CODE | ||
<?php | ||
namespace Test; | ||
/** | ||
* @My\Namespace\Lorem(x=100, y="foo") | ||
* @mixin My\Namespace\Ipsum | ||
* @property My\Namespace\Dolor \$foo | ||
* @property-read My\Namespace\Sit \$foo | ||
* @property-write My\Namespace\Amet \$foo | ||
* @method My\Namespace\Consetetur lorem(My\Namespace\Sadipscing \$a, My\Namespace\Elitr \$b) | ||
*/ | ||
final class MyClass | ||
{ | ||
/** @My\Namespace\Sed */ | ||
private int \$a; | ||
/** @var My\Namespace\Diam */ | ||
private \$b; | ||
/** @My\Namespace\Nonumy */ | ||
public function ipsum(): void {} | ||
/** @param My\Namespace\Eirmod \$z */ | ||
public function dolor(\$c): void {} | ||
/** @return My\Namespace\Tempor|My\Namespace\Invidunt */ | ||
public function sit() {} | ||
/** @return My\Namespace\Ut&My\Namespace\Labore */ | ||
public function amet() {} | ||
/** @return My\Namespace\Et<My\Namespace\Dolore > */ | ||
public function consetetur() {} | ||
/** @return My\Namespace\Magna[] */ | ||
public function sadipscing() {} | ||
/** @return array{'foo': My\Namespace\Aliquyam} */ | ||
public function elitr() {} | ||
/** @return callable(My\Namespace\Erat): My\Namespace\Voluptua */ | ||
public function sed() {} | ||
/** @return (\$x is My\Namespace\At ? My\Namespace\Vero : My\Namespace\Eos) */ | ||
public function diam(\$x) {} | ||
/** @return ?My\Namespace\Accusam */ | ||
public function nonumy() {} | ||
/** @return My\Namespace\Justo[My\Namespace\Dolores] */ | ||
public function eirmod() {} | ||
} | ||
CODE; | ||
|
||
self::assertSame( | ||
[ | ||
'My\Namespace\Lorem', | ||
'My\Namespace\Ipsum', | ||
'My\Namespace\Dolor', | ||
'My\Namespace\Sit', | ||
'My\Namespace\Amet', | ||
'My\Namespace\Consetetur', | ||
'My\Namespace\Sadipscing', | ||
'My\Namespace\Elitr', | ||
'My\Namespace\Sed', | ||
'My\Namespace\Diam', | ||
'My\Namespace\Nonumy', | ||
'My\Namespace\Eirmod', | ||
'My\Namespace\Tempor', | ||
'My\Namespace\Invidunt', | ||
'My\Namespace\Ut', | ||
'My\Namespace\Labore', | ||
'My\Namespace\Et', | ||
'My\Namespace\Dolore', | ||
'My\Namespace\Magna', | ||
'My\Namespace\Aliquyam', | ||
'My\Namespace\Voluptua', | ||
'My\Namespace\Erat', | ||
'My\Namespace\At', | ||
'My\Namespace\Vero', | ||
'My\Namespace\Eos', | ||
'My\Namespace\Accusam', | ||
'My\Namespace\Justo', | ||
'My\Namespace\Dolores', | ||
], | ||
$this->parseConsumedSymbols([$this->strategy], $code) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* @see https://github.com/composer-unused/symbol-parser/issues/66 | ||
*/ | ||
public function refTestIssue66(): void | ||
{ | ||
$code = <<<CODE | ||
<?php | ||
declare(strict_types=1); | ||
namespace App; | ||
final class SomeClass | ||
{ | ||
/** | ||
* @return array{ | ||
* KeyName1?: non-empty-list<non-empty-string>, | ||
* KeyName2?: non-empty-list<non-empty-string>, | ||
* KeyName3?: non-empty-list<non-empty-string>, | ||
* KeyName4?: non-empty-list<non-empty-string>, | ||
* KeyName5?: non-empty-list<non-empty-string>, | ||
* KeyName6?: non-empty-list<non-empty-string>, | ||
* KeyName7?: non-empty-list<non-empty-string>, | ||
* KeyName8?: non-empty-list<non-empty-string>, | ||
* KeyName9?: non-empty-list<non-empty-string>, | ||
* KeyName10?: non-empty-list<non-empty-string>, | ||
* KeyName11?: non-empty-list<non-empty-string>, | ||
* KeyName12?: non-empty-list<non-empty-string>, | ||
* KeyName13?: non-empty-list<non-empty-string>, | ||
* KeyName14?: non-empty-string, | ||
* KeyName15?: non-empty-string, | ||
* KeyName16?: non-empty-string, | ||
* KeyName17?: non-empty-string, | ||
* KeyName18?: non-empty-string, | ||
* KeyName19?: non-empty-string, | ||
* KeyName20?: non-empty-string, | ||
* KeyName21?: non-empty-string, | ||
* KeyName22?: non-empty-string, | ||
* KeyName23?: non-empty-string, | ||
* KeyName24?: non-empty-string, | ||
* KeyName25?: non-empty-string, | ||
* KeyName26?: non-empty-string | ||
* } | ||
*/ | ||
public function someMethod(): array | ||
{ | ||
return []; | ||
} | ||
} | ||
CODE; | ||
|
||
$symbols = $this->parseConsumedSymbols([$this->strategy], $code); | ||
|
||
self::assertSame(['non-empty-list', 'non-empty-string'], $symbols); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Unit/Parser/PHP/Strategy/FullQualifiedParameterStrategyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ComposerUnused\SymbolParser\Test\Unit\Parser\PHP\Strategy; | ||
|
||
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\FullQualifiedParameterStrategy; | ||
use ComposerUnused\SymbolParser\Test\ParserTestCase; | ||
|
||
final class FullQualifiedParameterStrategyTest extends ParserTestCase | ||
{ | ||
private FullQualifiedParameterStrategy $strategy; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->strategy = new FullQualifiedParameterStrategy(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldParseFQNFunctionParameter(): void | ||
{ | ||
$code = <<<CODE | ||
<?php | ||
namespace Testing; | ||
class Foo { | ||
public function __construct(private readonly \My\Namespace\Bar1 \$parameter) {} | ||
public function test(\My\Namespace\Bar2 \$parameter) {} | ||
} | ||
CODE; | ||
|
||
$symbols = $this->parseConsumedSymbols([$this->strategy], $code); | ||
|
||
self::assertCount(2, $symbols); | ||
self::assertSame('My\Namespace\Bar1', $symbols[0]); | ||
self::assertSame('My\Namespace\Bar2', $symbols[1]); | ||
} | ||
} |
Oops, something went wrong.