Skip to content
Open
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
2 changes: 1 addition & 1 deletion bin/watch-phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
$application = new Application();
$command = new RunCommand();
$application->add($command);
$application->setDefaultCommand($command->getName());
$application->setDefaultCommand('watch');
$application->run();
})();
93 changes: 66 additions & 27 deletions src/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')
Expand All @@ -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<string> $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();
}
}