summaryrefslogtreecommitdiff
path: root/tests/unit/Caching/TFileCacheDependencyTest.php
blob: fa6e8965acbeee3e59ba1d8e609a35723d5eafea (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
<?php
use Prado\Caching\TFileCacheDependency;

/**
 * @package System.Caching
 */
class TFileCacheDependencyTest extends PHPUnit_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);
	}
}