3 namespace Tests\Behat\Gherkin\Cache;
5 use Behat\Gherkin\Cache\FileCache;
6 use Behat\Gherkin\Node\FeatureNode;
7 use Behat\Gherkin\Node\ScenarioNode;
8 use Behat\Gherkin\Gherkin;
10 class FileCacheTest extends \PHPUnit_Framework_TestCase
15 public function testIsFreshWhenThereIsNoFile()
17 $this->assertFalse($this->cache->isFresh('unexisting', time() + 100));
20 public function testIsFreshOnFreshFile()
22 $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
24 $this->cache->write('some_path', $feature);
26 $this->assertFalse($this->cache->isFresh('some_path', time() + 100));
29 public function testIsFreshOnOutdated()
31 $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, null);
33 $this->cache->write('some_path', $feature);
35 $this->assertTrue($this->cache->isFresh('some_path', time() - 100));
38 public function testCacheAndRead()
40 $scenarios = array(new ScenarioNode('Some scenario', array(), array(), null, null));
41 $feature = new FeatureNode('Some feature', 'some description', array(), null, $scenarios, null, null, null, null);
43 $this->cache->write('some_feature', $feature);
44 $featureRead = $this->cache->read('some_feature');
46 $this->assertEquals($feature, $featureRead);
49 public function testBrokenCacheRead()
51 $this->setExpectedException('Behat\Gherkin\Exception\CacheException');
53 touch($this->path . '/v' . Gherkin::VERSION . '/' . md5('broken_feature') . '.feature.cache');
54 $this->cache->read('broken_feature');
57 public function testUnwriteableCacheDir()
59 $this->setExpectedException('Behat\Gherkin\Exception\CacheException');
61 new FileCache('/dev/null/gherkin-test');
64 protected function setUp()
66 $this->cache = new FileCache($this->path = sys_get_temp_dir() . '/gherkin-test');
69 protected function tearDown()
71 foreach (glob($this->path . '/*.feature.cache') as $file) {
72 unlink((string) $file);