4 * This file is part of the Behat Gherkin.
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\Gherkin\Loader;
13 use Behat\Gherkin\Node\BackgroundNode;
14 use Behat\Gherkin\Node\ExampleTableNode;
15 use Behat\Gherkin\Node\FeatureNode;
16 use Behat\Gherkin\Node\OutlineNode;
17 use Behat\Gherkin\Node\PyStringNode;
18 use Behat\Gherkin\Node\ScenarioNode;
19 use Behat\Gherkin\Node\StepNode;
20 use Behat\Gherkin\Node\TableNode;
25 * @author Konstantin Kudryashov <ever.zet@gmail.com>
27 class ArrayLoader implements LoaderInterface
30 * Checks if current loader supports provided resource.
32 * @param mixed $resource Resource to load
36 public function supports($resource)
38 return is_array($resource) && (isset($resource['features']) || isset($resource['feature']));
42 * Loads features from provided resource.
44 * @param mixed $resource Resource to load
46 * @return FeatureNode[]
48 public function load($resource)
52 if (isset($resource['features'])) {
53 foreach ($resource['features'] as $iterator => $hash) {
54 $feature = $this->loadFeatureHash($hash, $iterator);
55 $features[] = $feature;
57 } elseif (isset($resource['feature'])) {
58 $feature = $this->loadFeatureHash($resource['feature']);
59 $features[] = $feature;
66 * Loads feature from provided feature hash.
68 * @param array $hash Feature hash
69 * @param integer $line
73 protected function loadFeatureHash(array $hash, $line = 0)
78 'description' => null,
80 'keyword' => 'Feature',
83 'scenarios' => array(),
87 $background = isset($hash['background']) ? $this->loadBackgroundHash($hash['background']) : null;
90 foreach ((array) $hash['scenarios'] as $scenarioIterator => $scenarioHash) {
91 if (isset($scenarioHash['type']) && 'outline' === $scenarioHash['type']) {
92 $scenarios[] = $this->loadOutlineHash($scenarioHash, $scenarioIterator);
94 $scenarios[] = $this->loadScenarioHash($scenarioHash, $scenarioIterator);
98 return new FeatureNode($hash['title'], $hash['description'], $hash['tags'], $background, $scenarios, $hash['keyword'], $hash['language'], null, $hash['line']);
102 * Loads background from provided hash.
104 * @param array $hash Background hash
106 * @return BackgroundNode
108 protected function loadBackgroundHash(array $hash)
113 'keyword' => 'Background',
120 $steps = $this->loadStepsHash($hash['steps']);
122 return new BackgroundNode($hash['title'], $steps, $hash['keyword'], $hash['line']);
126 * Loads scenario from provided scenario hash.
128 * @param array $hash Scenario hash
129 * @param integer $line Scenario definition line
131 * @return ScenarioNode
133 protected function loadScenarioHash(array $hash, $line = 0)
139 'keyword' => 'Scenario',
146 $steps = $this->loadStepsHash($hash['steps']);
148 return new ScenarioNode($hash['title'], $hash['tags'], $steps, $hash['keyword'], $hash['line']);
152 * Loads outline from provided outline hash.
154 * @param array $hash Outline hash
155 * @param integer $line Outline definition line
157 * @return OutlineNode
159 protected function loadOutlineHash(array $hash, $line = 0)
165 'keyword' => 'Scenario Outline',
168 'examples' => array(),
173 $steps = $this->loadStepsHash($hash['steps']);
175 if (isset($hash['examples']['keyword'])) {
176 $examplesKeyword = $hash['examples']['keyword'];
177 unset($hash['examples']['keyword']);
179 $examplesKeyword = 'Examples';
182 $examples = new ExampleTableNode($hash['examples'], $examplesKeyword);
184 return new OutlineNode($hash['title'], $hash['tags'], $steps, $examples, $hash['keyword'], $hash['line']);
188 * Loads steps from provided hash.
194 private function loadStepsHash(array $hash)
197 foreach ($hash as $stepIterator => $stepHash) {
198 $steps[] = $this->loadStepHash($stepHash, $stepIterator);
205 * Loads step from provided hash.
207 * @param array $hash Step hash
208 * @param integer $line Step definition line
212 protected function loadStepHash(array $hash, $line = 0)
216 'keyword_type' => 'Given',
219 'keyword' => 'Scenario',
221 'arguments' => array(),
226 $arguments = array();
227 foreach ($hash['arguments'] as $argumentHash) {
228 if ('table' === $argumentHash['type']) {
229 $arguments[] = $this->loadTableHash($argumentHash['rows']);
230 } elseif ('pystring' === $argumentHash['type']) {
231 $arguments[] = $this->loadPyStringHash($argumentHash, $hash['line'] + 1);
235 return new StepNode($hash['type'], $hash['text'], $arguments, $hash['line'], $hash['keyword_type']);
239 * Loads table from provided hash.
241 * @param array $hash Table hash
245 protected function loadTableHash(array $hash)
247 return new TableNode($hash);
251 * Loads PyString from provided hash.
253 * @param array $hash PyString hash
254 * @param integer $line
256 * @return PyStringNode
258 protected function loadPyStringHash(array $hash, $line = 0)
260 $line = isset($hash['line']) ? $hash['line'] : $line;
263 foreach (explode("\n", $hash['text']) as $string) {
264 $strings[] = $string;
267 return new PyStringNode($strings, $line);