From 5ff858284227bbcaab4860860a2e1e9634b3f6e0 Mon Sep 17 00:00:00 2001 From: Adam Elsodaney Date: Wed, 3 Jul 2024 18:56:44 +0100 Subject: [PATCH] Transunit is now a Symfony Console application. --- composer.json | 3 ++- lib/Transunit/Pass/AssertionPass.php | 1 + lib/Transunit/Transunit.php | 27 ++++++++++++++------ transunit.php | 37 ++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index d2426bc..5b90326 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "phpspec/prophecy-phpunit": "^2.2" }, "require": { - "nikic/php-parser": "*" + "nikic/php-parser": "*", + "symfony/console": "^6.4" }, "autoload": { "psr-4": { diff --git a/lib/Transunit/Pass/AssertionPass.php b/lib/Transunit/Pass/AssertionPass.php index c887a87..87081b2 100644 --- a/lib/Transunit/Pass/AssertionPass.php +++ b/lib/Transunit/Pass/AssertionPass.php @@ -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 { diff --git a/lib/Transunit/Transunit.php b/lib/Transunit/Transunit.php index 344328f..aea6d31 100644 --- a/lib/Transunit/Transunit.php +++ b/lib/Transunit/Transunit.php @@ -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); } } diff --git a/transunit.php b/transunit.php index e83beed..32dee1c 100644 --- a/transunit.php +++ b/transunit.php @@ -1,9 +1,36 @@ 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();