FileCacheTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Tests\Behat\Gherkin\Cache;
  3. use Behat\Gherkin\Cache\FileCache;
  4. use Behat\Gherkin\Node\FeatureNode;
  5. use Behat\Gherkin\Node\ScenarioNode;
  6. use Behat\Gherkin\Gherkin;
  7. class FileCacheTest extends \PHPUnit_Framework_TestCase
  8. {
  9. private $path;
  10. private $cache;
  11. public function testIsFreshWhenThereIsNoFile()
  12. {
  13. $this->assertFalse($this->cache->isFresh('unexisting', time() + 100));
  14. }
  15. public function testIsFreshOnFreshFile()
  16. {
  17. $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
  18. $this->cache->write('some_path', $feature);
  19. $this->assertFalse($this->cache->isFresh('some_path', time() + 100));
  20. }
  21. public function testIsFreshOnOutdated()
  22. {
  23. $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
  24. $this->cache->write('some_path', $feature);
  25. $this->assertTrue($this->cache->isFresh('some_path', time() - 100));
  26. }
  27. public function testCacheAndRead()
  28. {
  29. $scenarios = array(new ScenarioNode('Some scenario', array(), array(), null, null));
  30. $feature = new FeatureNode('Some feature', 'some description', array(), null, $scenarios, null, null, null, null);
  31. $this->cache->write('some_feature', $feature);
  32. $featureRead = $this->cache->read('some_feature');
  33. $this->assertEquals($feature, $featureRead);
  34. }
  35. public function testBrokenCacheRead()
  36. {
  37. $this->setExpectedException('Behat\Gherkin\Exception\CacheException');
  38. touch($this->path . '/v' . Gherkin::VERSION . '/' . md5('broken_feature') . '.feature.cache');
  39. $this->cache->read('broken_feature');
  40. }
  41. public function testUnwriteableCacheDir()
  42. {
  43. $this->setExpectedException('Behat\Gherkin\Exception\CacheException');
  44. new FileCache('/dev/null/gherkin-test');
  45. }
  46. protected function setUp()
  47. {
  48. $this->cache = new FileCache($this->path = sys_get_temp_dir() . '/gherkin-test');
  49. }
  50. protected function tearDown()
  51. {
  52. foreach (glob($this->path . '/*.feature.cache') as $file) {
  53. unlink((string) $file);
  54. }
  55. }
  56. }