Skip to content

Commit 032113a

Browse files
committed
Update code according to situation; fix CR comment
1 parent 70cb0f3 commit 032113a

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

src/Loader/CucumberNDJsonAstLoader.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static function getFeature(array $json, $filePath)
5252

5353
return new FeatureNode(
5454
$featureJson['name'] ?? null,
55-
trim($featureJson['description'] ?? '') ?: null,
55+
$featureJson['description'] ? trim($featureJson['description']) : null,
5656
self::getTags($featureJson),
5757
self::getBackground($featureJson),
5858
self::getScenarios($featureJson),
@@ -158,17 +158,23 @@ private static function getTables(array $items): array
158158
static function ($tableJson) {
159159
$table = [];
160160

161-
if (!isset($tableJson['tableHeader'])) {
162-
throw new NodeException(
163-
sprintf(
164-
'Table header is required, but none was specified for the example on line %s.',
165-
$tableJson['location']['line'],
166-
)
167-
);
161+
if (isset($tableJson['tableHeader'])) {
162+
// Table header seems to have been deprecated, but is still in use (in cucumber test fixtures)
163+
$table[$tableJson['tableHeader']['location']['line']] = array_column($tableJson['tableHeader']['cells'], 'value');
164+
} else {
165+
// Otherwise, assume the header is the first row (which would be now required)
166+
$firstRow = array_shift($tableJson['tableBody']);
167+
if ($firstRow === null) {
168+
throw new NodeException(
169+
sprintf(
170+
'Expected either a table header or at least one row for the example on line %s but found none.',
171+
$tableJson['location']['line'],
172+
)
173+
);
174+
}
175+
$table[$firstRow['line']] = array_column($firstRow['cells'], 'value');
168176
}
169177

170-
$table[$tableJson['tableHeader']['location']['line']] = array_column($tableJson['tableHeader']['cells'], 'value');
171-
172178
foreach ($tableJson['tableBody'] as $bodyRow) {
173179
$table[$bodyRow['location']['line']] = array_column($bodyRow['cells'], 'value');
174180
}

tests/Loader/CucumberNDJsonAstLoaderTest.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ public function testValidLoading(): void
126126
);
127127
}
128128

129-
public function testOutlineTableHeaderIsRequired(): void
129+
public function testOutlineTableHeaderOrNonEmptyTableIsRequired(): void
130130
{
131-
$this->expectException(NodeException::class);
132-
$this->expectExceptionMessage('Table header is required, but none was specified for the example on line 3.');
131+
$this->expectExceptionObject(
132+
new NodeException('Expected either a table header or at least one row for the example on line 3 but found none.')
133+
);
133134

134135
$this->loader->load($this->serializeCucumberMessagesToFile([
135136
'gherkinDocument' => [
@@ -143,20 +144,7 @@ public function testOutlineTableHeaderIsRequired(): void
143144
'examples' => [
144145
[
145146
'location' => ['line' => 3],
146-
'tableBody' => [
147-
[
148-
'cells' => [
149-
['value' => 'A1'],
150-
['value' => 'B1'],
151-
],
152-
],
153-
[
154-
'cells' => [
155-
['value' => 'A2'],
156-
['value' => 'B2'],
157-
],
158-
],
159-
],
147+
'tableBody' => [],
160148
],
161149
],
162150
],

0 commit comments

Comments
 (0)