summaryrefslogtreecommitdiff
path: root/tests/unit/Caching/TDirectoryCacheDependencyTest.php
blob: 10da9b581eedcd702f077ea8147fac6f944d5fe6 (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
<?php

/**
 * @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);
	}
}