ArrayLoaderTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. namespace Tests\Behat\Gherkin\Loader;
  3. use Behat\Gherkin\Loader\ArrayLoader;
  4. class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
  5. {
  6. private $loader;
  7. protected function setUp()
  8. {
  9. $this->loader = new ArrayLoader();
  10. }
  11. public function testSupports()
  12. {
  13. $this->assertFalse($this->loader->supports(__DIR__));
  14. $this->assertFalse($this->loader->supports(__FILE__));
  15. $this->assertFalse($this->loader->supports('string'));
  16. $this->assertFalse($this->loader->supports(array('wrong_root')));
  17. $this->assertFalse($this->loader->supports(array('features')));
  18. $this->assertTrue($this->loader->supports(array('features' => array())));
  19. $this->assertTrue($this->loader->supports(array('feature' => array())));
  20. }
  21. public function testLoadEmpty()
  22. {
  23. $this->assertEquals(array(), $this->loader->load(array('features' => array())));
  24. }
  25. public function testLoadFeatures()
  26. {
  27. $features = $this->loader->load(array(
  28. 'features' => array(
  29. array(
  30. 'title' => 'First feature',
  31. 'line' => 3,
  32. ),
  33. array(
  34. 'description' => 'Second feature description',
  35. 'language' => 'ru',
  36. 'tags' => array('some', 'tags')
  37. )
  38. ),
  39. ));
  40. $this->assertEquals(2, count($features));
  41. $this->assertEquals(3, $features[0]->getLine());
  42. $this->assertEquals('First feature', $features[0]->getTitle());
  43. $this->assertNull($features[0]->getDescription());
  44. $this->assertNull($features[0]->getFile());
  45. $this->assertEquals('en', $features[0]->getLanguage());
  46. $this->assertFalse($features[0]->hasTags());
  47. $this->assertEquals(1, $features[1]->getLine());
  48. $this->assertNull($features[1]->getTitle());
  49. $this->assertEquals('Second feature description', $features[1]->getDescription());
  50. $this->assertNull($features[1]->getFile());
  51. $this->assertEquals('ru', $features[1]->getLanguage());
  52. $this->assertEquals(array('some', 'tags'), $features[1]->getTags());
  53. }
  54. public function testLoadScenarios()
  55. {
  56. $features = $this->loader->load(array(
  57. 'features' => array(
  58. array(
  59. 'title' => 'Feature',
  60. 'scenarios' => array(
  61. array(
  62. 'title' => 'First scenario',
  63. 'line' => 2
  64. ),
  65. array(
  66. 'tags' => array('second', 'scenario', 'tags')
  67. ),
  68. array(
  69. 'tags' => array('third', 'scenario'),
  70. 'line' => 3
  71. )
  72. )
  73. )
  74. ),
  75. ));
  76. $this->assertEquals(1, count($features));
  77. $scenarios = $features[0]->getScenarios();
  78. $this->assertEquals(3, count($scenarios));
  79. $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[0]);
  80. $this->assertEquals('First scenario', $scenarios[0]->getTitle());
  81. $this->assertFalse($scenarios[0]->hasTags());
  82. $this->assertEquals(2, $scenarios[0]->getLine());
  83. $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[1]);
  84. $this->assertNull($scenarios[1]->getTitle());
  85. $this->assertEquals(array('second', 'scenario', 'tags'), $scenarios[1]->getTags());
  86. $this->assertEquals(1, $scenarios[1]->getLine());
  87. $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[2]);
  88. $this->assertNull($scenarios[2]->getTitle());
  89. $this->assertEquals(array('third', 'scenario'), $scenarios[2]->getTags());
  90. $this->assertEquals(3, $scenarios[2]->getLine());
  91. }
  92. public function testLoadOutline()
  93. {
  94. $features = $this->loader->load(array(
  95. 'features' => array(
  96. array(
  97. 'title' => 'Feature',
  98. 'scenarios' => array(
  99. array(
  100. 'type' => 'outline',
  101. 'title' => 'First outline',
  102. 'line' => 2
  103. ),
  104. array(
  105. 'type' => 'outline',
  106. 'tags' => array('second', 'outline', 'tags')
  107. )
  108. )
  109. )
  110. ),
  111. ));
  112. $this->assertEquals(1, count($features));
  113. $outlines = $features[0]->getScenarios();
  114. $this->assertEquals(2, count($outlines));
  115. $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[0]);
  116. $this->assertEquals('First outline', $outlines[0]->getTitle());
  117. $this->assertFalse($outlines[0]->hasTags());
  118. $this->assertEquals(2, $outlines[0]->getLine());
  119. $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[1]);
  120. $this->assertNull($outlines[1]->getTitle());
  121. $this->assertEquals(array('second', 'outline', 'tags'), $outlines[1]->getTags());
  122. $this->assertEquals(1, $outlines[1]->getLine());
  123. }
  124. public function testOutlineExamples()
  125. {
  126. $features = $this->loader->load(array(
  127. 'features' => array(
  128. array(
  129. 'title' => 'Feature',
  130. 'scenarios' => array(
  131. array(
  132. 'type' => 'outline',
  133. 'title' => 'First outline',
  134. 'line' => 2,
  135. 'examples' => array(
  136. array('user', 'pass'),
  137. array('ever', 'sdsd'),
  138. array('anto', 'fdfd')
  139. )
  140. ),
  141. array(
  142. 'type' => 'outline',
  143. 'tags' => array('second', 'outline', 'tags')
  144. )
  145. )
  146. )
  147. ),
  148. ));
  149. $this->assertEquals(1, count($features));
  150. $scenarios = $features[0]->getScenarios();
  151. $scenario = $scenarios[0];
  152. $this->assertEquals(
  153. array(array('user' => 'ever', 'pass' => 'sdsd'), array('user' => 'anto', 'pass' => 'fdfd')),
  154. $scenario->getExampleTable()->getHash()
  155. );
  156. }
  157. public function testLoadBackground()
  158. {
  159. $features = $this->loader->load(array(
  160. 'features' => array(
  161. array(
  162. ),
  163. array(
  164. 'background' => array()
  165. ),
  166. array(
  167. 'background' => array(
  168. 'line' => 2
  169. )
  170. ),
  171. )
  172. ));
  173. $this->assertEquals(3, count($features));
  174. $this->assertFalse($features[0]->hasBackground());
  175. $this->assertTrue($features[1]->hasBackground());
  176. $this->assertEquals(0, $features[1]->getBackground()->getLine());
  177. $this->assertTrue($features[2]->hasBackground());
  178. $this->assertEquals(2, $features[2]->getBackground()->getLine());
  179. }
  180. public function testLoadSteps()
  181. {
  182. $features = $this->loader->load(array(
  183. 'features' => array(
  184. array(
  185. 'background' => array(
  186. 'steps' => array(
  187. array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'bg step 1', 'line' => 3),
  188. array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'bg step 2')
  189. )
  190. ),
  191. 'scenarios' => array(
  192. array(
  193. 'title' => 'Scenario',
  194. 'steps' => array(
  195. array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'sc step 1'),
  196. array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'sc step 2')
  197. )
  198. ),
  199. array(
  200. 'title' => 'Outline',
  201. 'type' => 'outline',
  202. 'steps' => array(
  203. array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'out step 1'),
  204. array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'out step 2')
  205. )
  206. )
  207. )
  208. )
  209. )
  210. ));
  211. $background = $features[0]->getBackground();
  212. $this->assertTrue($background->hasSteps());
  213. $this->assertEquals(2, count($background->getSteps()));
  214. $steps = $background->getSteps();
  215. $this->assertEquals('Gangway!', $steps[0]->getType());
  216. $this->assertEquals('Gangway!', $steps[0]->getKeyword());
  217. $this->assertEquals('Given', $steps[0]->getKeywordType());
  218. $this->assertEquals('bg step 1', $steps[0]->getText());
  219. $this->assertEquals(3, $steps[0]->getLine());
  220. $this->assertEquals('Blimey!', $steps[1]->getType());
  221. $this->assertEquals('Blimey!', $steps[1]->getKeyword());
  222. $this->assertEquals('When', $steps[1]->getKeywordType());
  223. $this->assertEquals('bg step 2', $steps[1]->getText());
  224. $this->assertEquals(1, $steps[1]->getLine());
  225. $scenarios = $features[0]->getScenarios();
  226. $scenario = $scenarios[0];
  227. $this->assertTrue($scenario->hasSteps());
  228. $this->assertEquals(2, count($scenario->getSteps()));
  229. $steps = $scenario->getSteps();
  230. $this->assertEquals('Gangway!', $steps[0]->getType());
  231. $this->assertEquals('Gangway!', $steps[0]->getKeyword());
  232. $this->assertEquals('Given', $steps[0]->getKeywordType());
  233. $this->assertEquals('sc step 1', $steps[0]->getText());
  234. $this->assertEquals(0, $steps[0]->getLine());
  235. $this->assertEquals('Blimey!', $steps[1]->getType());
  236. $this->assertEquals('Blimey!', $steps[1]->getKeyword());
  237. $this->assertEquals('When', $steps[1]->getKeywordType());
  238. $this->assertEquals('sc step 2', $steps[1]->getText());
  239. $this->assertEquals(1, $steps[1]->getLine());
  240. $outline = $scenarios[1];
  241. $this->assertTrue($outline->hasSteps());
  242. $this->assertEquals(2, count($outline->getSteps()));
  243. $steps = $outline->getSteps();
  244. $this->assertEquals('Gangway!', $steps[0]->getType());
  245. $this->assertEquals('Gangway!', $steps[0]->getKeyword());
  246. $this->assertEquals('Given', $steps[0]->getKeywordType());
  247. $this->assertEquals('out step 1', $steps[0]->getText());
  248. $this->assertEquals(0, $steps[0]->getLine());
  249. $this->assertEquals('Blimey!', $steps[1]->getType());
  250. $this->assertEquals('Blimey!', $steps[1]->getKeyword());
  251. $this->assertEquals('When', $steps[1]->getKeywordType());
  252. $this->assertEquals('out step 2', $steps[1]->getText());
  253. $this->assertEquals(1, $steps[1]->getLine());
  254. }
  255. public function testLoadStepArguments()
  256. {
  257. $features = $this->loader->load(array(
  258. 'features' => array(
  259. array(
  260. 'background' => array(
  261. 'steps' => array(
  262. array(
  263. 'type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'step with table argument',
  264. 'arguments' => array(
  265. array(
  266. 'type' => 'table',
  267. 'rows' => array(
  268. array('key', 'val'),
  269. array(1, 2),
  270. array(3, 4)
  271. )
  272. )
  273. )
  274. ),
  275. array(
  276. 'type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'step with pystring argument',
  277. 'arguments' => array(
  278. array(
  279. 'type' => 'pystring',
  280. 'text' => ' some text',
  281. )
  282. )
  283. ),
  284. array(
  285. 'type' => 'Let go and haul', 'keyword_type' => 'Then', 'text' => '2nd step with pystring argument',
  286. 'arguments' => array(
  287. array(
  288. 'type' => 'pystring',
  289. 'text' => 'some text',
  290. )
  291. )
  292. )
  293. )
  294. )
  295. )
  296. )
  297. ));
  298. $background = $features[0]->getBackground();
  299. $this->assertTrue($background->hasSteps());
  300. $steps = $background->getSteps();
  301. $this->assertEquals(3, count($steps));
  302. $arguments = $steps[0]->getArguments();
  303. $this->assertEquals('Gangway!', $steps[0]->getType());
  304. $this->assertEquals('Gangway!', $steps[0]->getKeyword());
  305. $this->assertEquals('Given', $steps[0]->getKeywordType());
  306. $this->assertEquals('step with table argument', $steps[0]->getText());
  307. $this->assertInstanceOf('Behat\Gherkin\Node\TableNode', $arguments[0]);
  308. $this->assertEquals(array(array('key'=>1, 'val'=>2), array('key'=>3,'val'=>4)), $arguments[0]->getHash());
  309. $arguments = $steps[1]->getArguments();
  310. $this->assertEquals('Blimey!', $steps[1]->getType());
  311. $this->assertEquals('Blimey!', $steps[1]->getKeyword());
  312. $this->assertEquals('When', $steps[1]->getKeywordType());
  313. $this->assertEquals('step with pystring argument', $steps[1]->getText());
  314. $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
  315. $this->assertEquals(' some text', (string) $arguments[0]);
  316. $arguments = $steps[2]->getArguments();
  317. $this->assertEquals('Let go and haul', $steps[2]->getType());
  318. $this->assertEquals('Let go and haul', $steps[2]->getKeyword());
  319. $this->assertEquals('Then', $steps[2]->getKeywordType());
  320. $this->assertEquals('2nd step with pystring argument', $steps[2]->getText());
  321. $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
  322. $this->assertEquals('some text', (string) $arguments[0]);
  323. }
  324. public function testSingleFeatureArray()
  325. {
  326. $features = $this->loader->load(array(
  327. 'feature' => array(
  328. 'title' => 'Some feature'
  329. )
  330. ));
  331. $this->assertEquals(1, count($features));
  332. $this->assertEquals('Some feature', $features[0]->getTitle());
  333. }
  334. }