GherkinTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Tests\Behat\Gherkin;
  3. use Behat\Gherkin\Gherkin;
  4. use Behat\Gherkin\Node\FeatureNode;
  5. use Behat\Gherkin\Node\ScenarioNode;
  6. class GherkinTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testLoader()
  9. {
  10. $customFilter1 = $this->getCustomFilterMock();
  11. $customFilter2 = $this->getCustomFilterMock();
  12. $gherkin = new Gherkin();
  13. $gherkin->addLoader($loader = $this->getLoaderMock());
  14. $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
  15. $gherkin->addFilter($tagFilter = $this->getTagFilterMock());
  16. $scenario = new ScenarioNode(null, array(), array(), null, null);
  17. $feature = new FeatureNode(null, null, array(), null, array($scenario), null, null, null, null);
  18. $loader
  19. ->expects($this->once())
  20. ->method('supports')
  21. ->with($resource = 'some/feature/resource')
  22. ->will($this->returnValue(true));
  23. $loader
  24. ->expects($this->once())
  25. ->method('load')
  26. ->with($resource)
  27. ->will($this->returnValue(array($feature)));
  28. $nameFilter
  29. ->expects($this->once())
  30. ->method('filterFeature')
  31. ->with($this->identicalTo($feature))
  32. ->will($this->returnValue($feature));
  33. $tagFilter
  34. ->expects($this->once())
  35. ->method('filterFeature')
  36. ->with($this->identicalTo($feature))
  37. ->will($this->returnValue($feature));
  38. $customFilter1
  39. ->expects($this->once())
  40. ->method('filterFeature')
  41. ->with($this->identicalTo($feature))
  42. ->will($this->returnValue($feature));
  43. $customFilter2
  44. ->expects($this->once())
  45. ->method('filterFeature')
  46. ->with($this->identicalTo($feature))
  47. ->will($this->returnValue($feature));
  48. $features = $gherkin->load($resource, array($customFilter1, $customFilter2));
  49. $this->assertEquals(1, count($features));
  50. $scenarios = $features[0]->getScenarios();
  51. $this->assertEquals(1, count($scenarios));
  52. $this->assertSame($scenario, $scenarios[0]);
  53. }
  54. public function testNotFoundLoader()
  55. {
  56. $gherkin = new Gherkin();
  57. $this->assertEquals(array(), $gherkin->load('some/feature/resource'));
  58. }
  59. public function testLoaderFiltersFeatures()
  60. {
  61. $gherkin = new Gherkin();
  62. $gherkin->addLoader($loader = $this->getLoaderMock());
  63. $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
  64. $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
  65. $loader
  66. ->expects($this->once())
  67. ->method('supports')
  68. ->with($resource = 'some/feature/resource')
  69. ->will($this->returnValue(true));
  70. $loader
  71. ->expects($this->once())
  72. ->method('load')
  73. ->with($resource)
  74. ->will($this->returnValue(array($feature)));
  75. $nameFilter
  76. ->expects($this->once())
  77. ->method('filterFeature')
  78. ->with($this->identicalTo($feature))
  79. ->will($this->returnValue($feature));
  80. $nameFilter
  81. ->expects($this->once())
  82. ->method('isFeatureMatch')
  83. ->with($this->identicalTo($feature))
  84. ->will($this->returnValue(false));
  85. $features = $gherkin->load($resource);
  86. $this->assertEquals(0, count($features));
  87. }
  88. public function testSetFiltersOverridesAllFilters()
  89. {
  90. $gherkin = new Gherkin();
  91. $gherkin->addLoader($loader = $this->getLoaderMock());
  92. $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
  93. $gherkin->setFilters(array());
  94. $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
  95. $loader
  96. ->expects($this->once())
  97. ->method('supports')
  98. ->with($resource = 'some/feature/resource')
  99. ->will($this->returnValue(true));
  100. $loader
  101. ->expects($this->once())
  102. ->method('load')
  103. ->with($resource)
  104. ->will($this->returnValue(array($feature)));
  105. $nameFilter
  106. ->expects($this->never())
  107. ->method('filterFeature');
  108. $nameFilter
  109. ->expects($this->never())
  110. ->method('isFeatureMatch');
  111. $features = $gherkin->load($resource);
  112. $this->assertEquals(1, count($features));
  113. }
  114. public function testSetBasePath()
  115. {
  116. $gherkin = new Gherkin();
  117. $gherkin->addLoader($loader1 = $this->getLoaderMock());
  118. $gherkin->addLoader($loader2 = $this->getLoaderMock());
  119. $loader1
  120. ->expects($this->once())
  121. ->method('setBasePath')
  122. ->with($basePath = '/base/path')
  123. ->will($this->returnValue(null));
  124. $loader2
  125. ->expects($this->once())
  126. ->method('setBasePath')
  127. ->with($basePath = '/base/path')
  128. ->will($this->returnValue(null));
  129. $gherkin->setBasePath($basePath);
  130. }
  131. protected function getLoaderMock()
  132. {
  133. return $this->getMockBuilder('Behat\Gherkin\Loader\GherkinFileLoader')
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. }
  137. protected function getCustomFilterMock()
  138. {
  139. return $this->getMockBuilder('Behat\Gherkin\Filter\FilterInterface')
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. }
  143. protected function getNameFilterMock()
  144. {
  145. return $this->getMockBuilder('Behat\Gherkin\Filter\NameFilter')
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. }
  149. protected function getTagFilterMock()
  150. {
  151. return $this->getMockBuilder('Behat\Gherkin\Filter\TagFilter')
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. }
  155. }