symplify PhpDocParser printer test

This commit is contained in:
TomasVotruba 2020-04-03 12:56:28 +02:00
parent 593d19c786
commit 7ca1279216
34 changed files with 134 additions and 180 deletions

View File

@ -10,7 +10,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\TagAwareNodeInterface;
use Rector\Core\BetterPhpDocParser\Utils\ArrayItemStaticHelper;
use Rector\BetterPhpDocParser\Utils\ArrayItemStaticHelper;
abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpDocTagValueNode
{

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\Core\BetterPhpDocParser\Utils;
namespace Rector\BetterPhpDocParser\Utils;
use Nette\Utils\Strings;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser;
use PhpParser\Node;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
@ -41,14 +41,35 @@ abstract class AbstractPhpDocInfoTest extends AbstractKernelTestCase
$this->phpDocInfoPrinter = self::$container->get(PhpDocInfoPrinter::class);
}
protected function parseFileAndGetFirstNodeOfType(string $filePath, string $type): Node
/**
* @param class-string $nodeType
*/
protected function doTestPrintedPhpDocInfo(string $filePath, string $nodeType): void
{
$nodeWithPhpDocInfo = $this->parseFileAndGetFirstNodeOfType($filePath, $nodeType);
$docComment = $nodeWithPhpDocInfo->getDocComment();
if ($docComment === null) {
throw new ShouldNotHappenException(sprintf('Doc comments for "%s" file cannot not be empty', $filePath));
}
$originalDocCommentText = $docComment->getText();
$printedPhpDocInfo = $this->printNodePhpDocInfoToString($nodeWithPhpDocInfo);
$this->assertSame($originalDocCommentText, $printedPhpDocInfo);
}
/**
* @param class-string $nodeType
*/
private function parseFileAndGetFirstNodeOfType(string $filePath, string $nodeType): Node
{
$nodes = $this->fileInfoParser->parseFileInfoToNodesAndDecorate(new SmartFileInfo($filePath));
return $this->betterNodeFinder->findFirstInstanceOf($nodes, $type);
return $this->betterNodeFinder->findFirstInstanceOf($nodes, $nodeType);
}
protected function printNodePhpDocInfoToString(Node $node): string
private function printNodePhpDocInfoToString(Node $node): string
{
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser;
use Iterator;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\AbstractPhpDocInfoTest;
final class DoctrineOrmTagNodeTest extends AbstractPhpDocInfoTest
{
/**
* @dataProvider provideData()
* @param class-string $nodeType
*/
public function test(string $filePath, string $nodeType): void
{
$this->doTestPrintedPhpDocInfo($filePath, $nodeType);
}
public function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SomeEntity.php', Class_::class];
yield [__DIR__ . '/Fixture/SomeEntitySimple.php', Class_::class];
yield [__DIR__ . '/Fixture/SkipNonDoctrineEntity.php', ClassMethod::class];
yield [__DIR__ . '/Fixture/TableWithIndexes.php', Class_::class];
yield [__DIR__ . '/Fixture/FormattingDoctrineEntity.php', Class_::class];
yield [__DIR__ . '/Fixture/SomeProperty.php', Property::class];
yield [__DIR__ . '/Fixture/PropertyWithName.php', Property::class];
yield [__DIR__ . '/Fixture/FromOfficialDocs.php', Property::class];
yield [__DIR__ . '/Fixture/JoinTable.php', Property::class];
}
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Property_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Property_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Property_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;

View File

@ -2,12 +2,12 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(readOnly=true, repositoryClass="Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Source\ExistingRepositoryClass")
* @ORM\Entity(readOnly=true, repositoryClass="Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Source\ExistingRepositoryClass")
* @ORM\Table(name="answer")
*/
final class SomeEntity

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Property_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Fixture;
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Fixture;
use Doctrine\ORM\Mapping as ORM;

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\DoctrineOrmTagParser\Source;
final class ExistingRepositoryClass
{
}

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\GedmoTagParser;
use Iterator;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\AbstractPhpDocInfoTest;
/**
* @see \Rector\BetterPhpDocParser\PhpDocNode\Gedmo\BlameableTagValueNode
*/
final class GedmoTagParserTest extends AbstractPhpDocInfoTest
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestPrintedPhpDocInfo($filePath, Property::class);
}
public function provideData(): Iterator
{
yield [__DIR__ . '/Source/SomeClassMethod.php'];
}
}

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\GedmoTagParser\Source;
use Gedmo\Mapping\Annotation as Gedmo;
final class SomeClassMethod
{
/**
* @Gedmo\Slug(fields={"lastName", "firstName"}, prefix="papo-")
*/
protected $gitoliteName;
}

View File

@ -1,14 +0,0 @@
/**
* @ORM\Table(
* name="my_entity",
* indexes={
* @ORM\Index(
* name="my_entity_idx", columns={"x", "xx", "xxx", "xxxx"}
* ),
* @ORM\Index(
* name="my_entity_xxx_idx", columns={"xxx"}
* )
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\MyEntityRepository")
*/

View File

@ -1,3 +0,0 @@
/**
* @Entity("user", expr="repository.findOneBy({'legalInformation.payment.uboDeclarationId': uboDeclarationId})")
*/

View File

@ -1,4 +0,0 @@
/**
* @ORM\Entity(readOnly=true, repositoryClass="Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Source\ExistingRepositoryClass")
* @ORM\Table(name="answer")
*/

View File

@ -1,5 +0,0 @@
/**
* @ORM\Table(name="amenity_building", uniqueConstraints={@ORM\UniqueConstraint(name="building_id_amenity_id",
* columns={"building_id", "amenity_id"})}
* )
*/

View File

@ -1,53 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_;
use Iterator;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\AbstractPhpDocInfoTest;
final class ParserClassTest extends AbstractPhpDocInfoTest
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath, string $expectedPrintedPhpDoc, string $type): void
{
$class = $this->parseFileAndGetFirstNodeOfType($filePath, $type);
$printedPhpDocInfo = $this->printNodePhpDocInfoToString($class);
$this->assertStringEqualsFile($expectedPrintedPhpDoc, $printedPhpDocInfo);
}
public function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SomeEntity.php', __DIR__ . '/Fixture/expected_some_entity.txt', Class_::class];
yield [
__DIR__ . '/Fixture/SomeEntitySimple.php',
__DIR__ . '/Fixture/expected_some_entity_simple.txt',
Class_::class,
];
yield [
__DIR__ . '/Fixture/SkipNonDoctrineEntity.php',
__DIR__ . '/Fixture/expected_skip_non_doctrine_entity.txt',
ClassMethod::class,
];
yield [
__DIR__ . '/Fixture/TableWithIndexes.php',
__DIR__ . '/Fixture/expected_table_with_indexes.txt',
Class_::class,
];
yield [
__DIR__ . '/Fixture/FormattingDoctrineEntity.php',
__DIR__ . '/Fixture/expected_formatting.txt',
Class_::class,
];
}
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Class_\Source;
final class ExistingRepositoryClass
{
}

View File

@ -1,3 +0,0 @@
/**
* @ORM\Column(type="integer", name="login_count", nullable=false, options={"unsigned":true, "default":0}, columnDefinition="CHAR(2) NOT NULL")
*/

View File

@ -1,6 +0,0 @@
/**
* @ORM\JoinTable(name="page_template_area",
* joinColumns={@ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="area_id", referencedColumnName="id")}
* )
*/

View File

@ -1,4 +0,0 @@
/**
* @ORM\Column(type="string", name="hey")
* @ORM\Column(name="hey", type="string")
*/

View File

@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\Property_;
use Iterator;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\AbstractPhpDocInfoTest;
final class ParserPropertyTest extends AbstractPhpDocInfoTest
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath, string $expectedPrintedPhpDoc): void
{
$property = $this->parseFileAndGetFirstNodeOfType($filePath, Property::class);
$printedPhpDocInfo = $this->printNodePhpDocInfoToString($property);
$this->assertStringEqualsFile($expectedPrintedPhpDoc, $printedPhpDocInfo);
}
public function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SomeProperty.php', __DIR__ . '/Fixture/expected_some_property.txt'];
yield [__DIR__ . '/Fixture/PropertyWithName.php', __DIR__ . '/Fixture/expected_property_with_name.txt'];
yield [__DIR__ . '/Fixture/FromOfficialDocs.php', __DIR__ . '/Fixture/expected_from_official_docs.txt'];
yield [__DIR__ . '/Fixture/JoinTable.php', __DIR__ . '/Fixture/expected_join_table.txt'];
}
}

View File

@ -1,3 +0,0 @@
/**
* @Route("/user", name="user_index", host="%test%", methods={"GET"})
*/

View File

@ -1,3 +0,0 @@
/**
* @Route({"en": "/en"}, name="homepage")
*/

View File

@ -6,7 +6,7 @@ namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\SymfonyRouteTagParser;
use Iterator;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\AbstractPhpDocInfoTest;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\AbstractPhpDocInfoTest;
/**
* @see \Rector\BetterPhpDocParser\PhpDocNode\Symfony\SymfonyRouteTagValueNode
@ -16,20 +16,14 @@ final class SymfonyRouteClassMethodTest extends AbstractPhpDocInfoTest
/**
* @dataProvider provideData()
*/
public function test(string $filePath, string $expectedPrintedPhpDoc): void
public function test(string $filePath): void
{
// this is needed to have "Symfony\Component\Routing\Annotation\Route" in the annotation
$classMethod = $this->parseFileAndGetFirstNodeOfType($filePath, ClassMethod::class);
$printedPhpDocInfo = $this->printNodePhpDocInfoToString($classMethod);
$this->assertStringEqualsFile($expectedPrintedPhpDoc, $printedPhpDocInfo);
$this->doTestPrintedPhpDocInfo($filePath, ClassMethod::class);
}
public function provideData(): Iterator
{
yield [__DIR__ . '/Source/SomeClassMethod.php', __DIR__ . '/Fixture/expected_some_class_method.txt'];
yield [__DIR__ . '/Source/RouteWithHost.php', __DIR__ . '/Fixture/expected_route_with_host.txt'];
yield [__DIR__ . '/Source/SomeClassMethod.php'];
yield [__DIR__ . '/Source/RouteWithHost.php'];
}
}

View File

@ -145,7 +145,7 @@ parameters:
# bugs
- '#Parameter \#1 \$items of class PhpParser\\Node\\Expr\\Array_ constructor expects array<PhpParser\\Node\\Expr\\ArrayItem\>, array<PhpParser\\Node\\Expr\\ArrayItem\|null\> given#'
- '#Method Rector\\BetterPhpDocParser\\Tests\\PhpDocParser\\OrmTagParser\\AbstractPhpDocInfoTest\:\:parseFileAndGetFirstNodeOfType\(\) should return PhpParser\\Node but returns PhpParser\\Node\|null#'
- '#Method Rector\\BetterPhpDocParser\\Tests\\PhpDocParser\\AbstractPhpDocInfoTest\:\:parseFileAndGetFirstNodeOfType\(\) should return PhpParser\\Node but returns PhpParser\\Node\|null#'
- '#Method Rector\\BetterPhpDocParser\\PhpDocNodeFactory\\JMS\\SerializerTypePhpDocNodeFactory::resolveTypeAnnotation\(\) should return JMS\\Serializer\\Annotation\\Type\|null but returns object\|null#'