Skip to content

Commit 8090029

Browse files
committed
Improve tag handling; update test
1 parent 5edc2f5 commit 8090029

File tree

8 files changed

+65
-121
lines changed

8 files changed

+65
-121
lines changed

src/Node/ExampleNode.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class ExampleNode implements ScenarioInterface, NamedScenarioInterface
1919
{
20+
use TaggedNodeTrait;
21+
2022
/**
2123
* @var string
2224
*/
@@ -105,33 +107,6 @@ public function getTitle()
105107
return $this->text;
106108
}
107109

108-
/**
109-
* Checks if outline is tagged with tag.
110-
*
111-
* @param string $tag
112-
*
113-
* @return bool
114-
*/
115-
public function hasTag($tag)
116-
{
117-
return in_array($tag, $this->getTags());
118-
}
119-
120-
/**
121-
* Checks if outline has tags (both inherited from feature and own).
122-
*
123-
* @return bool
124-
*/
125-
public function hasTags()
126-
{
127-
return count($this->getTags()) > 0;
128-
}
129-
130-
/**
131-
* Returns outline tags (including inherited from feature).
132-
*
133-
* @return string[]
134-
*/
135110
public function getTags()
136111
{
137112
return $this->tags;

src/Node/ExampleTableNode.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*
1616
* @author Konstantin Kudryashov <ever.zet@gmail.com>
1717
*/
18-
class ExampleTableNode extends TableNode
18+
class ExampleTableNode extends TableNode implements TaggedNodeInterface
1919
{
20+
use TaggedNodeTrait;
21+
2022
/**
2123
* @var string[]
2224
*/
@@ -52,11 +54,6 @@ public function getNodeType()
5254
return 'ExampleTable';
5355
}
5456

55-
/**
56-
* Returns attached tags.
57-
*
58-
* @return string[]
59-
*/
6057
public function getTags()
6158
{
6259
return $this->tags;

src/Node/FeatureNode.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
class FeatureNode implements KeywordNodeInterface, TaggedNodeInterface
2323
{
24+
use TaggedNodeTrait;
25+
2426
/**
2527
* @var string|null
2628
*/
@@ -136,33 +138,6 @@ public function getDescription()
136138
return $this->description;
137139
}
138140

139-
/**
140-
* Checks if feature is tagged with tag.
141-
*
142-
* @param string $tag
143-
*
144-
* @return bool
145-
*/
146-
public function hasTag($tag)
147-
{
148-
return in_array($tag, $this->tags);
149-
}
150-
151-
/**
152-
* Checks if feature has tags.
153-
*
154-
* @return bool
155-
*/
156-
public function hasTags()
157-
{
158-
return count($this->tags) > 0;
159-
}
160-
161-
/**
162-
* Returns feature tags.
163-
*
164-
* @return string[]
165-
*/
166141
public function getTags()
167142
{
168143
return $this->tags;

src/Node/OutlineNode.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class OutlineNode implements ScenarioInterface
1919
{
20+
use TaggedNodeTrait;
21+
2022
/**
2123
* @var string
2224
*/
@@ -92,33 +94,6 @@ public function getTitle()
9294
return $this->title;
9395
}
9496

95-
/**
96-
* Checks if outline is tagged with tag.
97-
*
98-
* @param string $tag
99-
*
100-
* @return bool
101-
*/
102-
public function hasTag($tag)
103-
{
104-
return in_array($tag, $this->getTags());
105-
}
106-
107-
/**
108-
* Checks if outline has tags (both inherited from feature and own).
109-
*
110-
* @return bool
111-
*/
112-
public function hasTags()
113-
{
114-
return count($this->getTags()) > 0;
115-
}
116-
117-
/**
118-
* Returns outline tags (including inherited from feature).
119-
*
120-
* @return string[]
121-
*/
12297
public function getTags()
12398
{
12499
return $this->tags;

src/Node/ScenarioNode.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class ScenarioNode implements ScenarioInterface, NamedScenarioInterface
1919
{
20+
use TaggedNodeTrait;
21+
2022
/**
2123
* @var string
2224
*/
@@ -83,33 +85,6 @@ public function getName(): ?string
8385
return $this->title;
8486
}
8587

86-
/**
87-
* Checks if scenario is tagged with tag.
88-
*
89-
* @param string $tag
90-
*
91-
* @return bool
92-
*/
93-
public function hasTag($tag)
94-
{
95-
return in_array($tag, $this->getTags());
96-
}
97-
98-
/**
99-
* Checks if scenario has tags (both inherited from feature and own).
100-
*
101-
* @return bool
102-
*/
103-
public function hasTags()
104-
{
105-
return count($this->getTags()) > 0;
106-
}
107-
108-
/**
109-
* Returns scenario tags (including inherited from feature).
110-
*
111-
* @return array
112-
*/
11388
public function getTags()
11489
{
11590
return $this->tags;

src/Node/TaggedNodeInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ interface TaggedNodeInterface extends NodeInterface
2727
public function hasTag($tag);
2828

2929
/**
30-
* Checks if node has tags (both inherited from feature and own).
30+
* Checks if node has tags (including any inherited tags e.g. from feature).
3131
*
3232
* @return bool
3333
*/
3434
public function hasTags();
3535

3636
/**
37-
* Returns node tags (including inherited from feature).
37+
* Returns node tags (including any inherited tags e.g. from feature).
3838
*
3939
* @return list<string>
4040
*/

src/Node/TaggedNodeTrait.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Behat Gherkin Parser.
5+
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Behat\Gherkin\Node;
12+
13+
/**
14+
* This trait partially implements {@see TaggedNodeInterface}.
15+
*/
16+
trait TaggedNodeTrait
17+
{
18+
/**
19+
* @return list<string>
20+
*/
21+
abstract public function getTags();
22+
23+
/**
24+
* @param string $tag
25+
*
26+
* @return bool
27+
*/
28+
public function hasTag($tag)
29+
{
30+
return in_array($tag, $this->getTags(), true);
31+
}
32+
33+
/**
34+
* @return bool
35+
*/
36+
public function hasTags()
37+
{
38+
return $this->getTags() !== [];
39+
}
40+
}

tests/Node/OutlineNodeTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ public function testPopulatesExampleWithOutlineTitle(): void
128128
new StepNode('', 'I am <name>', [], 1, 'Given'),
129129
];
130130

131-
$table = new ExampleTableNode([
132-
10 => ['name', 'email'],
133-
11 => ['Ciaran', 'ciaran@example.com'],
134-
12 => ['John', 'john@example.com'],
135-
], 'Examples');
131+
$table = new ExampleTableNode(
132+
[
133+
10 => ['name', 'email'],
134+
11 => ['Ciaran', 'ciaran@example.com'],
135+
12 => ['John', 'john@example.com'],
136+
],
137+
'Examples',
138+
['tagA', 'tagB'],
139+
);
136140

137141
$outline = new OutlineNode('An outline title for <name>', [], $steps, $table, '', 1);
138142

@@ -143,12 +147,14 @@ public function testPopulatesExampleWithOutlineTitle(): void
143147
'getTitle' => '| Ciaran | ciaran@example.com |',
144148
'getOutlineTitle' => 'An outline title for <name>',
145149
'getExampleText' => '| Ciaran | ciaran@example.com |',
150+
'getTags' => ['tagA', 'tagB'],
146151
],
147152
[
148153
'getName' => 'An outline title for John #2',
149154
'getTitle' => '| John | john@example.com |',
150155
'getOutlineTitle' => 'An outline title for <name>',
151156
'getExampleText' => '| John | john@example.com |',
157+
'getTags' => ['tagA', 'tagB'],
152158
],
153159
],
154160
array_map(
@@ -158,6 +164,7 @@ static function (ExampleNode $node) {
158164
'getTitle' => $node->getTitle(),
159165
'getOutlineTitle' => $node->getOutlineTitle(),
160166
'getExampleText' => $node->getExampleText(),
167+
'getTags' => $node->getTags(),
161168
];
162169
},
163170
$outline->getExamples()

0 commit comments

Comments
 (0)