summaryrefslogtreecommitdiff
path: root/tests/unit/Caching/TFileCacheDependencyTest.php
blob: 6b61aed672341c1ab8d5b8a748249326803fbcd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

require_once(dirname(__FILE__).'/../phpunit2.php');

/**
 * @package System.Caching
 */
class TFileCacheDependencyTest extends PHPUnit2_Framework_TestCase
{
	public function setUp()
	{
	}

	public function tearDown()
	{
	}

	public function testFileName()
	{
		$dependency=new TFileCacheDependency(__FILE__);
		$this->assertEquals($dependency->getFileName(),__FILE__);
		$this->assertEquals($dependency->getTimestamp(),filemtime(__FILE__));

		$dependency=new TFileCacheDependency(dirname(__FILE__).'/foo.txt');
		$this->assertFalse($dependency->getTimestamp());
	}

	public function testHasChanged()
	{
		$tempFile=dirname(__FILE__).'/temp/foo.txt';
		@unlink($tempFile);
		$fw=fopen($tempFile,"w");
		fwrite($fw,"test");
		fclose($fw);
		clearstatcache();

		$dependency=new TFileCacheDependency($tempFile);
		$str=serialize($dependency);

		// test file not changed
		sleep(2);
		$dependency=unserialize($str);
		$this->assertFalse($dependency->getHasChanged());

		// change file
		$fw=fopen($tempFile,"w");
		fwrite($fw,"test again");
		fclose($fw);
		clearstatcache();

		// test file changed
		sleep(2);
		$dependency=unserialize($str);
		$this->assertTrue($dependency->getHasChanged());

		@unlink($tempFile);
	}
}

?>