diff --git a/bin/watch-phpunit.php b/bin/watch-phpunit.php index 3c11978..9ea61ea 100644 --- a/bin/watch-phpunit.php +++ b/bin/watch-phpunit.php @@ -24,6 +24,6 @@ $application = new Application(); $command = new RunCommand(); $application->add($command); - $application->setDefaultCommand($command->getName()); + $application->setDefaultCommand('watch'); $application->run(); })(); diff --git a/src/RunCommand.php b/src/RunCommand.php index 81d9b4d..b663e9a 100644 --- a/src/RunCommand.php +++ b/src/RunCommand.php @@ -7,12 +7,14 @@ use Depend\DependencyFinder; use DOMDocument; use Exception; +use LogicException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; use Watcher\FileSystem\PHPUnitConfigFinder; +use Watcher\PHPUnit\Configuration; use Watcher\PHPUnit\SavingConfiguration; use Watcher\PHPUnit\XMLConfig; use function array_filter; @@ -24,6 +26,21 @@ final class RunCommand extends Command { + /** @var OutputInterface */ + private $output; + + /** @var DependencyFinder */ + private $dependencyFinder; + + /** @var Watcher */ + private $watcher; + + /** @var Configuration */ + private $configFile; + + /** @var string */ + private $configLocation; + protected function configure() : void { $this->setName('watch') @@ -49,47 +66,69 @@ protected function configure() : void */ public function execute(InputInterface $input, OutputInterface $output) : int { - $src = convertInputToPath($input->getOption('src')); - $test = convertInputToPath($input->getOption('test')); - $finder = new PHPUnitConfigFinder(); - $dependencyFinder = new DependencyFinder([$src, $test]); - $dependencyFinder->build(); - $watcher = new Watcher([$src, $test]); - - $outFile = getcwd() . '/phpunit.tmp.xml.dist'; - $originalConfig = $finder->find(); - copy($originalConfig, $outFile); + $this->output = $output; + $src = convertInputToPath($input->getOption('src')); + $test = convertInputToPath($input->getOption('test')); + $this->dependencyFinder = new DependencyFinder([$src, $test]); + $this->dependencyFinder->build(); + $this->watcher = new Watcher([$src, $test]); + + $cwd = getcwd(); + if ($cwd === false) { + throw new LogicException('Unable to determine current directory, exiting'); + } + + $this->configLocation = $cwd . '/phpunit.tmp.xml.dist'; + $originalConfig = (new PHPUnitConfigFinder())->find(); + copy($originalConfig, $this->configLocation); $dom = new DOMDocument(); $dom->load($originalConfig); - $configFile = new SavingConfiguration(new XMLConfig($dom), $outFile); + $this->configFile = new SavingConfiguration(new XMLConfig($dom), $this->configLocation); + + $this->configFile->removeExistingTestSuite(); - $configFile->removeExistingTestSuite(); + $this->loop(); + + return 1; + } + private function loop() : void + { while (true) { - if (! $watcher->hasChangedFiles()) { + if (! $this->watcher->hasChangedFiles()) { usleep(1000000); continue; } - $changedFiles = array_filter(array_map('realpath', $watcher->getChangedFilesSinceLastCommit())); - $dependencyFinder->reBuild($changedFiles); - $changedAndRelatedFiles = array_unique($dependencyFinder->getAllFilesDependingOn($changedFiles)); + $changedFiles = array_filter( + array_map('realpath', $this->watcher->getChangedFilesSinceLastCommit()) + ); + $this->doLoop($changedFiles); - $configFile->addTestSuiteWithFilteredTestFiles($changedAndRelatedFiles); + usleep(1000000); + } + } - $p = new Process([ - 'vendor/bin/phpunit', - '--configuration=' . $outFile, - ]); + /** + * @param array $changedFiles + */ + private function doLoop(array $changedFiles) : void + { + $this->dependencyFinder->reBuild($changedFiles); + $changedAndRelatedFiles = array_unique( + $this->dependencyFinder->getAllFilesDependingOn($changedFiles) + ); - $p->run(); - $output->write($p->getOutput()); + $this->configFile->addTestSuiteWithFilteredTestFiles($changedAndRelatedFiles); - $configFile->removeExistingTestSuite(); + $p = new Process([ + 'vendor/bin/phpunit', + '--configuration=' . $this->configLocation, + ]); - usleep(1000000); - } + $p->run(); + $this->output->write($p->getOutput()); - return 1; + $this->configFile->removeExistingTestSuite(); } }