Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GithubMagicLink] Handle Create a Test link on rector-* packages #613

Merged
merged 4 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages-tests/Demo/Helpers/StringToArrayRunFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\Website\Tests\Demo\Helpers;

use Rector\Symfony\Rector\New_\StringToArrayArgumentProcessRector;
use Rector\Website\Demo\Entity\RectorRun;
use Symfony\Component\Uid\Uuid;

final class StringToArrayRunFactory
{
public function create(): RectorRun
{
$rectorRun = new RectorRun(
Uuid::v4(),
file_get_contents(__DIR__ . '/Source/rector_run_file_content.php.inc'),
file_get_contents(__DIR__ . '/Source/rector_run_config_content.php.inc')
);

$jsonResult['file_diffs'][]['applied_rectors'] = [StringToArrayArgumentProcessRector::class];
$rectorRun->setJsonResult($jsonResult);

return $rectorRun;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/rectorphp/rector-symfony/new/main/tests/Rector/New_/StringToArrayArgumentProcessRector/Fixture?filename=Fixture/any_class.php.inc&value=%3C%3Fphp%0A%0Anamespace+Rector%5CTests%5CSymfony%5CRector%5CNew_%5CStringToArrayArgumentProcessRector%5CFixture%3B%0A%0Aclass+AnyClass%0A%7B%0A++++public+function+run%28%29%0A++++%7B%0A++++++++echo+%27some+PHP%27%3B%0A++++%7D%0A%7D%0A%0A%3F%3E%0A-----%0A%3C%3Fphp%0A%0Anamespace+Rector%5CTests%5CSymfony%5CRector%5CNew_%5CStringToArrayArgumentProcessRector%5CFixture%3B%0A%0A%2F%2F+what+is+expected+code%3F%0A%2F%2F+should+remain+the+same%3F+delete+part+bellow+-----+%28included%29%0A%0A%3F%3E%0A&message=Add+failing+test+fixture+for+StringToArrayArgumentProcessRector&description=%23+Failing+Test+for+StringToArrayArgumentProcessRector%0A%0ABased+on+https%3A%2F%2Fgetrector.org%2Fdemo%2F%s%0A
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Website\Tests\GitHubMagicLink\LinkFactory\FixtureLinkFactory;

use Rector\Website\Tests\Demo\Helpers\DummyRectorRunFactory;
use Rector\Website\GetRectorKernel;
use Rector\Website\GitHubMagicLink\LinkFactory\FixtureLinkFactory;
use Rector\Website\Tests\Demo\Helpers\StringToArrayRunFactory;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;

final class RectorSymfonyFixtureLinkFactoryTest extends AbstractKernelTestCase
{
private FixtureLinkFactory $testFixtureLinkFactory;

private StringToArrayRunFactory $stringToArrayRunFactory;

protected function setUp(): void
{
$this->bootKernel(GetRectorKernel::class);
$this->testFixtureLinkFactory = $this->getService(FixtureLinkFactory::class);
$this->stringToArrayRunFactory = new StringToArrayRunFactory();
}

public function test(): void
{
$rectorRun = $this->stringToArrayRunFactory->create();
$testFixtureLink = $this->testFixtureLinkFactory->create($rectorRun);

$this->assertStringMatchesFormatFile(__DIR__ . '/FixtureRectorSymfony/expected_link.txt', $testFixtureLink . PHP_EOL);
}
}
41 changes: 41 additions & 0 deletions packages/GitHubMagicLink/LinkFactory/FixtureLinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Website\GitHubMagicLink\LinkFactory;

use Nette\Utils\Strings;
use Rector\Website\Demo\Entity\RectorRun;
use Rector\Website\GitHubMagicLink\BodyFactory\FixtureBodyFactory;
use Rector\Website\GitHubMagicLink\BodyFactory\PullRequestDescriptionFactory;
Expand All @@ -18,6 +19,25 @@ final class FixtureLinkFactory
*/
private const BASE_URL = 'https://github.com/rectorphp/rector-src/new/main';

/**
* @var string
* @see https://regex101.com/r/ABk9gM/1
*/
private const PACKAGE_NAME_REGEX = '#^rules-tests\/(?<Package>[^\/]+)\/Rector#';

/**
* @var string[]
*/
private const RECTOR_PACKAGE_NAMES = [
'Symfony',
'PHPUnit',
'Doctrine',
'Nette',
'CakePHP',
'PHPOffice',
'Laravel',
];

public function __construct(
private readonly FixtureBodyFactory $fixtureBodyFactory,
private readonly PullRequestDescriptionFactory $pullRequestDescriptionFactory
Expand All @@ -33,6 +53,27 @@ public function create(RectorRun $rectorRun): string
$message = 'Add failing test fixture for ' . $rectorRun->getRectorShortClass();
$description = $this->pullRequestDescriptionFactory->create($rectorRun);

$match = Strings::match($expectedRectorTestPath, self::PACKAGE_NAME_REGEX);
$link = $this->resolveLink($expectedRectorTestPath, $rectorRun, $content, $message, $description);

if ($match === null || ! in_array($match['Package'], self::RECTOR_PACKAGE_NAMES, true)) {
return $link;
}

$package = strtolower($match['Package']);
$link = str_replace('rules-tests/' . $match['Package'] . '/Rector', 'tests/Rector', $link);

return str_replace('rector-src', 'rector-' . $package, $link);
}

private function resolveLink(
string $expectedRectorTestPath,
RectorRun $rectorRun,
string $content,
string $message,
string $description
): string
{
return self::BASE_URL . '/'
. $expectedRectorTestPath
. '?filename=Fixture/' . $rectorRun->getFixtureFileName()
Expand Down