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

Add AliceYamlFixturesToPhpCommand #68

Merged
merged 2 commits into from
Jan 10, 2025
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ Searching for regex: #this->get\((.*)\)#

```


<br>

## 9. Convert Alice fixtures from YAML to PHP

The `nelmio/alice` package [allows to use PHP](https://github.com/nelmio/alice/blob/v2.3.0/doc/complete-reference.md#php) for test fixture definitions. It's much better format, because Rector and PHPStan can understand it.

But what if we have 100+ YAML files in our project?

```bash
vendor/bin/swiss-knife convert-alice-yaml-to-php fixtures
```

That's it!


<br>

Happy coding!
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"nikic/php-parser": "^5.4",
"symfony/console": "^6.4",
"symfony/finder": "^6.4",
"symfony/yaml": "^7.2",
"webmozart/assert": "^1.11"
},
"require-dev": {
Expand Down
90 changes: 90 additions & 0 deletions src/Command/AliceYamlFixturesToPhpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Command;

use Nette\Utils\FileSystem;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Stmt\Return_;
use PhpParser\PrettyPrinter\Standard;
use Rector\SwissKnife\Finder\FilesFinder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;

/**
* @see https://github.com/nelmio/alice/blob/v2.3.0/doc/complete-reference.md#php
*/
final class AliceYamlFixturesToPhpCommand extends Command
{
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
) {
parent::__construct();
}

protected function configure(): void
{
$this->setName('alice-yaml-fixtures-to-php');

$this->addArgument(
'sources',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'One or more paths to check'
);

$this->setDescription('Converts Alice YAML fixtures to PHP format, so Rector and PHPStan can understand it');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$sources = (array) $input->getArgument('sources');
$yamlFileInfos = FilesFinder::findYamlFiles($sources);

$standard = new Standard();

// use php-parser to dump their PHP version, @see https://github.com/nelmio/alice/blob/main/doc%2Fcomplete-reference.md#php
foreach ($yamlFileInfos as $yamlFileInfo) {
$yaml = Yaml::parseFile($yamlFileInfo->getRealPath());

$return = $this->createArrayReturn($yaml);
$phpFileContents = $standard->prettyPrintFile([$return]);

// get real path without yml/yaml suffix
if (str_ends_with($yamlFileInfo->getRealPath(), '.yml')) {
$phpFilePath = substr($yamlFileInfo->getRealPath(), 0, -4) . '.php';
} else {
$phpFilePath = substr($yamlFileInfo->getRealPath(), 0, -5) . '.php';
}

FileSystem::write($phpFilePath, $phpFileContents);

// remove YAML file
unlink($yamlFileInfo->getRealPath());

$this->symfonyStyle->writeln('[DELETED] ' . $yamlFileInfo->getRelativePathname());
$this->symfonyStyle->writeln('[ADDED] ' . $phpFilePath);
$this->symfonyStyle->newLine();
}

$this->symfonyStyle->success(
sprintf('Successfully converted %d Alice YAML fixtures to PHP', count($yamlFileInfos))
);

return self::SUCCESS;
}

/**
* @param mixed[] $yaml
*/
private function createArrayReturn(array $yaml): Return_
{
$expr = BuilderHelpers::normalizeValue($yaml);

return new Return_($expr);
}
}
Loading