3 namespace Tests\Behat\Gherkin\Loader;
5 use Behat\Gherkin\Loader\ArrayLoader;
7 class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
11 protected function setUp()
13 $this->loader = new ArrayLoader();
16 public function testSupports()
18 $this->assertFalse($this->loader->supports(__DIR__));
19 $this->assertFalse($this->loader->supports(__FILE__));
20 $this->assertFalse($this->loader->supports('string'));
21 $this->assertFalse($this->loader->supports(array('wrong_root')));
22 $this->assertFalse($this->loader->supports(array('features')));
23 $this->assertTrue($this->loader->supports(array('features' => array())));
24 $this->assertTrue($this->loader->supports(array('feature' => array())));
27 public function testLoadEmpty()
29 $this->assertEquals(array(), $this->loader->load(array('features' => array())));
32 public function testLoadFeatures()
34 $features = $this->loader->load(array(
37 'title' => 'First feature',
41 'description' => 'Second feature description',
43 'tags' => array('some', 'tags')
48 $this->assertEquals(2, count($features));
50 $this->assertEquals(3, $features[0]->getLine());
51 $this->assertEquals('First feature', $features[0]->getTitle());
52 $this->assertNull($features[0]->getDescription());
53 $this->assertNull($features[0]->getFile());
54 $this->assertEquals('en', $features[0]->getLanguage());
55 $this->assertFalse($features[0]->hasTags());
57 $this->assertEquals(1, $features[1]->getLine());
58 $this->assertNull($features[1]->getTitle());
59 $this->assertEquals('Second feature description', $features[1]->getDescription());
60 $this->assertNull($features[1]->getFile());
61 $this->assertEquals('ru', $features[1]->getLanguage());
62 $this->assertEquals(array('some', 'tags'), $features[1]->getTags());
65 public function testLoadScenarios()
67 $features = $this->loader->load(array(
73 'title' => 'First scenario',
77 'tags' => array('second', 'scenario', 'tags')
80 'tags' => array('third', 'scenario'),
88 $this->assertEquals(1, count($features));
90 $scenarios = $features[0]->getScenarios();
92 $this->assertEquals(3, count($scenarios));
94 $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[0]);
95 $this->assertEquals('First scenario', $scenarios[0]->getTitle());
96 $this->assertFalse($scenarios[0]->hasTags());
97 $this->assertEquals(2, $scenarios[0]->getLine());
99 $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[1]);
100 $this->assertNull($scenarios[1]->getTitle());
101 $this->assertEquals(array('second', 'scenario', 'tags'), $scenarios[1]->getTags());
102 $this->assertEquals(1, $scenarios[1]->getLine());
104 $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[2]);
105 $this->assertNull($scenarios[2]->getTitle());
106 $this->assertEquals(array('third', 'scenario'), $scenarios[2]->getTags());
107 $this->assertEquals(3, $scenarios[2]->getLine());
110 public function testLoadOutline()
112 $features = $this->loader->load(array(
115 'title' => 'Feature',
116 'scenarios' => array(
119 'title' => 'First outline',
124 'tags' => array('second', 'outline', 'tags')
131 $this->assertEquals(1, count($features));
133 $outlines = $features[0]->getScenarios();
135 $this->assertEquals(2, count($outlines));
137 $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[0]);
138 $this->assertEquals('First outline', $outlines[0]->getTitle());
139 $this->assertFalse($outlines[0]->hasTags());
140 $this->assertEquals(2, $outlines[0]->getLine());
142 $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[1]);
143 $this->assertNull($outlines[1]->getTitle());
144 $this->assertEquals(array('second', 'outline', 'tags'), $outlines[1]->getTags());
145 $this->assertEquals(1, $outlines[1]->getLine());
148 public function testOutlineExamples()
150 $features = $this->loader->load(array(
153 'title' => 'Feature',
154 'scenarios' => array(
157 'title' => 'First outline',
160 array('user', 'pass'),
161 array('ever', 'sdsd'),
162 array('anto', 'fdfd')
167 'tags' => array('second', 'outline', 'tags')
174 $this->assertEquals(1, count($features));
176 $scenarios = $features[0]->getScenarios();
177 $scenario = $scenarios[0];
180 array(array('user' => 'ever', 'pass' => 'sdsd'), array('user' => 'anto', 'pass' => 'fdfd')),
181 $scenario->getExampleTable()->getHash()
185 public function testLoadBackground()
187 $features = $this->loader->load(array(
192 'background' => array()
195 'background' => array(
202 $this->assertEquals(3, count($features));
204 $this->assertFalse($features[0]->hasBackground());
205 $this->assertTrue($features[1]->hasBackground());
206 $this->assertEquals(0, $features[1]->getBackground()->getLine());
207 $this->assertTrue($features[2]->hasBackground());
208 $this->assertEquals(2, $features[2]->getBackground()->getLine());
211 public function testLoadSteps()
213 $features = $this->loader->load(array(
216 'background' => array(
218 array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'bg step 1', 'line' => 3),
219 array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'bg step 2')
222 'scenarios' => array(
224 'title' => 'Scenario',
226 array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'sc step 1'),
227 array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'sc step 2')
231 'title' => 'Outline',
234 array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'out step 1'),
235 array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'out step 2')
243 $background = $features[0]->getBackground();
244 $this->assertTrue($background->hasSteps());
245 $this->assertEquals(2, count($background->getSteps()));
246 $steps = $background->getSteps();
247 $this->assertEquals('Gangway!', $steps[0]->getType());
248 $this->assertEquals('Gangway!', $steps[0]->getKeyword());
249 $this->assertEquals('Given', $steps[0]->getKeywordType());
250 $this->assertEquals('bg step 1', $steps[0]->getText());
251 $this->assertEquals(3, $steps[0]->getLine());
252 $this->assertEquals('Blimey!', $steps[1]->getType());
253 $this->assertEquals('Blimey!', $steps[1]->getKeyword());
254 $this->assertEquals('When', $steps[1]->getKeywordType());
255 $this->assertEquals('bg step 2', $steps[1]->getText());
256 $this->assertEquals(1, $steps[1]->getLine());
258 $scenarios = $features[0]->getScenarios();
260 $scenario = $scenarios[0];
261 $this->assertTrue($scenario->hasSteps());
262 $this->assertEquals(2, count($scenario->getSteps()));
263 $steps = $scenario->getSteps();
264 $this->assertEquals('Gangway!', $steps[0]->getType());
265 $this->assertEquals('Gangway!', $steps[0]->getKeyword());
266 $this->assertEquals('Given', $steps[0]->getKeywordType());
267 $this->assertEquals('sc step 1', $steps[0]->getText());
268 $this->assertEquals(0, $steps[0]->getLine());
269 $this->assertEquals('Blimey!', $steps[1]->getType());
270 $this->assertEquals('Blimey!', $steps[1]->getKeyword());
271 $this->assertEquals('When', $steps[1]->getKeywordType());
272 $this->assertEquals('sc step 2', $steps[1]->getText());
273 $this->assertEquals(1, $steps[1]->getLine());
275 $outline = $scenarios[1];
276 $this->assertTrue($outline->hasSteps());
277 $this->assertEquals(2, count($outline->getSteps()));
278 $steps = $outline->getSteps();
279 $this->assertEquals('Gangway!', $steps[0]->getType());
280 $this->assertEquals('Gangway!', $steps[0]->getKeyword());
281 $this->assertEquals('Given', $steps[0]->getKeywordType());
282 $this->assertEquals('out step 1', $steps[0]->getText());
283 $this->assertEquals(0, $steps[0]->getLine());
284 $this->assertEquals('Blimey!', $steps[1]->getType());
285 $this->assertEquals('Blimey!', $steps[1]->getKeyword());
286 $this->assertEquals('When', $steps[1]->getKeywordType());
287 $this->assertEquals('out step 2', $steps[1]->getText());
288 $this->assertEquals(1, $steps[1]->getLine());
291 public function testLoadStepArguments()
293 $features = $this->loader->load(array(
296 'background' => array(
299 'type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'step with table argument',
300 'arguments' => array(
312 'type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'step with pystring argument',
313 'arguments' => array(
315 'type' => 'pystring',
316 'text' => ' some text',
321 'type' => 'Let go and haul', 'keyword_type' => 'Then', 'text' => '2nd step with pystring argument',
322 'arguments' => array(
324 'type' => 'pystring',
325 'text' => 'some text',
335 $background = $features[0]->getBackground();
337 $this->assertTrue($background->hasSteps());
339 $steps = $background->getSteps();
341 $this->assertEquals(3, count($steps));
343 $arguments = $steps[0]->getArguments();
344 $this->assertEquals('Gangway!', $steps[0]->getType());
345 $this->assertEquals('Gangway!', $steps[0]->getKeyword());
346 $this->assertEquals('Given', $steps[0]->getKeywordType());
347 $this->assertEquals('step with table argument', $steps[0]->getText());
348 $this->assertInstanceOf('Behat\Gherkin\Node\TableNode', $arguments[0]);
349 $this->assertEquals(array(array('key'=>1, 'val'=>2), array('key'=>3,'val'=>4)), $arguments[0]->getHash());
351 $arguments = $steps[1]->getArguments();
352 $this->assertEquals('Blimey!', $steps[1]->getType());
353 $this->assertEquals('Blimey!', $steps[1]->getKeyword());
354 $this->assertEquals('When', $steps[1]->getKeywordType());
355 $this->assertEquals('step with pystring argument', $steps[1]->getText());
356 $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
357 $this->assertEquals(' some text', (string) $arguments[0]);
359 $arguments = $steps[2]->getArguments();
360 $this->assertEquals('Let go and haul', $steps[2]->getType());
361 $this->assertEquals('Let go and haul', $steps[2]->getKeyword());
362 $this->assertEquals('Then', $steps[2]->getKeywordType());
363 $this->assertEquals('2nd step with pystring argument', $steps[2]->getText());
364 $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
365 $this->assertEquals('some text', (string) $arguments[0]);
368 public function testSingleFeatureArray()
370 $features = $this->loader->load(array(
372 'title' => 'Some feature'
376 $this->assertEquals(1, count($features));
377 $this->assertEquals('Some feature', $features[0]->getTitle());