PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/test/functional/cacheTest.php

https://bitbucket.org/carlod/appt
PHP | 502 lines | 362 code | 93 blank | 47 comment | 4 complexity | 8d83ea861a866ee6c7ce640e24be8274 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. $app = 'cache';
  10. if (!include(dirname(__FILE__).'/../bootstrap/functional.php'))
  11. {
  12. return;
  13. }
  14. class myTestBrowser extends sfTestBrowser
  15. {
  16. function checkResponseContent($content, $message)
  17. {
  18. $this->test()->ok($this->getResponse()->getContent() == $content, $message);
  19. return $this;
  20. }
  21. function getMultiAction($parameter = null)
  22. {
  23. return $this->
  24. get('/cache/multi'.(null !== $parameter ? '/param/'.$parameter : ''))->
  25. with('request')->begin()->
  26. isParameter('module', 'cache')->
  27. isParameter('action', 'multi')->
  28. end()->
  29. with('response')->begin()->
  30. isStatusCode(200)->
  31. // partials
  32. checkElement('#partial .partial')->
  33. // contextual partials
  34. checkElement('#contextualPartial .contextualPartial')->
  35. checkElement('#contextualCacheablePartial .contextualCacheablePartial__'.$parameter)->
  36. checkElement('#contextualCacheablePartialVarParam .contextualCacheablePartial_varParam_'.$parameter)->
  37. // components
  38. checkElement('#component .component__componentParam_'.$parameter)->
  39. checkElement('#componentVarParam .component_varParam_componentParam_'.$parameter)->
  40. // contextual components
  41. checkElement('#contextualComponent .contextualComponent__componentParam_'.$parameter)->
  42. checkElement('#contextualComponentVarParam .contextualComponent_varParam_componentParam_'.$parameter)->
  43. checkElement('#contextualCacheableComponent .contextualCacheableComponent__componentParam_'.$parameter)->
  44. checkElement('#contextualCacheableComponentVarParam .contextualCacheableComponent_varParam_componentParam_'.$parameter)->
  45. end()->
  46. with('view_cache')->begin()->
  47. isCached(false)->
  48. // partial cache
  49. isUriCached('@sf_cache_partial?module=cache&action=_partial&sf_cache_key='.md5(serialize(array())), false)->
  50. isUriCached('@sf_cache_partial?module=cache&action=_partial&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), false)->
  51. isUriCached('@sf_cache_partial?module=cache&action=_cacheablePartial&sf_cache_key='.md5(serialize(array())), true)->
  52. isUriCached('@sf_cache_partial?module=cache&action=_cacheablePartial&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), true)->
  53. isUriCached('@sf_cache_partial?module=cache&action=_cacheablePartial&sf_cache_key='.md5(serialize(array('varParam' => 'another'))), false)->
  54. // contextual partial cache
  55. isUriCached('@sf_cache_partial?module=cache&action=_contextualPartial&sf_cache_key='.md5(serialize(array())), false)->
  56. isUriCached('@sf_cache_partial?module=cache&action=_contextualPartial&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), false)->
  57. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheablePartial&sf_cache_key='.md5(serialize(array())), true)->
  58. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheablePartial&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), true)->
  59. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheablePartial&sf_cache_key='.md5(serialize(array('varParam' => 'another'))), false)->
  60. // component cache
  61. isUriCached('@sf_cache_partial?module=cache&action=_component&sf_cache_key='.md5(serialize(array())), false)->
  62. isUriCached('@sf_cache_partial?module=cache&action=_component&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), false)->
  63. isUriCached('@sf_cache_partial?module=cache&action=_cacheableComponent&sf_cache_key='.md5(serialize(array())), true)->
  64. isUriCached('@sf_cache_partial?module=cache&action=_cacheableComponent&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), true)->
  65. isUriCached('@sf_cache_partial?module=cache&action=_cacheableComponent&sf_cache_key='.md5(serialize(array('varParam' => 'another'))), false)->
  66. // contextual component cache
  67. isUriCached('@sf_cache_partial?module=cache&action=_contextualComponent&sf_cache_key='.md5(serialize(array())), false)->
  68. isUriCached('@sf_cache_partial?module=cache&action=_contextualComponent&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), false)->
  69. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key='.md5(serialize(array())), true)->
  70. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key='.md5(serialize(array('varParam' => 'varParam'))), true)->
  71. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key='.md5(serialize(array('varParam' => 'another'))), false)->
  72. end()
  73. ;
  74. }
  75. public function launch()
  76. {
  77. $b = $this;
  78. // default page is in cache (without layout)
  79. $b->
  80. get('/')->
  81. with('request')->begin()->
  82. isParameter('module', 'default')->
  83. isParameter('action', 'index')->
  84. end()->
  85. with('response')->begin()->
  86. isStatusCode(200)->
  87. checkElement('body', '/congratulations/i')->
  88. end()->
  89. with('view_cache')->isCached(true)
  90. ;
  91. $b->
  92. get('/nocache')->
  93. with('request')->begin()->
  94. isParameter('module', 'nocache')->
  95. isParameter('action', 'index')->
  96. end()->
  97. with('response')->begin()->
  98. isStatusCode(200)->
  99. checkElement('body', '/nocache/i')->
  100. end()->
  101. with('view_cache')->isCached(false)
  102. ;
  103. $b->
  104. get('/cache/page')->
  105. with('request')->begin()->
  106. isParameter('module', 'cache')->
  107. isParameter('action', 'page')->
  108. end()->
  109. with('response')->begin()->
  110. isStatusCode(200)->
  111. checkElement('body', '/page in cache/')->
  112. end()->
  113. with('view_cache')->isCached(true, true)
  114. ;
  115. $b->
  116. get('/cache/forward')->
  117. with('request')->begin()->
  118. isParameter('module', 'cache')->
  119. isParameter('action', 'forward')->
  120. end()->
  121. with('response')->begin()->
  122. isStatusCode(200)->
  123. checkElement('body', '/page in cache/')->
  124. end()->
  125. with('view_cache')->isCached(true)
  126. ;
  127. // remove all cache
  128. sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
  129. $b->
  130. getMultiAction()->
  131. getMultiAction('requestParam')->
  132. // component already in cache and not contextual, so request parameter is not there
  133. with('response')->begin()->
  134. checkElement('#cacheableComponent .cacheableComponent__componentParam_')->
  135. checkElement('#cacheableComponentVarParam .cacheableComponent_varParam_componentParam_')->
  136. checkElement('#cacheablePartial .cacheablePartial__')->
  137. checkElement('#cacheablePartialVarParam .cacheablePartial_varParam_')->
  138. end()
  139. ;
  140. // remove all cache
  141. sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
  142. $b->
  143. getMultiAction('requestParam')->
  144. with('response')->begin()->
  145. checkElement('#cacheableComponent .cacheableComponent__componentParam_requestParam')->
  146. checkElement('#cacheableComponentVarParam .cacheableComponent_varParam_componentParam_requestParam')->
  147. checkElement('#cacheablePartial .cacheablePartial__requestParam')->
  148. checkElement('#cacheablePartialVarParam .cacheablePartial_varParam_requestParam')->
  149. end()->
  150. getMultiAction()->
  151. with('response')->begin()->
  152. checkElement('#cacheableComponent .cacheableComponent__componentParam_requestParam')->
  153. checkElement('#cacheableComponentVarParam .cacheableComponent_varParam_componentParam_requestParam')->
  154. checkElement('#cacheablePartial .cacheablePartial__requestParam')->
  155. checkElement('#cacheablePartialVarParam .cacheablePartial_varParam_requestParam')->
  156. end()->
  157. getMultiAction('anotherRequestParam')->
  158. with('response')->begin()->
  159. checkElement('#cacheableComponent .cacheableComponent__componentParam_requestParam')->
  160. checkElement('#cacheableComponentVarParam .cacheableComponent_varParam_componentParam_requestParam')->
  161. checkElement('#cacheablePartial .cacheablePartial__requestParam')->
  162. checkElement('#cacheablePartialVarParam .cacheablePartial_varParam_requestParam')->
  163. end()
  164. ;
  165. // check contextual cache with another action
  166. $b->
  167. get('/cache/multiBis')->
  168. with('request')->begin()->
  169. isParameter('module', 'cache')->
  170. isParameter('action', 'multiBis')->
  171. end()->
  172. with('response')->begin()->
  173. isStatusCode(200)->
  174. // partials
  175. checkElement('#cacheablePartial .cacheablePartial__requestParam')->
  176. // contextual partials
  177. checkElement('#contextualCacheablePartial .contextualCacheablePartial__')->
  178. // components
  179. checkElement('#cacheableComponent .cacheableComponent__componentParam_requestParam')->
  180. // contextual components
  181. checkElement('#contextualCacheableComponent .contextualCacheableComponent__componentParam_')->
  182. end()->
  183. with('view_cache')->begin()->
  184. isCached(false)->
  185. // partial cache
  186. isUriCached('@sf_cache_partial?module=cache&action=_cacheablePartial&sf_cache_key='.md5(serialize(array())), true)->
  187. // contextual partial cache
  188. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key='.md5(serialize(array())), true)->
  189. // component cache
  190. isUriCached('@sf_cache_partial?module=cache&action=_cacheableComponent&sf_cache_key='.md5(serialize(array())), true)->
  191. // contextual component cache
  192. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key='.md5(serialize(array())), true)->
  193. end()
  194. ;
  195. // remove all cache
  196. sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
  197. // check user supplied cache key for partials and components
  198. $b->
  199. get('/cache/specificCacheKey')->
  200. with('request')->begin()->
  201. isParameter('module', 'cache')->
  202. isParameter('action', 'specificCacheKey')->
  203. end()->
  204. with('response')->isStatusCode(200)->
  205. with('view_cache')->begin(200)->
  206. isCached(false)->
  207. // partial cache
  208. isUriCached('@sf_cache_partial?module=cache&action=_cacheablePartial&sf_cache_key=cacheablePartial', true)->
  209. // contextual partial cache
  210. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key=contextualCacheableComponent', true)->
  211. // component cache
  212. isUriCached('@sf_cache_partial?module=cache&action=_cacheableComponent&sf_cache_key=cacheableComponent', true)->
  213. // contextual component cache
  214. isUriCached('@sf_cache_partial?module=cache&action=_contextualCacheableComponent&sf_cache_key=contextualCacheableComponent', true)->
  215. end()
  216. ;
  217. // check cache content for actions
  218. // remove all cache
  219. sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
  220. $b->
  221. get('/cache/action')->
  222. with('request')->begin()->
  223. isParameter('module', 'cache')->
  224. isParameter('action', 'action')->
  225. end()->
  226. with('response')->isStatusCode(200)->
  227. with('view_cache')->isCached(true)
  228. ;
  229. $b->test()->is(sfConfig::get('ACTION_EXECUTED', false), true, 'action is executed when not in cache');
  230. sfConfig::set('ACTION_EXECUTED', false);
  231. $response = $b->getResponse();
  232. $content1 = $response->getContent();
  233. $contentType1 = $response->getContentType();
  234. $headers1 = $response->getHttpHeaders();
  235. $b->
  236. get('/cache/action')->
  237. with('request')->begin()->
  238. isParameter('module', 'cache')->
  239. isParameter('action', 'action')->
  240. end()->
  241. with('response')->isStatusCode(200)->
  242. with('view_cache')->isCached(true)
  243. ;
  244. $b->test()->is(sfConfig::get('ACTION_EXECUTED', false), false, 'action is not executed when in cache');
  245. $response = $b->getResponse();
  246. $content2 = $response->getContent();
  247. $contentType2 = $response->getContentType();
  248. $headers2 = $response->getHttpHeaders();
  249. $b->test()->is($content1, $content2, 'response content is the same');
  250. $b->test()->is($contentType1, $contentType2, 'response content type is the same');
  251. $b->test()->is($headers1, $headers2, 'response http headers are the same');
  252. }
  253. }
  254. $b = new myTestBrowser();
  255. // non HTML cache
  256. $image = file_get_contents(dirname(__FILE__).'/fixtures/apps/cache/modules/cache/data/ok48.png');
  257. sfConfig::set('sf_web_debug', true);
  258. $b->
  259. get('/cache/imageWithLayoutCacheWithLayout')->
  260. with('view_cache')->isCached(true, true)->
  261. checkResponseContent($image, 'image (with layout/page cache) in cache is not decorated when web_debug is on')->
  262. get('/cache/imageWithLayoutCacheWithLayout')->
  263. with('view_cache')->isCached(true, true)->
  264. checkResponseContent($image, 'image (with layout/page cache) in cache is not decorated when web_debug is on')->
  265. get('/cache/imageWithLayoutCacheNoLayout')->
  266. with('view_cache')->isCached(true)->
  267. checkResponseContent($image, 'image (with layout/action cache) in cache is not decorated when web_debug is on')->
  268. get('/cache/imageWithLayoutCacheNoLayout')->
  269. with('view_cache')->isCached(true)->
  270. checkResponseContent($image, 'image (with layout/action cache) in cache is not decorated when web_debug is on')->
  271. get('/cache/imageNoLayoutCacheWithLayout')->
  272. with('view_cache')->isCached(true, true)->
  273. checkResponseContent($image, 'image (no layout/page cache) in cache is not decorated when web_debug is on')->
  274. get('/cache/imageNoLayoutCacheWithLayout')->
  275. with('view_cache')->isCached(true, true)->
  276. checkResponseContent($image, 'image (no layout/page cache) in cache is not decorated when web_debug is on')->
  277. get('/cache/imageNoLayoutCacheNoLayout')->
  278. with('view_cache')->isCached(true)->
  279. checkResponseContent($image, 'image (no layout/action cache) in cache is not decorated when web_debug is on')->
  280. get('/cache/imageNoLayoutCacheNoLayout')->
  281. with('view_cache')->isCached(true)->
  282. checkResponseContent($image, 'image (no layout/action cache) in cache is not decorated when web_debug is on')
  283. ;
  284. sfConfig::set('sf_web_debug', false);
  285. // check stylesheets, javascripts inclusions
  286. sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
  287. $b->
  288. get('/cache/multiBis')->
  289. with('request')->begin()->
  290. isParameter('module', 'cache')->
  291. isParameter('action', 'multiBis')->
  292. end()->
  293. with('response')->begin()->
  294. isStatusCode(200)->
  295. // the first time (no cache)
  296. checkElement('link[href*="/main_css"]')->
  297. checkElement('script[src*="/main_js"]')->
  298. checkElement('link[href*="/partial_css"]')->
  299. checkElement('script[src*="/partial_js"]')->
  300. checkElement('link[href*="/another_partial_css"]')->
  301. checkElement('script[src*="/another_partial_js"]')->
  302. checkElement('link[href*="/component_css"]')->
  303. checkElement('script[src*="/component_js"]')->
  304. checkElement('#partial_slot_content', 'Partial')->
  305. checkElement('#another_partial_slot_content', 'Another Partial')->
  306. checkElement('#component_slot_content', 'Component')->
  307. end()->
  308. get('/cache/multiBis')->
  309. with('response')->begin()->
  310. // when in cache
  311. checkElement('link[href*="/main_css"]')->
  312. checkElement('script[src*="/main_js"]')->
  313. checkElement('link[href*="/partial_css"]')->
  314. checkElement('script[src*="/partial_js"]')->
  315. checkElement('link[href*="/another_partial_css"]')->
  316. checkElement('script[src*="/another_partial_js"]')->
  317. checkElement('link[href*="/component_css"]')->
  318. checkElement('script[src*="/component_js"]')->
  319. checkElement('#partial_slot_content', 'Partial')->
  320. checkElement('#another_partial_slot_content', 'Another Partial')->
  321. checkElement('#component_slot_content', 'Component')->
  322. end()
  323. ;
  324. $b->
  325. get('/cache/partial')->
  326. with('request')->begin()->
  327. isParameter('module', 'cache')->
  328. isParameter('action', 'partial')->
  329. end()->
  330. with('response')->begin()->
  331. isStatusCode(200)->
  332. // only partial specific css and js are included
  333. checkElement('link[href*="/main_css"]', false)->
  334. checkElement('script[src*="/main_js"]', false)->
  335. checkElement('link[href*="/partial_css"]')->
  336. checkElement('script[src*="/partial_js"]')->
  337. checkElement('link[href*="/another_partial_css"]')->
  338. checkElement('script[src*="/another_partial_js"]')->
  339. checkElement('link[href*="/component_css"]', false)->
  340. checkElement('script[src*="/component_js"]', false)->
  341. checkElement('#partial_slot_content', 'Partial')->
  342. checkElement('#another_partial_slot_content', 'Another Partial')->
  343. checkElement('#component_slot_content', '')->
  344. end()->
  345. get('/cache/anotherPartial')->
  346. with('request')->begin()->
  347. isParameter('module', 'cache')->
  348. isParameter('action', 'anotherPartial')->
  349. end()->
  350. with('response')->begin()->
  351. isStatusCode(200)->
  352. // only partial specific css and js are included
  353. checkElement('link[href*="/main_css"]', false)->
  354. checkElement('script[src*="/main_js"]', false)->
  355. checkElement('link[href*="/partial_css"]', false)->
  356. checkElement('script[src*="/partial_js"]', false)->
  357. checkElement('link[href*="/another_partial_css"]')->
  358. checkElement('script[src*="/another_partial_js"]')->
  359. checkElement('link[href*="/component_css"]', false)->
  360. checkElement('script[src*="/component_js"]', false)->
  361. checkElement('#partial_slot_content', '')->
  362. checkElement('#another_partial_slot_content', 'Another Partial')->
  363. checkElement('#component_slot_content', '')->
  364. end()->
  365. get('/cache/component')->
  366. with('request')->begin()->
  367. isParameter('module', 'cache')->
  368. isParameter('action', 'component')->
  369. end()->
  370. with('response')->begin()->
  371. isStatusCode(200)->
  372. // only partial specific css and js are included
  373. checkElement('link[href*="/main_css"]', false)->
  374. checkElement('script[src*="/main_js"]', false)->
  375. checkElement('link[href*="/partial_css"]', false)->
  376. checkElement('script[src*="/partial_js"]', false)->
  377. checkElement('link[href*="/another_partial_css"]', false)->
  378. checkElement('script[src*="/another_partial_js"]', false)->
  379. checkElement('link[href*="/component_css"]')->
  380. checkElement('script[src*="/component_js"]')->
  381. checkElement('#partial_slot_content', '')->
  382. checkElement('#another_partial_slot_content', '')->
  383. checkElement('#component_slot_content', 'Component')->
  384. end()
  385. ;
  386. $b->get('/')
  387. ->with('view_cache')->isUriCached('cache/list', false)
  388. ->get('/cache/list')
  389. ->with('view_cache')->isUriCached('cache/list', true)
  390. // include GET parameters
  391. ->with('view_cache')->isUriCached('cache/list?page=10', false)
  392. ->get('/cache/list?page=10')
  393. ->with('response')->checkElement('#page', '10')
  394. ->with('view_cache')->isUriCached('cache/list?page=10', true)
  395. // include different GET parameters
  396. ->with('view_cache')->isUriCached('cache/list?page=20', false)
  397. ->get('/cache/list?page=20')
  398. ->with('response')->checkElement('#page', '20')
  399. ->with('view_cache')->isUriCached('cache/list?page=20', true)
  400. ;
  401. // test with sfFileCache class (default)
  402. $b->launch();
  403. // test with sfSQLiteCache class
  404. if (extension_loaded('SQLite') || extension_loaded('pdo_SQLite'))
  405. {
  406. sfConfig::set('sf_factory_view_cache', 'sfSQLiteCache');
  407. sfConfig::set('sf_factory_view_cache_parameters', array('database' => sfConfig::get('sf_template_cache_dir').DIRECTORY_SEPARATOR.'cache.db'));
  408. $b->launch();
  409. }