ParserTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Tests\Behat\Gherkin;
  3. use Behat\Gherkin\Node\FeatureNode;
  4. use Behat\Gherkin\Lexer;
  5. use Behat\Gherkin\Parser;
  6. use Behat\Gherkin\Keywords\ArrayKeywords;
  7. use Behat\Gherkin\Loader\YamlFileLoader;
  8. class ParserTest extends \PHPUnit_Framework_TestCase
  9. {
  10. private $gherkin;
  11. private $yaml;
  12. public function parserTestDataProvider()
  13. {
  14. $data = array();
  15. foreach (glob(__DIR__ . '/Fixtures/etalons/*.yml') as $file) {
  16. $testname = basename($file, '.yml');
  17. $data[] = array($testname);
  18. }
  19. return $data;
  20. }
  21. /**
  22. * @dataProvider parserTestDataProvider
  23. *
  24. * @param string $fixtureName name of the fixture
  25. */
  26. public function testParser($fixtureName)
  27. {
  28. $etalon = $this->parseEtalon($fixtureName . '.yml');
  29. $features = $this->parseFixture($fixtureName . '.feature');
  30. $this->assertInternalType('array', $features);
  31. $this->assertEquals(1, count($features));
  32. $fixture = $features[0];
  33. $this->assertEquals($etalon, $fixture);
  34. }
  35. public function testParserResetsTagsBetweenFeatures()
  36. {
  37. $parser = $this->getGherkinParser();
  38. $parser->parse(<<<FEATURE
  39. Feature:
  40. Scenario:
  41. Given step
  42. @skipped
  43. FEATURE
  44. );
  45. $feature2 = $parser->parse(<<<FEATURE
  46. Feature:
  47. Scenario:
  48. Given step
  49. FEATURE
  50. );
  51. $this->assertFalse($feature2->hasTags());
  52. }
  53. protected function getGherkinParser()
  54. {
  55. if (null === $this->gherkin) {
  56. $keywords = new ArrayKeywords(array(
  57. 'en' => array(
  58. 'feature' => 'Feature',
  59. 'background' => 'Background',
  60. 'scenario' => 'Scenario',
  61. 'scenario_outline' => 'Scenario Outline',
  62. 'examples' => 'Examples',
  63. 'given' => 'Given',
  64. 'when' => 'When',
  65. 'then' => 'Then',
  66. 'and' => 'And',
  67. 'but' => 'But'
  68. ),
  69. 'ru' => array(
  70. 'feature' => 'Функционал',
  71. 'background' => 'Предыстория',
  72. 'scenario' => 'Сценарий',
  73. 'scenario_outline' => 'Структура сценария',
  74. 'examples' => 'Примеры',
  75. 'given' => 'Допустим',
  76. 'when' => 'То',
  77. 'then' => 'Если',
  78. 'and' => 'И',
  79. 'but' => 'Но'
  80. ),
  81. 'ja' => array (
  82. 'feature' => 'フィーチャ',
  83. 'background' => '背景',
  84. 'scenario' => 'シナリオ',
  85. 'scenario_outline' => 'シナリオアウトライン',
  86. 'examples' => '例|サンプル',
  87. 'given' => '前提<',
  88. 'when' => 'もし<',
  89. 'then' => 'ならば<',
  90. 'and' => 'かつ<',
  91. 'but' => 'しかし<'
  92. )
  93. ));
  94. $this->gherkin = new Parser(new Lexer($keywords));
  95. }
  96. return $this->gherkin;
  97. }
  98. protected function getYamlParser()
  99. {
  100. if (null === $this->yaml) {
  101. $this->yaml = new YamlFileLoader();
  102. }
  103. return $this->yaml;
  104. }
  105. protected function parseFixture($fixture)
  106. {
  107. $file = __DIR__ . '/Fixtures/features/' . $fixture;
  108. return array($this->getGherkinParser()->parse(file_get_contents($file), $file));
  109. }
  110. protected function parseEtalon($etalon)
  111. {
  112. $features = $this->getYamlParser()->load(__DIR__ . '/Fixtures/etalons/' . $etalon);
  113. $feature = $features[0];
  114. return new FeatureNode(
  115. $feature->getTitle(),
  116. $feature->getDescription(),
  117. $feature->getTags(),
  118. $feature->getBackground(),
  119. $feature->getScenarios(),
  120. $feature->getKeyword(),
  121. $feature->getLanguage(),
  122. __DIR__ . '/Fixtures/features/' . basename($etalon, '.yml') . '.feature',
  123. $feature->getLine()
  124. );
  125. }
  126. }