Skip to content

Commit

Permalink
Add AliceYamlFixturesToPhpCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 10, 2025
1 parent 249d9ab commit b01b845
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
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
85 changes: 85 additions & 0 deletions src/Command/AliceYamlFixturesToPhpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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]);

// print PHP file, without YML suffix
$phpFilePath = substr($yamlFileInfo->getRealPath(), 0, -4) . '.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);
}
}

0 comments on commit b01b845

Please sign in to comment.