Skip to content

Commit

Permalink
Split up test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Frömer committed Mar 13, 2023
1 parent 07310a9 commit 126397d
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 268 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ analyse: ## Run phpstan analyse

cs: ## Run php cs
docker compose run php$(PHP_VERSION) vendor/bin/phpcs
docker compose run php$(PHP_VERSION) composer normalize --dry-run

csfix: ## Run phpcs fixer
docker compose run php$(PHP_VERSION) vendor/bin/phpcbf
docker compose run php$(PHP_VERSION) composer normalize

box: ## Compile /build/composer-unused.phar
docker compose run php$(PHP_VERSION) php box.phar compile
Expand Down
52 changes: 27 additions & 25 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
{
"name": "composer-unused/symbol-parser",
"type": "library",
"description": "Toolkit to parse symbols from a composer package",
"license": "MIT",
"type": "library",
"keywords": [
"composer",
"symbol",
"parser"
],
"homepage": "https://github.com/composer-unused/symbol-parser",
"license": "MIT",
"authors": [
{
"name": "Andreas Frömer",
"email": "composer-unused@icanhazstring.com"
}
],
"homepage": "https://github.com/composer-unused/symbol-parser",
"support": {
"issues": "https://github.com/composer-unused/symbol-parser/issues",
"source": "https://github.com/composer-unused/symbol-parser"
},
"funding": [
{
"type": "other",
"url": "https://paypal.me/icanhazstring"
},
{
"type": "github",
"url": "https://github.com/sponsors/icanhazstring"
}
],
"require": {
"php": "^7.4 || ^8.0",
"composer-unused/contracts": "^0.1 || ^0.2",
Expand All @@ -26,28 +40,30 @@
},
"require-dev": {
"ext-ds": "*",
"ergebnis/composer-normalize": "^2.30",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6.5",
"roave/security-advisories": "dev-master",
"squizlabs/php_codesniffer": "^3.7.2",
"symfony/serializer": "^5.4"
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"psr-4": {
"ComposerUnused\\SymbolParser\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"ComposerUnused\\SymbolParser\\Test\\Unit\\": "tests/Unit",
"ComposerUnused\\SymbolParser\\Test\\Integration\\": "tests/Integration",
"ComposerUnused\\SymbolParser\\Test\\Stubs\\": "tests/Stubs"
"ComposerUnused\\SymbolParser\\Test\\": "tests/"
}
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
},
"preferred-install": "dist",
"sort-packages": true
},
"scripts": {
"analyse": "phpstan analyse --no-progress",
"check": [
Expand All @@ -65,19 +81,5 @@
"cs-check": "Use \"phpcs\" to check the coding convention. See phpcs.xml",
"cs-fix": "Use \"phpcbf\" to fix the coding convention. See phpcs.xml",
"test": "Use \"phpunit\" to run the tests. See phpunit.xml"
},
"support": {
"issues": "https://github.com/composer-unused/symbol-parser/issues",
"source": "https://github.com/composer-unused/symbol-parser"
},
"funding": [
{
"type": "other",
"url": "https://paypal.me/icanhazstring"
},
{
"type": "github",
"url": "https://github.com/sponsors/icanhazstring"
}
]
}
}
8 changes: 2 additions & 6 deletions src/Parser/PHP/Strategy/AnnotationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public function __construct(

public function canHandle(Node $node): bool
{
if ($node->getDocComment() === null) {
return false;
}

return true;
return $node->getDocComment() !== null;
}

/**
Expand All @@ -64,7 +60,7 @@ public function extractSymbolNames(Node $node): array

$phpDocTagNodes = array_filter(
$phpDoc->children,
function (PhpDocChildNode $node): bool {
static function (PhpDocChildNode $node): bool {
return $node instanceof PhpDocTagNode;
}
);
Expand Down
42 changes: 42 additions & 0 deletions tests/ParserTestCase.php
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 tests/Unit/Parser/PHP/Strategy/AnnotationStrategyTest.php
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);
}
}
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]);
}
}
Loading

0 comments on commit 126397d

Please sign in to comment.