summaryrefslogtreecommitdiff
path: root/tests/unit/Caching/TDirectoryCacheDependencyTest.php
blob: 7a18004f2a6f80cc0c1c7f0b41f1813c20c8d0d1 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
use Prado\Caching\TDirectoryCacheDependency;
use Prado\Exceptions\TInvalidDataValueException;

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

	public function tearDown()
	{
	}

	public function testDirectoryName()
	{
		$directory=realpath(dirname(__FILE__).'/temp');
		$dependency=new TDirectoryCacheDependency(dirname(__FILE__).'/temp');
		$this->assertEquals($dependency->getDirectory(),$directory);

		try
		{
			$dependency=new TDirectoryCacheDependency(dirname(__FILE__).'/temp2');
			$this->fail("Expected exception is not raised");
		}
		catch(TInvalidDataValueException $e)
		{
		}
	}

	public function testRecursiveCheck()
	{
		$directory=realpath(dirname(__FILE__).'/temp');
		$dependency=new TDirectoryCacheDependency(dirname(__FILE__).'/temp');
		$this->assertTrue($dependency->getRecursiveCheck());
		$dependency->setRecursiveCheck(false);
		$this->assertFalse($dependency->getRecursiveCheck());
	}

	public function testRecursiveLevel()
	{
		$directory=realpath(dirname(__FILE__).'/temp');
		$dependency=new TDirectoryCacheDependency(dirname(__FILE__).'/temp');
		$this->assertEquals($dependency->getRecursiveLevel(),-1);
		$dependency->setRecursiveLevel(5);
		$this->assertEquals($dependency->getRecursiveLevel(),5);
	}

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

		$dependency=new TDirectoryCacheDependency(dirname($tempFile));
		$str=serialize($dependency);

		// test directory 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);
	}
}