Skip to content

Commit

Permalink
Transunit is now a Symfony Console application.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamelso committed Jul 3, 2024
1 parent 959ecde commit 5ff8582
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"phpspec/prophecy-phpunit": "^2.2"
},
"require": {
"nikic/php-parser": "*"
"nikic/php-parser": "*",
"symfony/console": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions lib/Transunit/Pass/AssertionPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* - $this->_testSubject->contractOut(47)->shouldReturn($agent47);
* + self::assertSame($agent47, $this->_testSubject->contractOut(47));
* ```
* @todo Fix duplicate statements from appearing.
*/
class AssertionPass implements Pass
{
Expand Down
27 changes: 19 additions & 8 deletions lib/Transunit/Transunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,39 @@

class Transunit
{
public static function run(string $path, string $destination = 'var'): void
private Filesystem $filesystem;

public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

public static function create(): self
{
return new self(new Filesystem());
}

public function run(string $path, string $destination = 'var'): void
{
$fs = new Filesystem();
$specs = (new Finder())->files()->in($path)->name('*Spec.php');

$root = dirname(__DIR__, 2);
$exportDir = "{$root}/{$destination}";

// @todo Confirm filesystem changes with user.
// $fs->remove($exportDir);
$fs->mkdir($exportDir);
// $this->filesystem->remove($exportDir);
$this->filesystem->mkdir($exportDir);

foreach ($specs as $file) {
$relative = trim($fs->makePathRelative($file->getPath(), $path), DIRECTORY_SEPARATOR);
$relative = trim($this->filesystem->makePathRelative($file->getPath(), $path), DIRECTORY_SEPARATOR);
$newFilename = substr($file->getBasename(), 0, -8) . 'Test.php';
$fullPathToNewTestFile = "{$exportDir}/{$relative}/{$newFilename}";

$modifiedCode = self::processFile($file->getRealPath());

$fs->mkdir("{$exportDir}/{$relative}");
$fs->touch($fullPathToNewTestFile);
$fs->dumpFile($fullPathToNewTestFile, $modifiedCode);
$this->filesystem->mkdir("{$exportDir}/{$relative}");
$this->filesystem->touch($fullPathToNewTestFile);
$this->filesystem->dumpFile($fullPathToNewTestFile, $modifiedCode);
}
}

Expand Down
37 changes: 32 additions & 5 deletions transunit.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
<?php

require_once 'vendor/autoload.php'; // Assuming you have installed nikic/php-parser via Composer
require __DIR__.'/vendor/autoload.php';

$args = $argv;
array_shift($args);
$args[0] = getcwd().'/'.$args[0];
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\SingleCommandApplication;

\Transunit\Transunit::run(...$args);
(new SingleCommandApplication())
->setName('Transunit')

->addArgument('source', InputArgument::REQUIRED, 'Location of PhpSpec tests to convert.')
->addArgument('destination', InputArgument::REQUIRED, 'Export directory for converted PHPUnit test.')

->setCode(function (InputInterface $input, OutputInterface $output): int {
$fs = new \Symfony\Component\Filesystem\Filesystem();

$source = $input->getArgument('source');
$destination = $input->getArgument('destination');

if (!$fs->isAbsolutePath($source)) {
$source = getcwd().'/'.$source;
}

if (!$fs->isAbsolutePath($destination)) {
$destination = getcwd().'/'.$destination;
}

\Transunit\Transunit::create()->run($source, $destination);

return Command::SUCCESS;
})

->run();

0 comments on commit 5ff8582

Please sign in to comment.