Skip to content

Instantly share code, notes, and snippets.

@tentacode
Created November 23, 2011 16:36
Show Gist options
  • Save tentacode/1389156 to your computer and use it in GitHub Desktop.
Save tentacode/1389156 to your computer and use it in GitHub Desktop.
Une commande pour lancer des tests Behat
<?php
namespace Pmsipilot\TestBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand,
Symfony\Component\HttpKernel\Util,
Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Input\ArrayInput,
Symfony\Component\Console\Output\OutputInterface;
/**
* Lance les tests Behat
*/
class BehatCommand extends ContainerAwareCommand
{
/**
* @return void
*/
protected function configure()
{
//récupération des arguments du script (sans app/console ni la commande en cours)
$commandArguments = array_slice($_SERVER['argv'], 2);
$definition = array();
foreach($commandArguments as $argument)
{
//options sans valeur avec alias
if(preg_match('/^-([^-][^ =]*)$/', $argument, $result))
{
$definition[] = new InputOption($result[1], $result[1], InputOption::VALUE_NONE, 'argv');
}
//options sans valeur sans alias
elseif(preg_match('/^-{2}([^ =]+)$/', $argument, $result))
{
$definition[] = new InputOption($result[1], null, InputOption::VALUE_NONE, 'argv');
}
//options avec valeur avec alias
elseif(preg_match('/^-([^-][^ =]*)=(.*)$/', $argument, $result))
{
$definition[] = new InputOption($result[1], $result[1], InputOption::VALUE_REQUIRED, 'argv');
}
//options avec valeur sans alias
elseif(preg_match('/^-{2}([^ =]+)=(.*)$/', $argument, $result))
{
$definition[] = new InputOption($result[1], null, InputOption::VALUE_REQUIRED, 'argv');
}
}
$this
->setName('test:behat')
->setDescription('Exécution des tests Behat')
->setDefinition($definition)
;
//ajout de l'argument features
$this->addArgument('features', InputArgument::OPTIONAL);
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('<info>%s</info>', $this->getDescription()));
$behatPath = $this->getContainer()->get('kernel')->getRootDir().'/../vendor/BehatCH/behat.phar';
//autoload behat
require_once(sprintf("phar://%s/autoload.php", $behatPath));
//chargement des options de la ligne de commande
$behatOptions = array();
foreach($this->getDefinition()->getOptions() as $option)
{
//seulement celles qui viennet de la ligne de commande
if($option->getDescription() == 'argv')
{
$behatOptions[($option->getShortcut() != '')? '-'.$option->getShortcut() : '--'.$option->getName()] = $input->getOption($option->getName());
}
}
//chargement du fichier de config par défaut
if(!isset($behatOptions['-c']) && !isset($behatOptions['--config']))
{
$behatOptions['--config'] = $this->getContainer()->get('kernel')->getRootDir().'/../src/Pmsipilot/ReferentielsBundle/Tests/Behat/behat.yml';
}
//quelle feature ?
if($input->getArgument('features') != '')
{
$behatOptions['features'] = $this->getContainer()->get('kernel')->getRootDir().'/../src/Pmsipilot/ReferentielsBundle/Tests/Behat/features/'.basename($input->getArgument('features'));
}
else
{
$behatOptions['features'] = $this->getContainer()->get('kernel')->getRootDir().'/../src/Pmsipilot/ReferentielsBundle/Tests/Behat/features';
}
//execution des tests
$app = new \Behat\Behat\Console\BehatApplication('0');
$commandReturn = $app->run(new \Symfony\Component\Console\Input\ArrayInput($behatOptions));
if($commandReturn === 0)
{
$output->writeln('<info>Aucune erreur n\'a été détectée par Behat</info>');
}
else
{
$output->writeln('<error>Des erreurs ont été détectées par Behat</error>');
}
return $commandReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment