4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Ordering\Cli;
13 use Behat\Testwork\Ordering\Exception\InvalidOrderException;
14 use Behat\Testwork\Ordering\OrderedExercise;
15 use Behat\Testwork\Ordering\Orderer\Orderer;
16 use Behat\Testwork\Cli\Controller;
17 use Symfony\Component\Console\Command\Command as SymfonyCommand;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
21 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24 * Preloads scenarios and then modifies the order when --order is passed
26 * @author Ciaran McNulty <mail@ciaranmcnulty.com>
28 final class OrderController implements Controller
31 * @var EventDispatcherInterface
33 private $eventDispatcher;
35 * @var OrderedExercise
41 private $orderers = array();
44 * Initializes controller.
46 * @param EventDispatcherInterface $eventDispatcher
47 * @param OrderedExercise $exercise
49 public function __construct(EventDispatcherInterface $eventDispatcher, OrderedExercise $exercise)
51 $this->eventDispatcher = $eventDispatcher;
52 $this->exercise = $exercise;
56 * Configures command to be executable by the controller.
58 * @param SymfonyCommand $command
60 public function configure(SymfonyCommand $command)
62 $command->addOption('--order', null, InputOption::VALUE_REQUIRED,
63 'Set an order in which to execute the specifications (this will result in slower feedback).'
68 * Executes controller.
70 * @param InputInterface $input
71 * @param OutputInterface $output
73 * @return null|integer
75 public function execute(InputInterface $input, OutputInterface $output)
77 $orderer = $input->getOption('order');
83 if (!array_key_exists($orderer, $this->orderers)) {
84 throw new InvalidOrderException(sprintf("Order option '%s' was not recognised", $orderer));
87 $this->exercise->setOrderer($this->orderers[$orderer]);
91 * Register a new available controller
93 * @param Orderer $orderer
95 public function registerOrderer(Orderer $orderer)
97 $this->orderers[$orderer->getName()] = $orderer;