Skip to content

Commit d899102

Browse files
authored
Merge pull request #289 from uuf6429/feature/improve-tags-handling
Improve tag handling; update test
2 parents 47ee3db + 684860c commit d899102

File tree

11 files changed

+204
-179
lines changed

11 files changed

+204
-179
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* @internal
17+
*/
18+
trait TaggedNodeTrait
19+
{
20+
/**
21+
* @return list<string>
22+
*/
23+
abstract public function getTags();
24+
25+
/**
26+
* @param string $tag
27+
*
28+
* @return bool
29+
*/
30+
public function hasTag($tag)
31+
{
32+
return in_array($tag, $this->getTags(), true);
33+
}
34+
35+
/**
36+
* @return bool
37+
*/
38+
public function hasTags()
39+
{
40+
return $this->getTags() !== [];
41+
}
42+
}

tests/Loader/ArrayLoaderTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ public function testLoadFeatures(): void
6060

6161
$this->assertCount(2, $features);
6262

63-
$this->assertEquals(3, $features[0]->getLine());
64-
$this->assertEquals('First feature', $features[0]->getTitle());
63+
$this->assertSame(3, $features[0]->getLine());
64+
$this->assertSame('First feature', $features[0]->getTitle());
6565
$this->assertNull($features[0]->getDescription());
6666
$this->assertNull($features[0]->getFile());
67-
$this->assertEquals('en', $features[0]->getLanguage());
68-
$this->assertFalse($features[0]->hasTags());
67+
$this->assertSame('en', $features[0]->getLanguage());
68+
$this->assertSame([], $features[0]->getTags());
6969

70-
$this->assertEquals(1, $features[1]->getLine());
70+
$this->assertSame(1, $features[1]->getLine());
7171
$this->assertNull($features[1]->getTitle());
72-
$this->assertEquals('Second feature description', $features[1]->getDescription());
72+
$this->assertSame('Second feature description', $features[1]->getDescription());
7373
$this->assertNull($features[1]->getFile());
74-
$this->assertEquals('ru', $features[1]->getLanguage());
75-
$this->assertEquals(['some', 'tags'], $features[1]->getTags());
74+
$this->assertSame('ru', $features[1]->getLanguage());
75+
$this->assertSame(['some', 'tags'], $features[1]->getTags());
7676
}
7777

7878
public function testLoadScenarios(): void
@@ -105,19 +105,19 @@ public function testLoadScenarios(): void
105105
$this->assertCount(3, $scenarios);
106106

107107
$this->assertInstanceOf(ScenarioNode::class, $scenarios[0]);
108-
$this->assertEquals('First scenario', $scenarios[0]->getTitle());
109-
$this->assertFalse($scenarios[0]->hasTags());
110-
$this->assertEquals(2, $scenarios[0]->getLine());
108+
$this->assertSame('First scenario', $scenarios[0]->getTitle());
109+
$this->assertSame([], $scenarios[0]->getTags());
110+
$this->assertSame(2, $scenarios[0]->getLine());
111111

112112
$this->assertInstanceOf(ScenarioNode::class, $scenarios[1]);
113113
$this->assertNull($scenarios[1]->getTitle());
114-
$this->assertEquals(['second', 'scenario', 'tags'], $scenarios[1]->getTags());
115-
$this->assertEquals(1, $scenarios[1]->getLine());
114+
$this->assertSame(['second', 'scenario', 'tags'], $scenarios[1]->getTags());
115+
$this->assertSame(1, $scenarios[1]->getLine());
116116

117117
$this->assertInstanceOf(ScenarioNode::class, $scenarios[2]);
118118
$this->assertNull($scenarios[2]->getTitle());
119-
$this->assertEquals(['third', 'scenario'], $scenarios[2]->getTags());
120-
$this->assertEquals(3, $scenarios[2]->getLine());
119+
$this->assertSame(['third', 'scenario'], $scenarios[2]->getTags());
120+
$this->assertSame(3, $scenarios[2]->getLine());
121121
}
122122

123123
public function testLoadOutline(): void
@@ -148,14 +148,14 @@ public function testLoadOutline(): void
148148
$this->assertCount(2, $outlines);
149149

150150
$this->assertInstanceOf(OutlineNode::class, $outlines[0]);
151-
$this->assertEquals('First outline', $outlines[0]->getTitle());
152-
$this->assertFalse($outlines[0]->hasTags());
153-
$this->assertEquals(2, $outlines[0]->getLine());
151+
$this->assertSame('First outline', $outlines[0]->getTitle());
152+
$this->assertSame([], $outlines[0]->getTags());
153+
$this->assertSame(2, $outlines[0]->getLine());
154154

155155
$this->assertInstanceOf(OutlineNode::class, $outlines[1]);
156156
$this->assertNull($outlines[1]->getTitle());
157-
$this->assertEquals(['second', 'outline', 'tags'], $outlines[1]->getTags());
158-
$this->assertEquals(1, $outlines[1]->getLine());
157+
$this->assertSame(['second', 'outline', 'tags'], $outlines[1]->getTags());
158+
$this->assertSame(1, $outlines[1]->getLine());
159159
}
160160

161161
public function testOutlineExamples(): void

0 commit comments

Comments
 (0)