GherkinFileLoaderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Tests\Behat\Gherkin\Loader;
  3. use Behat\Gherkin\Keywords\CucumberKeywords;
  4. use Behat\Gherkin\Lexer;
  5. use Behat\Gherkin\Loader\GherkinFileLoader;
  6. use Behat\Gherkin\Parser;
  7. class GherkinFileLoaderTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var GherkinFileLoader
  11. */
  12. private $loader;
  13. private $featuresPath;
  14. public function testSupports()
  15. {
  16. $this->assertFalse($this->loader->supports('non-existent path'));
  17. $this->assertFalse($this->loader->supports('non-existent path:2'));
  18. $this->assertFalse($this->loader->supports(__DIR__));
  19. $this->assertFalse($this->loader->supports(__DIR__ . ':d'));
  20. $this->assertFalse($this->loader->supports(__FILE__));
  21. $this->assertTrue($this->loader->supports(__DIR__ . '/../Fixtures/features/pystring.feature'));
  22. }
  23. public function testLoad()
  24. {
  25. $features = $this->loader->load($this->featuresPath . '/pystring.feature');
  26. $this->assertEquals(1, count($features));
  27. $this->assertEquals('A py string feature', $features[0]->getTitle());
  28. $this->assertEquals($this->featuresPath . DIRECTORY_SEPARATOR . 'pystring.feature', $features[0]->getFile());
  29. $features = $this->loader->load($this->featuresPath . '/multiline_name.feature');
  30. $this->assertEquals(1, count($features));
  31. $this->assertEquals('multiline', $features[0]->getTitle());
  32. $this->assertEquals($this->featuresPath . DIRECTORY_SEPARATOR . 'multiline_name.feature', $features[0]->getFile());
  33. }
  34. public function testParsingUncachedFeature()
  35. {
  36. $cache = $this->getMockBuilder('Behat\Gherkin\Cache\CacheInterface')->getMock();
  37. $this->loader->setCache($cache);
  38. $cache->expects($this->once())
  39. ->method('isFresh')
  40. ->with($path = $this->featuresPath . DIRECTORY_SEPARATOR . 'pystring.feature', filemtime($path))
  41. ->will($this->returnValue(false));
  42. $cache->expects($this->once())
  43. ->method('write');
  44. $features = $this->loader->load($this->featuresPath . '/pystring.feature');
  45. $this->assertEquals(1, count($features));
  46. }
  47. public function testParsingCachedFeature()
  48. {
  49. $cache = $this->getMockBuilder('Behat\Gherkin\Cache\CacheInterface')->getMock();
  50. $this->loader->setCache($cache);
  51. $cache->expects($this->once())
  52. ->method('isFresh')
  53. ->with($path = $this->featuresPath . DIRECTORY_SEPARATOR . 'pystring.feature', filemtime($path))
  54. ->will($this->returnValue(true));
  55. $cache->expects($this->once())
  56. ->method('read')
  57. ->with($path)
  58. ->will($this->returnValue('cache'));
  59. $cache->expects($this->never())
  60. ->method('write');
  61. $features = $this->loader->load($this->featuresPath . '/pystring.feature');
  62. $this->assertEquals('cache', $features[0]);
  63. }
  64. public function testBasePath()
  65. {
  66. $this->assertFalse($this->loader->supports('features'));
  67. $this->assertFalse($this->loader->supports('tables.feature'));
  68. $this->loader->setBasePath($this->featuresPath . '/../');
  69. $this->assertFalse($this->loader->supports('features'));
  70. $this->assertFalse($this->loader->supports('tables.feature'));
  71. $this->assertTrue($this->loader->supports('features/tables.feature'));
  72. $features = $this->loader->load('features/pystring.feature');
  73. $this->assertEquals(1, count($features));
  74. $this->assertEquals('A py string feature', $features[0]->getTitle());
  75. $this->assertEquals('features' . DIRECTORY_SEPARATOR . 'pystring.feature', $features[0]->getFile());
  76. $this->loader->setBasePath($this->featuresPath);
  77. $features = $this->loader->load('multiline_name.feature');
  78. $this->assertEquals(1, count($features));
  79. $this->assertEquals('multiline', $features[0]->getTitle());
  80. $this->assertEquals('multiline_name.feature', $features[0]->getFile());
  81. }
  82. protected function setUp()
  83. {
  84. $keywords = new CucumberKeywords(__DIR__ . '/../Fixtures/i18n.yml');
  85. $parser = new Parser(new Lexer($keywords));
  86. $this->loader = new GherkinFileLoader($parser);
  87. $this->featuresPath = realpath(__DIR__ . '/../Fixtures/features');
  88. }
  89. }