3 namespace Tests\Behat\Gherkin;
5 use Behat\Gherkin\Gherkin;
6 use Behat\Gherkin\Node\FeatureNode;
7 use Behat\Gherkin\Node\ScenarioNode;
9 class GherkinTest extends \PHPUnit_Framework_TestCase
11 public function testLoader()
13 $customFilter1 = $this->getCustomFilterMock();
14 $customFilter2 = $this->getCustomFilterMock();
16 $gherkin = new Gherkin();
17 $gherkin->addLoader($loader = $this->getLoaderMock());
18 $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
19 $gherkin->addFilter($tagFilter = $this->getTagFilterMock());
21 $scenario = new ScenarioNode(null, array(), array(), null, null);
22 $feature = new FeatureNode(null, null, array(), null, array($scenario), null, null, null, null);
25 ->expects($this->once())
27 ->with($resource = 'some/feature/resource')
28 ->will($this->returnValue(true));
30 ->expects($this->once())
33 ->will($this->returnValue(array($feature)));
36 ->expects($this->once())
37 ->method('filterFeature')
38 ->with($this->identicalTo($feature))
39 ->will($this->returnValue($feature));
41 ->expects($this->once())
42 ->method('filterFeature')
43 ->with($this->identicalTo($feature))
44 ->will($this->returnValue($feature));
46 ->expects($this->once())
47 ->method('filterFeature')
48 ->with($this->identicalTo($feature))
49 ->will($this->returnValue($feature));
51 ->expects($this->once())
52 ->method('filterFeature')
53 ->with($this->identicalTo($feature))
54 ->will($this->returnValue($feature));
56 $features = $gherkin->load($resource, array($customFilter1, $customFilter2));
57 $this->assertEquals(1, count($features));
59 $scenarios = $features[0]->getScenarios();
60 $this->assertEquals(1, count($scenarios));
61 $this->assertSame($scenario, $scenarios[0]);
64 public function testNotFoundLoader()
66 $gherkin = new Gherkin();
68 $this->assertEquals(array(), $gherkin->load('some/feature/resource'));
71 public function testLoaderFiltersFeatures()
73 $gherkin = new Gherkin();
74 $gherkin->addLoader($loader = $this->getLoaderMock());
75 $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
77 $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
80 ->expects($this->once())
82 ->with($resource = 'some/feature/resource')
83 ->will($this->returnValue(true));
85 ->expects($this->once())
88 ->will($this->returnValue(array($feature)));
91 ->expects($this->once())
92 ->method('filterFeature')
93 ->with($this->identicalTo($feature))
94 ->will($this->returnValue($feature));
96 ->expects($this->once())
97 ->method('isFeatureMatch')
98 ->with($this->identicalTo($feature))
99 ->will($this->returnValue(false));
101 $features = $gherkin->load($resource);
102 $this->assertEquals(0, count($features));
105 public function testSetFiltersOverridesAllFilters()
107 $gherkin = new Gherkin();
108 $gherkin->addLoader($loader = $this->getLoaderMock());
109 $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
110 $gherkin->setFilters(array());
112 $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
115 ->expects($this->once())
117 ->with($resource = 'some/feature/resource')
118 ->will($this->returnValue(true));
120 ->expects($this->once())
123 ->will($this->returnValue(array($feature)));
126 ->expects($this->never())
127 ->method('filterFeature');
129 ->expects($this->never())
130 ->method('isFeatureMatch');
132 $features = $gherkin->load($resource);
133 $this->assertEquals(1, count($features));
136 public function testSetBasePath()
138 $gherkin = new Gherkin();
139 $gherkin->addLoader($loader1 = $this->getLoaderMock());
140 $gherkin->addLoader($loader2 = $this->getLoaderMock());
143 ->expects($this->once())
144 ->method('setBasePath')
145 ->with($basePath = '/base/path')
146 ->will($this->returnValue(null));
149 ->expects($this->once())
150 ->method('setBasePath')
151 ->with($basePath = '/base/path')
152 ->will($this->returnValue(null));
154 $gherkin->setBasePath($basePath);
157 protected function getLoaderMock()
159 return $this->getMockBuilder('Behat\Gherkin\Loader\GherkinFileLoader')
160 ->disableOriginalConstructor()
164 protected function getCustomFilterMock()
166 return $this->getMockBuilder('Behat\Gherkin\Filter\FilterInterface')
167 ->disableOriginalConstructor()
171 protected function getNameFilterMock()
173 return $this->getMockBuilder('Behat\Gherkin\Filter\NameFilter')
174 ->disableOriginalConstructor()
178 protected function getTagFilterMock()
180 return $this->getMockBuilder('Behat\Gherkin\Filter\TagFilter')
181 ->disableOriginalConstructor()