4 * This file is part of the Behat Testwork.
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\Suite;
13 use Behat\Testwork\Suite\Exception\SuiteConfigurationException;
14 use Behat\Testwork\Suite\Exception\SuiteGenerationException;
15 use Behat\Testwork\Suite\Generator\SuiteGenerator;
18 * Acts like a suite repository by auto-generating suites for registered suite configurations using registered
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 final class SuiteRegistry implements SuiteRepository
28 private $suitesGenerated = false;
30 * @var SuiteGenerator[]
32 private $generators = array();
36 private $suiteConfigurations = array();
40 private $suites = array();
43 * Registers suite generator.
45 * @param SuiteGenerator $generator
47 public function registerSuiteGenerator(SuiteGenerator $generator)
49 $this->generators[] = $generator;
50 $this->suitesGenerated = false;
54 * Registers suite using provided name, type & parameters.
58 * @param array $settings
60 * @throws SuiteConfigurationException
62 public function registerSuiteConfiguration($name, $type, array $settings)
64 if (isset($this->suiteConfigurations[$name])) {
65 throw new SuiteConfigurationException(sprintf(
66 'Suite configuration for a suite "%s" is already registered.',
71 $this->suiteConfigurations[$name] = array($type, $settings);
72 $this->suitesGenerated = false;
76 * Returns all available suites.
80 public function getSuites()
82 if ($this->suitesGenerated) {
86 $this->suites = array();
87 foreach ($this->suiteConfigurations as $name => $configuration) {
88 list($type, $settings) = $configuration;
90 $this->suites[] = $this->generateSuite($name, $type, $settings);
93 $this->suitesGenerated = true;
99 * Generates suite using registered generators.
101 * @param string $name
102 * @param string $type
103 * @param array $settings
107 * @throws SuiteGenerationException If no appropriate generator found
109 private function generateSuite($name, $type, array $settings)
111 foreach ($this->generators as $generator) {
112 if (!$generator->supportsTypeAndSettings($type, $settings)) {
116 return $generator->generateSuite($name, $settings);
119 throw new SuiteGenerationException(sprintf(
120 'Can not find suite generator for a suite `%s` of type `%s`.',