PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/web_app/test/app/test/module/object/cache_driver/file/FileCacheDriverTestTask.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 441 lines | 315 code | 105 blank | 21 comment | 10 complexity | 47308a18fbb3686f75f7efe5114e0549 MD5 | raw file
  1. <?php
  2. /**
  3. * タスク
  4. *
  5. * PHP version 5
  6. *
  7. * @package renderers
  8. * @author stk2k <stk2k@sazysoft.com>
  9. * @copyright 2008 stk2k, sazysoft
  10. */
  11. class FileCacheDriverTestTask extends Charcoal_TestTask
  12. {
  13. private $cache_root;
  14. private $cache_driver;
  15. /**
  16. * check if action will be processed
  17. */
  18. public function isValidAction( $action )
  19. {
  20. switch( $action ){
  21. case "get_empty_data":
  22. case "get_integer_data":
  23. case "get_string_data":
  24. case "get_array_data":
  25. case "get_boolean_data":
  26. case "get_float_data":
  27. case "get_object_data":
  28. case "set_duration":
  29. case "delete":
  30. case "delete_wildcard":
  31. case "delete_regex":
  32. case "touch":
  33. case "touch_wildcard":
  34. case "touch_regex":
  35. return TRUE;
  36. }
  37. return FALSE;
  38. }
  39. /**
  40. * セットアップ
  41. */
  42. public function setUp( $action, $context )
  43. {
  44. $action = us($action);
  45. switch( $action ){
  46. case "get_empty_data":
  47. case "get_integer_data":
  48. case "get_string_data":
  49. case "get_array_data":
  50. case "get_boolean_data":
  51. case "get_float_data":
  52. case "get_object_data":
  53. case "set_duration":
  54. case "delete":
  55. case "delete_wildcard":
  56. case "delete_regex":
  57. case "touch":
  58. case "touch_wildcard":
  59. case "touch_regex":
  60. {
  61. $cache_driver = $context->createObject( 'file', 'cache_driver', 'Charcoal_ICacheDriver' );
  62. $env = $this->getSandbox()->getEnvironment();
  63. $this->cache_root = Charcoal_ResourceLocator::getPath( $env, '%CHARCOAL_HOME%/tmp/test/cache/' );
  64. $config['cache_root'] = $this->cache_root;
  65. $cache_driver->configure( $config );
  66. $this->cache_driver = $cache_driver;
  67. $cache_files = glob( $this->cache_root . '/*' );
  68. if ( $cache_files && is_array($cache_files) ){
  69. foreach( $cache_files as $cache ){
  70. $file = new Charcoal_File( s($cache) );
  71. $name = $file->getName();
  72. $ext = $file->getExtension();
  73. if ( $file->exists() && $file->isFile() && in_array($ext,array("meta","data")) ) {
  74. $file->delete();
  75. echo "removed cache file: [$cache]" . eol();
  76. }
  77. }
  78. }
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. /**
  86. * クリーンアップ
  87. */
  88. public function cleanUp( $action, $context )
  89. {
  90. $action = us($action);
  91. switch( $action ){
  92. case "get_empty_data":
  93. case "get_integer_data":
  94. case "get_string_data":
  95. case "get_array_data":
  96. case "get_boolean_data":
  97. case "get_float_data":
  98. case "get_object_data":
  99. case "set_duration":
  100. case "delete":
  101. case "delete_wildcard":
  102. case "delete_regex":
  103. case "touch":
  104. case "touch_wildcard":
  105. case "touch_regex":
  106. {
  107. $cache_files = glob( $this->cache_root . '/*' );
  108. if ( $cache_files && is_array($cache_files) ){
  109. foreach( $cache_files as $cache ){
  110. $file = new Charcoal_File( s($cache) );
  111. $name = $file->getName();
  112. $ext = $file->getExtension();
  113. if ( $file->exists() && $file->isFile() && in_array($ext,array("meta","data")) ) {
  114. $file->delete();
  115. echo "removed cache file: [$cache]" . eol();
  116. }
  117. }
  118. }
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. /**
  126. * テスト
  127. */
  128. public function test( $action, $context )
  129. {
  130. $action = us($action);
  131. switch( $action ){
  132. case "get_empty_data":
  133. $this->cache_driver->delete( 'foo' );
  134. $value = $this->cache_driver->get( 'foo' );
  135. $this->assertEquals( FALSE, $value );
  136. return TRUE;
  137. case "get_integer_data":
  138. $this->cache_driver->set( 'foo', 100 );
  139. $value = $this->cache_driver->get( 'foo' );
  140. $this->assertEquals( 100, $value );
  141. return TRUE;
  142. case "get_string_data":
  143. $this->cache_driver->set( 'foo', 'bar' );
  144. $value = $this->cache_driver->get( 'foo' );
  145. $this->assertEquals( 'bar', $value );
  146. return TRUE;
  147. case "get_array_data":
  148. $data = array( 'foo' => 100, 'bar' => 'baz' );
  149. $this->cache_driver->set( 'foo', $data );
  150. $value = $this->cache_driver->get( 'foo' );
  151. $this->assertEquals( $data, $value );
  152. return TRUE;
  153. case "get_boolean_data":
  154. $this->cache_driver->set( 'foo', TRUE );
  155. $value = $this->cache_driver->get( 'foo' );
  156. $this->assertEquals( TRUE, $value );
  157. $this->cache_driver->set( 'foo', FALSE );
  158. $value = $this->cache_driver->get( s('foo') );
  159. $this->assertEquals( FALSE, $value );
  160. return TRUE;
  161. case "get_float_data":
  162. $this->cache_driver->set( 'foo', 3.14 );
  163. $value = $this->cache_driver->get( 'foo' );
  164. $this->assertEquals( 3.14, $value );
  165. return TRUE;
  166. case "get_object_data":
  167. $data = new Charcoal_DTO();
  168. $data['foo'] = 100;
  169. $data['bar'] = 'test';
  170. $this->cache_driver->set( 'foo', $data );
  171. $value = $this->cache_driver->get( 'foo' );
  172. echo "value:" . print_r($value,true) . eol();
  173. $this->assertEquals( $data, $value );
  174. return TRUE;
  175. case "set_duration":
  176. $this->cache_driver->set( 'foo', 3.14, 5 );
  177. $cache_files = glob( $this->cache_root . '/*' );
  178. $this->assertEquals( FALSE, is_null($cache_files) );
  179. $this->assertEquals( TRUE, is_array($cache_files) );
  180. $this->assertEquals( 2, count($cache_files) );
  181. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.meta') );
  182. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.data') );
  183. $this->assertEquals( 3.14, $this->cache_driver->get( 'foo' ) );
  184. echo "waiting 3 seconds..." . eol();
  185. sleep(3);
  186. $this->assertEquals( 3.14, $this->cache_driver->get( 'foo' ) );
  187. echo "waiting 3 seconds..." . eol();
  188. sleep(3);
  189. $this->assertEquals( NULL, $this->cache_driver->get( 'foo' ) );
  190. return TRUE;
  191. case "delete":
  192. $this->cache_driver->set( 'foo', 3.14 );
  193. $this->cache_driver->set( 'bar', 2 );
  194. $this->cache_driver->set( 'baz', 'hello!' );
  195. $cache_files = glob( $this->cache_root . '/*' );
  196. $this->assertEquals( FALSE, is_null($cache_files) );
  197. $this->assertEquals( TRUE, is_array($cache_files) );
  198. $this->assertEquals( 6, count($cache_files) );
  199. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.meta') );
  200. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.data') );
  201. $this->assertEquals( TRUE, file_exists($this->cache_root . '/bar.meta') );
  202. $this->assertEquals( TRUE, file_exists($this->cache_root . '/bar.data') );
  203. $this->assertEquals( TRUE, file_exists($this->cache_root . '/baz.meta') );
  204. $this->assertEquals( TRUE, file_exists($this->cache_root . '/baz.data') );
  205. $this->cache_driver->delete( 'bar' );
  206. $cache_files = glob( $this->cache_root . '/*' );
  207. $this->assertEquals( FALSE, is_null($cache_files) );
  208. $this->assertEquals( TRUE, is_array($cache_files) );
  209. $this->assertEquals( 4, count($cache_files) );
  210. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.meta') );
  211. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.data') );
  212. $this->assertEquals( FALSE, file_exists($this->cache_root . '/bar.meta') );
  213. $this->assertEquals( FALSE, file_exists($this->cache_root . '/bar.data') );
  214. $this->assertEquals( TRUE, file_exists($this->cache_root . '/baz.meta') );
  215. $this->assertEquals( TRUE, file_exists($this->cache_root . '/baz.data') );
  216. $this->cache_driver->delete( 'baz' );
  217. $cache_files = glob( $this->cache_root . '/*' );
  218. $this->assertEquals( FALSE, is_null($cache_files) );
  219. $this->assertEquals( TRUE, is_array($cache_files) );
  220. $this->assertEquals( 2, count($cache_files) );
  221. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.meta') );
  222. $this->assertEquals( TRUE, file_exists($this->cache_root . '/foo.data') );
  223. $this->assertEquals( FALSE, file_exists($this->cache_root . '/bar.meta') );
  224. $this->assertEquals( FALSE, file_exists($this->cache_root . '/bar.data') );
  225. $this->assertEquals( FALSE, file_exists($this->cache_root . '/baz.meta') );
  226. $this->assertEquals( FALSE, file_exists($this->cache_root . '/baz.data') );
  227. return TRUE;
  228. case "delete_wildcard":
  229. $this->cache_driver->set( 'test.foo', 3.14 );
  230. $this->cache_driver->set( 'test.bar', 2 );
  231. $this->cache_driver->set( 'test.baz', 'hello!' );
  232. $this->cache_driver->set( 'no_delete', ':)' );
  233. $cache_files = glob( $this->cache_root . '/*' );
  234. $this->assertEquals( FALSE, is_null($cache_files) );
  235. $this->assertEquals( TRUE, is_array($cache_files) );
  236. $this->assertEquals( 8, count($cache_files) );
  237. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.meta') );
  238. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.data') );
  239. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.bar.meta') );
  240. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.bar.data') );
  241. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.baz.meta') );
  242. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.baz.data') );
  243. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.meta') );
  244. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.data') );
  245. $this->cache_driver->delete( 'test.b*' );
  246. $cache_files = glob( $this->cache_root . '/*' );
  247. $this->assertEquals( FALSE, is_null($cache_files) );
  248. $this->assertEquals( TRUE, is_array($cache_files) );
  249. $this->assertEquals( 4, count($cache_files) );
  250. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.meta') );
  251. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.data') );
  252. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.meta') );
  253. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.data') );
  254. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.meta') );
  255. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.data') );
  256. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.meta') );
  257. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.data') );
  258. $this->cache_driver->set( 'test.foo', 3.14 );
  259. $this->cache_driver->set( 'test.bar', 2 );
  260. $this->cache_driver->set( 'test.baz', 'hello!' );
  261. $this->cache_driver->set( 'no_delete', ':)' );
  262. $this->cache_driver->delete( 'test.*' );
  263. $cache_files = glob( $this->cache_root . '/*' );
  264. $this->assertEquals( FALSE, is_null($cache_files) );
  265. $this->assertEquals( TRUE, is_array($cache_files) );
  266. $this->assertEquals( 2, count($cache_files) );
  267. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.foo.meta') );
  268. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.foo.data') );
  269. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.meta') );
  270. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.data') );
  271. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.meta') );
  272. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.data') );
  273. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.meta') );
  274. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.data') );
  275. return TRUE;
  276. case "delete_regex":
  277. $this->cache_driver->set( 'test.foo', 3.14 );
  278. $this->cache_driver->set( 'test.bar', 2 );
  279. $this->cache_driver->set( 'test.baz', 'hello!' );
  280. $this->cache_driver->set( 'no_delete', ':)' );
  281. $cache_files = glob( $this->cache_root . '/*' );
  282. $this->assertEquals( FALSE, is_null($cache_files) );
  283. $this->assertEquals( TRUE, is_array($cache_files) );
  284. $this->assertEquals( 8, count($cache_files) );
  285. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.meta') );
  286. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.data') );
  287. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.bar.meta') );
  288. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.bar.data') );
  289. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.baz.meta') );
  290. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.baz.data') );
  291. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.meta') );
  292. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.data') );
  293. $this->cache_driver->deleteRegEx( '/test\\.ba./', TRUE );
  294. $cache_files = glob( $this->cache_root . '/*' );
  295. $this->assertEquals( FALSE, is_null($cache_files) );
  296. $this->assertEquals( TRUE, is_array($cache_files) );
  297. $this->assertEquals( 4, count($cache_files) );
  298. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.meta') );
  299. $this->assertEquals( TRUE, file_exists($this->cache_root . '/test.foo.data') );
  300. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.meta') );
  301. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.data') );
  302. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.meta') );
  303. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.data') );
  304. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.meta') );
  305. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.data') );
  306. $this->cache_driver->set( 'test.foo', 3.14 );
  307. $this->cache_driver->set( 'test.bar', 2 );
  308. $this->cache_driver->set( 'test.baz', 'hello!' );
  309. $this->cache_driver->set( 'no_delete', ':)' );
  310. $this->cache_driver->deleteRegEx( '/test\\.[foo|bar|baz]/', TRUE );
  311. $cache_files = glob( $this->cache_root . '/*' );
  312. echo "files found:" . print_r($cache_files,true) . eol();
  313. $this->assertEquals( FALSE, is_null($cache_files) );
  314. $this->assertEquals( TRUE, is_array($cache_files) );
  315. $this->assertEquals( 2, count($cache_files) );
  316. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.foo.meta') );
  317. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.foo.data') );
  318. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.meta') );
  319. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.bar.data') );
  320. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.meta') );
  321. $this->assertEquals( FALSE, file_exists($this->cache_root . '/test.baz.data') );
  322. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.meta') );
  323. $this->assertEquals( TRUE, file_exists($this->cache_root . '/no_delete.data') );
  324. return TRUE;
  325. return TRUE;
  326. case "touch":
  327. return TRUE;
  328. case "touch_wildcard":
  329. return TRUE;
  330. case "touch_regex":
  331. return TRUE;
  332. }
  333. return FALSE;
  334. }
  335. }
  336. return __FILE__;