1 | <?php |
||
2 | /** |
||
3 | * EGroupware Api: Caching tests |
||
4 | * |
||
5 | * @link http://www.stylite.de |
||
6 | * @package api |
||
7 | * @subpackage cache |
||
8 | * @author Ralf Becker <rb-AT-stylite.de> |
||
9 | * @copyright (c) 2016 by Ralf Becker <rb-AT-stylite.de> |
||
10 | * @author Stylite AG <[email protected]> |
||
11 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
||
12 | * @version $Id$ |
||
13 | */ |
||
14 | |||
15 | namespace EGroupware\Api; |
||
16 | |||
17 | use EGroupware\Api; |
||
18 | use PHPUnit\Framework\TestCase as TestCase; |
||
19 | use ReflectionClass; |
||
20 | |||
21 | /** |
||
22 | * Mail account credentials tests |
||
23 | * |
||
24 | * Only testing en&decryption of mail passwords so far. |
||
25 | * Further tests would need database. |
||
26 | */ |
||
27 | class CacheTest extends TestCase |
||
28 | { |
||
29 | /** |
||
30 | * Test a caching provider |
||
31 | * |
||
32 | * @param string $class |
||
33 | * @param string $params |
||
34 | * @dataProvider cachingProvider |
||
35 | */ |
||
36 | public function testCache($class, $params=array()) |
||
37 | { |
||
38 | // set us up as provider for Api\Cache class |
||
39 | $GLOBALS['egw_info']['server']['install_id'] = md5(microtime(true).__FILE__); |
||
40 | unset($GLOBALS['egw_info']['server']['cache_provider_instance']); |
||
41 | unset($GLOBALS['egw_info']['server']['cache_provider_tree']); |
||
42 | Api\Cache::$default_provider = $class; |
||
0 ignored issues
–
show
|
|||
43 | |||
44 | try { |
||
45 | $provider = new $class($params); |
||
46 | $refclass = new ReflectionClass($class); |
||
47 | $methods = array(); |
||
48 | foreach(array('get','set','add','mget','delete') as $name) |
||
49 | { |
||
50 | if ($name != 'mget' || is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
51 | { |
||
52 | $methods[$name] = $refclass->getMethod($name); |
||
53 | $methods[$name]->setAccessible(true); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | foreach(array( |
||
58 | Api\Cache::TREE => 'tree', |
||
59 | Api\Cache::INSTANCE => 'instance', |
||
60 | ) as $level => $label) |
||
61 | { |
||
62 | $locations = array(); |
||
63 | foreach(array('string',123,true,false,null,array(),array(1,2,3)) as $data) |
||
64 | { |
||
65 | $location = md5(microtime(true).$label.serialize($data)); |
||
66 | $this->assertNull($methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
67 | "$class: $label: get_before_set"); |
||
68 | |||
69 | $this->assertTrue($methods['set']->invokeArgs($provider, array(array($level,__CLASS__,$location), $data, 10)), |
||
70 | "$class: $label: set"); |
||
71 | |||
72 | $this->assertEquals($data, $methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
73 | "$class: $label: get_after_set"); |
||
74 | |||
75 | if (is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
76 | { |
||
77 | $this->assertEquals(array($location => $data), |
||
78 | $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,array($location)))), |
||
79 | "$class: $label: mget_after_set"); |
||
80 | } |
||
81 | $this->assertNotTrue($methods['add']->invokeArgs($provider, array(array($level,__CLASS__,$location), 'other-data')), |
||
82 | "$class: $label: add_after_set"); |
||
83 | |||
84 | $this->assertTrue($methods['delete']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
85 | "$class: $label: delete"); |
||
86 | |||
87 | $this->assertNull($methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
88 | "$class: $label: get_after_delete"); |
||
89 | |||
90 | // prepare for mget of everything |
||
91 | if (is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
92 | { |
||
93 | $locations[$location] = $data; |
||
94 | $mget_after_delete = $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,array($location)))); |
||
95 | $this->assertNotTrue(isset($mget_after_delete[$location]), |
||
96 | "$class: $label: mget_after_delete['$location']"); |
||
97 | } |
||
98 | elseif (!is_null($data)) // emulation can NOT distinquish between null and not set |
||
99 | { |
||
100 | $locations[$location] = $data; |
||
101 | } |
||
102 | $this->assertTrue($methods['add']->invokeArgs($provider, array(array($level,__CLASS__,$location), $data, 10)), |
||
103 | "$class: $label: add_after_delete"); |
||
104 | |||
105 | $this->assertEquals($data, $methods['get']->invokeArgs($provider, array(array($level,__CLASS__,$location))), |
||
106 | "$class: $label: get_after_add"); |
||
107 | } |
||
108 | // get all above in one request |
||
109 | $keys = array_keys($locations); |
||
110 | $keys_bogus = array_merge(array('not-set'),array_keys($locations),array('not-set-too')); |
||
111 | if (is_a($provider, 'EGroupware\Api\Cache\ProviderMultiple')) |
||
112 | { |
||
113 | $this->assertEquals($locations, $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,$keys))), |
||
114 | "$class: $label: mget_all"); |
||
115 | $this->assertEquals($locations, $methods['mget']->invokeArgs($provider, array(array($level,__CLASS__,$keys_bogus))), |
||
116 | "$class: $label: mget_with_bogus_key"); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | catch (\Exception $e) { |
||
121 | $this->markTestSkipped($e->getMessage()); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Caching provides to set with constructor parameters |
||
127 | * |
||
128 | * @return array of array |
||
129 | */ |
||
130 | public static function cachingProvider() |
||
131 | { |
||
132 | // create empty temp. directory |
||
133 | unlink($tmp_dir = tempnam('/tmp', 'tmp')); |
||
134 | mkdir($tmp_dir); |
||
135 | |||
136 | return array( |
||
137 | array(__NAMESPACE__.'\\Cache\\Apcu'), |
||
138 | array(__NAMESPACE__.'\\Cache\\Apc'), |
||
139 | array(__NAMESPACE__.'\\Cache\\Memcache', array('localhost')), |
||
140 | array(__NAMESPACE__.'\\Cache\\Memcached', array('localhost')), |
||
141 | array(__NAMESPACE__.'\\Cache\\Files', array($tmp_dir)), |
||
142 | ); |
||
143 | } |
||
144 | } |
||
145 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..