GherkinTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Class GherkinTest
  4. * @group gherkin
  5. */
  6. class GherkinTest extends \Codeception\Test\Unit
  7. {
  8. protected $feature;
  9. public static $calls = '';
  10. /**
  11. * @var \Codeception\Test\Loader\Gherkin
  12. */
  13. protected $loader;
  14. protected function _before()
  15. {
  16. $this->loader = new \Codeception\Test\Loader\Gherkin(
  17. [
  18. 'gherkin' => [
  19. 'contexts' => [
  20. 'default' => ['GherkinTestContext']
  21. ]
  22. ]
  23. ]
  24. );
  25. self::$calls = '';
  26. }
  27. protected function getServices()
  28. {
  29. return [
  30. 'di' => new \Codeception\Lib\Di(),
  31. 'dispatcher' => new \Codeception\Util\Maybe(),
  32. 'modules' => \Codeception\Util\Stub::makeEmpty('Codeception\Lib\ModuleContainer')
  33. ];
  34. }
  35. public function testLoadGherkin()
  36. {
  37. $this->loader->loadTests(codecept_data_dir('refund.feature'));
  38. $tests = $this->loader->getTests();
  39. $this->assertCount(1, $tests);
  40. /** @var $test \Codeception\Test\Gherkin * */
  41. $test = $tests[0];
  42. $this->assertInstanceOf('\Codeception\Test\Gherkin', $test);
  43. $this->assertEquals('Jeff returns a faulty microwave', $test->getFeature());
  44. }
  45. /**
  46. * @depends testLoadGherkin
  47. */
  48. public function testLoadWithContexts()
  49. {
  50. $this->loader->loadTests(codecept_data_dir('refund.feature'));
  51. $test = $this->loader->getTests()[0];
  52. /** @var $test \Codeception\Test\Gherkin * */
  53. $test->getMetadata()->setServices($this->getServices());
  54. $test->test();
  55. $this->assertEquals('abc', self::$calls);
  56. }
  57. /**
  58. * @depends testLoadGherkin
  59. * @expectedException \Codeception\Exception\ParseException
  60. */
  61. public function testBadRegex()
  62. {
  63. $this->loader = new \Codeception\Test\Loader\Gherkin(
  64. [
  65. 'gherkin' => [
  66. 'contexts' => [
  67. 'default' => ['GherkinInvalidContext'],
  68. ]
  69. ]
  70. ]
  71. );
  72. $this->loader->loadTests(codecept_data_dir('refund.feature'));
  73. $test = $this->loader->getTests()[0];
  74. /** @var $test \Codeception\Test\Gherkin * */
  75. $test->getMetadata()->setServices($this->getServices());
  76. $test->test();
  77. }
  78. public function testTags()
  79. {
  80. $this->loader = new \Codeception\Test\Loader\Gherkin(
  81. [
  82. 'gherkin' => [
  83. 'contexts' => [
  84. 'default' => ['GherkinTestContext'],
  85. 'tag' => [
  86. 'important' => ['TagGherkinContext']
  87. ]
  88. ]
  89. ]
  90. ]
  91. );
  92. $this->loader->loadTests(codecept_data_dir('refund.feature'));
  93. $test = $this->loader->getTests()[0];
  94. /** @var $test \Codeception\Test\Gherkin * */
  95. $test->getMetadata()->setServices($this->getServices());
  96. $test->test();
  97. $this->assertEquals('aXc', self::$calls);
  98. }
  99. public function testRoles()
  100. {
  101. $this->loader = new \Codeception\Test\Loader\Gherkin(
  102. [
  103. 'gherkin' => [
  104. 'contexts' => [
  105. 'default' => ['GherkinTestContext'],
  106. 'role' => [
  107. 'customer' => ['TagGherkinContext']
  108. ]
  109. ]
  110. ]
  111. ]
  112. );
  113. $this->loader->loadTests(codecept_data_dir('refund.feature'));
  114. $test = $this->loader->getTests()[0];
  115. /** @var $test \Codeception\Test\Gherkin * */
  116. $test->getMetadata()->setServices($this->getServices());
  117. $test->test();
  118. $this->assertEquals('aXc', self::$calls);
  119. }
  120. public function testMatchingPatterns()
  121. {
  122. $pattern = 'hello :name, are you from :place?';
  123. $regex = $this->loader->makePlaceholderPattern($pattern);
  124. $this->assertRegExp($regex, 'hello "davert", are you from "kiev"?');
  125. $this->assertNotRegExp($regex, 'hello davert, are you from "kiev"?');
  126. $pattern = 'hello ":name", how are you';
  127. $regex = $this->loader->makePlaceholderPattern($pattern);
  128. $this->assertRegExp($regex, 'hello "davert", how are you');
  129. $this->assertNotRegExp($regex, 'hello "davert", are you from "kiev"?');
  130. $pattern = 'there should be :num cow(s)';
  131. $regex = $this->loader->makePlaceholderPattern($pattern);
  132. $this->assertRegExp($regex, 'there should be "1" cow');
  133. $this->assertRegExp($regex, 'there should be "5" cows');
  134. $this->assertRegExp($regex, 'there should be 1000 cows');
  135. }
  136. public function testGherkinCurrencySymbols()
  137. {
  138. $pattern = 'I have :money in my pocket';
  139. $regex = $this->loader->makePlaceholderPattern($pattern);
  140. $this->assertRegExp($regex, 'I have 3.5$ in my pocket');
  141. $this->assertRegExp($regex, 'I have 3.5€ in my pocket');
  142. $this->assertRegExp($regex, 'I have $3.5 in my pocket');
  143. $this->assertRegExp($regex, 'I have £3.5 in my pocket');
  144. $this->assertRegExp($regex, 'I have "35.10" in my pocket');
  145. $this->assertRegExp($regex, 'I have 5 in my pocket');
  146. $this->assertRegExp($regex, 'I have 5.1 in my pocket');
  147. $this->assertNotRegExp($regex, 'I have 3.5 $ in my pocket');
  148. $this->assertNotRegExp($regex, 'I have 3.5euro in my pocket');
  149. // Issue #3156
  150. $pattern = "there is a :arg1 product witch costs :arg2 €";
  151. $regex = $this->loader->makePlaceholderPattern($pattern);
  152. $this->assertRegExp($regex, 'there is a "football ball" product witch costs "1,5" €');
  153. }
  154. public function testMatchingEscapedPatterns()
  155. {
  156. $pattern = 'use password ":pass"';
  157. $regex = $this->loader->makePlaceholderPattern($pattern);
  158. $this->assertRegExp($regex, 'use password "fref\"fr"');
  159. }
  160. /**
  161. * @Issue #3051
  162. */
  163. public function testSimilarSteps()
  164. {
  165. $pattern = 'there is a User called :arg1';
  166. $regex = $this->loader->makePlaceholderPattern($pattern);
  167. $this->assertRegExp($regex, 'there is a User called "John"');
  168. $this->assertNotRegExp($regex, 'there is a User called "John" and surname "Smith"');
  169. }
  170. public function testMultipleSteps()
  171. {
  172. $patterns = array_keys($this->loader->getSteps()['default']);
  173. $this->assertContains('/^he returns the microwave$/u', $patterns);
  174. $this->assertContains('/^microwave is brought back$/u', $patterns);
  175. }
  176. }
  177. class GherkinTestContext
  178. {
  179. /**
  180. * @Given Jeff has bought a microwave for :param
  181. */
  182. public function hasBoughtMicrowave()
  183. {
  184. GherkinTest::$calls .= 'a';
  185. }
  186. /**
  187. * @When he returns the microwave
  188. * @Then microwave is brought back
  189. */
  190. public function heReturns()
  191. {
  192. GherkinTest::$calls .= 'b';
  193. }
  194. /**
  195. * @Then Jeff should be refunded $100
  196. */
  197. public function beRefunded()
  198. {
  199. GherkinTest::$calls .= 'c';
  200. }
  201. }
  202. class TagGherkinContext
  203. {
  204. /**
  205. * @When he returns the microwave
  206. */
  207. public function heReturns()
  208. {
  209. GherkinTest::$calls .= 'X';
  210. }
  211. }
  212. class GherkinInvalidContext
  213. {
  214. /**
  215. * @Given /I (?:use:am connected to) the database (?db:.+)/i
  216. */
  217. public function notWorks()
  218. {
  219. }
  220. }