TestCache::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Test - Cache class
4
 * @package     fwolflib
5
 * @subpackage	class-test
6
 * @copyright   Copyright 2010, Fwolf
7
 * @author      Fwolf <[email protected]>
8
 * @since		2010-01-07
9
 */
10
11
// Define like this, so test can run both under eclipse and web alone.
12
// {{{
13
if (! defined('SIMPLE_TEST')) {
14
	define('SIMPLE_TEST', 'simpletest/');
15
	require_once(SIMPLE_TEST . 'autorun.php');
16
}
17
// Then set output encoding
18
//header('Content-Type: text/html; charset=utf-8');
19
// }}}
20
21
// Require library define file which need test
22
require_once(dirname(__FILE__) . '/fwolflib.php');
23
require_once(FWOLFLIB . 'class/cache.php');
24
require_once(FWOLFLIB . 'func/ecl.php');
25
require_once(FWOLFLIB . 'func/request.php');
26
require_once(FWOLFLIB . 'func/string.php');
27
28
class TestCache extends UnitTestCase {
29
30
	/**
31
	 * Cache object
32
	 * @var	object
33
	 */
34
	protected $oCh = null;
35
36
37
	/**
38
	 * Constructor
39
	 */
40
	public function __construct () {
41
		$ar_cfg = array(
42
			'cache-file-dir'	=> '/tmp/cache/',
43
			'cache-file-rule'	=> '1142',
44
		);
45
		$this->oCh = new CacheTest($ar_cfg);
46
	} // end of func __construct
47
48
49
    function TestCacheFilePath () {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
		$this->oCh->SetCfg(array(
51
			'cache-type'		=> 'file',
52
			'cache-file-dir'	=> '/tmp/cache/',
53
			'cache-file-rule'	=> '1140',
54
		));
55
		$key = 'site/index';
56
57
		//Ecl(md5($key));
58
59
		$x = '/tmp/cache/d0/ex/3ed0dc6e';
60
		$y = $this->oCh->CacheFilePath($key);
61
		$this->assertEqual($x, $y);
62
63
		$this->oCh->SetCfg(array('cache-file-rule' => '1131'));
64
		$x = '/tmp/cache/d0/te/3ed0dc6e';
65
		$y = $this->oCh->CacheFilePath($key);
66
		$this->assertEqual($x, $y);
67
68
		// Notice: Directly use key's part as path may cause wrong
69
		$this->oCh->SetCfg(array('cache-file-rule' => '2342'));
70
		$x = '/tmp/cache/57//i/3ed0dc6e';
71
		$y = $this->oCh->CacheFilePath($key);
72
		$this->assertEqual($x, $y);
73
74
		// Common usage
75
		$this->oCh->SetCfg(array('cache-file-rule' => '1011'));
76
		$x = '/tmp/cache/3e/d0/3ed0dc6e';
77
		$y = $this->oCh->CacheFilePath($key);
78
		$this->assertEqual($x, $y);
79
80
		// Common usage 2
81
		$this->oCh->SetCfg(array('cache-file-rule' => '2021'));
82
		$x = '/tmp/cache/b6/9c/3ed0dc6e';
83
		$y = $this->oCh->CacheFilePath($key);
84
		$this->assertEqual($x, $y);
85
86
		// Common usage 3
87
		$this->oCh->SetCfg(array('cache-file-rule' => '55'));
88
		$x = '/tmp/cache/89/3ed0dc6e';
89
		$y = $this->oCh->CacheFilePath($key);
90
		$this->assertEqual($x, $y);
91
92
		//Ecl($y);
93
94
		// Read/write
95
		$v = $this->oCh->CacheGet($key, 1);
96
		var_dump($v);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($v); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
97
		Ecl(hash('crc32', $key)
0 ignored issues
show
Deprecated Code introduced by
The function Ecl() has been deprecated with message: Use Fwlib\Util\Env::ecl()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
98
			. '|'
99
			. $this->oCh->CacheFilePath($key));
100
    } // end of func TestCacheFilePath
101
102
103
} // end of class TestCache
104
105
106
class CacheTest extends Cache {
0 ignored issues
show
Deprecated Code introduced by
The class Cache has been deprecated with message: use Fwlib\Cache\Cache

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
107
	protected function CacheGenVal ($key) {
108
		$this->sDummy = RandomString(30, 'a0');
0 ignored issues
show
Bug introduced by
The property sDummy does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Deprecated Code introduced by
The function RandomString() has been deprecated with message: Use Fwlib\Util\StringUtil::random()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
109
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (CacheTest) is incompatible with the return type declared by the abstract method Cache::CacheGenVal of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
110
	} // end of func CacheGenVal
111
112
	public function CacheLifetime ($key) {
113
		return 0;
114
	} // end of func CacheLifetime
115
} // end of class CacheTest
116
117
118
// Change output charset in this way.
119
// {{{
120
$s_url = GetSelfUrl(false);
0 ignored issues
show
Deprecated Code introduced by
The function GetSelfUrl() has been deprecated with message: Use Fwlib\Util\HttpUtil::getSelfUrl()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
121
$s_url = substr($s_url, strrpos($s_url, '/') + 1);
122
if ('cache.test.php' == $s_url) {
123
	$test = new TestCache();
124
	$test->run(new HtmlReporter('utf-8'));
125
}
126
// }}}
127
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
128