Created
September 16, 2011 08:33
-
-
Save henrikbjorn/1221566 to your computer and use it in GitHub Desktop.
ResourceWatcher usage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace FooBar\FooBundle\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use FooBar\FooBundle\Test\Runner; | |
class AutotestCommand extends \Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('foobar:autotest') | |
->setDescription('Autorun your PHPUnit tests when a .php file is modified.') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$runner = new Runner($this->getContainer()->getParameter('kernel.root_dir') . '/../src'); | |
$runner->run($output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace FooBar\FooBundle\Test; | |
use Symfony\Component\ResourceWatcher\ResourceWatcher; | |
use Symfony\Component\ResourceWatcher\Event\Event; | |
use Symfony\Component\Config\Resource\DirectoryResource; | |
use Symfony\Component\Process\Process; | |
use Symfony\Component\Console\Output\OutputInterface; | |
/** | |
* Watches resources and runs phpunit cli when a php file changes. | |
* | |
* @package FooBarFooBundle | |
*/ | |
class Runner | |
{ | |
/** | |
* @var string | |
*/ | |
protected $dir; | |
public function __construct($dir) | |
{ | |
$this->dir = realpath($dir); | |
} | |
public function run(OutputInterface $output) | |
{ | |
$watcher = new ResourceWatcher(); | |
$watcher->track(new DirectoryResource(realpath($this->dir)), function ($event) use ($output) { | |
$fileName = (string) $event->getResource(); | |
if (preg_match('/^(.*Bundle\/)(.*)(\.php)$/', $fileName, $matches)) { | |
if ('Test' !== substr($matches[2], -4)) { | |
$fileName = $matches[1] . 'Tests/' . $matches[2] . 'Test' . $matches[3]; | |
} | |
$process = new Process('phpunit ' . escapeshellarg($fileName), $this->dir . '/../'); | |
$process->run(function ($type, $data) use ($output) { | |
$output->writeln($data); | |
}); | |
} | |
}); | |
$watcher->start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment