Repository: Behat/Gherkin (php)

Issue:
Background should not be allowed after the first Scenario
We currently parse the Background and Scenario out of a file like this:

```
Feature: Whatever

   Scenario: Something
     Then I should fail

   Background:
     Given I am in the wrong sequence
```

However, cucumber/gherkin will report this as a parse error, because it does not allow Background nodes after the first Scenario.

There is not yet any testdata for this case, either here or upstream.

See #323 

Pull request description:
change: Throw ParserException if Background comes after first Scenario
Gherkin requires that any Feature `Background:` must come before the first Scenario / Scenario Outline in a file (to match the execution order of these steps).

We were previously allowing the background to appear anywhere at the top (Feature) level of the file. This will now throw a ParserException.

Although this could theoretically cause existing features to break, I do not consider it to be a BC break - we are now just being stricter that the input matches the expected schema.

Fixes #328

Patch:
diff --git a/src/Parser.php b/src/Parser.php
index f3361f2..befdc6b 100644
--- a/src/Parser.php
+++ b/src/Parser.php
@@ -224,7 +224,9 @@ class Parser
                 continue;
             }
 
-            if (!$background && $node instanceof BackgroundNode) {
+            $isBackgroundAllowed = ($background === null && $scenarios === []);
+
+            if ($isBackgroundAllowed && $node instanceof BackgroundNode) {
                 $background = $node;
                 continue;
             }
@@ -235,7 +237,7 @@ class Parser
             }
 
             throw new UnexpectedParserNodeException(
-                match ($background === null && $scenarios === []) {
+                match ($isBackgroundAllowed) {
                     true => 'Background, Scenario or Outline',
                     false => 'Scenario or Outline',
                 },


Test patch:
diff --git a/tests/Cucumber/extra_testdata/bad/background_after_scenario.feature b/tests/Cucumber/extra_testdata/bad/background_after_scenario.feature
new file mode 100644
index 0000000..c57e1a3
--- /dev/null
+++ b/tests/Cucumber/extra_testdata/bad/background_after_scenario.feature
@@ -0,0 +1,7 @@
+Feature: Feature background cannot come after first scenario
+
+    Scenario:
+        Then I should fail
+
+    Background:
+        Given I am in the wrong sequence
diff --git a/tests/Cucumber/extra_testdata/bad/background_after_scenario.feature.errors.ndjson b/tests/Cucumber/extra_testdata/bad/background_after_scenario.feature.errors.ndjson
new file mode 100644
index 0000000..7c0ee49
--- /dev/null
+++ b/tests/Cucumber/extra_testdata/bad/background_after_scenario.feature.errors.ndjson
@@ -0,0 +1,1 @@
+{"parseError":{"message":"(6:5): expected: #EOF, #TableRow, #DocStringSeparator, #StepLine, #TagLine, #ExamplesLine, #ScenarioLine, #RuleLine, #Comment, #Empty, got 'Background:'","source":{"location":{"column":5,"line":6},"uri":"../bad/background_after_scenario.feature"}}}