1. sfTesterViewCache.class.php
  2. /** * sfTesterViewCache implements tests for the symfony view cache manager. * * @package symfony * @subpackage test * @author Fabien Potencier * @version SVN: $Id: sfTesterViewCache.class.php 24615 2009-11-30 22:30:46Z Kris.Wallsmith $ */
  3. class sfTesterViewCache extends sfTester
  4. {
  5. protected
  6. $viewCacheManager = null,
  7. $response = null,
  8. $routing = null;
  9. /**
  10. * Prepares the tester.
  11. */
  12. public function prepare()
  13. {
  14. }
  15. /**
  16. * Initializes the tester.
  17. */
  18. public function initialize()
  19. {
  20. $this->viewCacheManager = $this->browser->getContext()->getViewCacheManager();
  21. $this->routing = $this->browser->getContext()->getRouting();
  22. $this->response = $this->browser->getResponse();
  23. }
  24. /**
  25. * Tests if the given uri is cached.
  26. *
  27. * @param boolean $boolean Flag for checking the cache
  28. * @param boolean $with_layout If have or not layout
  29. *
  30. * @return sfTestFunctionalBase|sfTester
  31. */
  32. public function isCached($boolean, $with_layout = false)
  33. {
  34. return $this->isUriCached($this->viewCacheManager->getCurrentCacheKey(), $boolean, $with_layout);
  35. }
  36. /**
  37. * Tests if the given uri is cached.
  38. *
  39. * @param string $uri Uniform resource identifier
  40. * @param boolean $boolean Flag for checking the cache
  41. * @param boolean $with_layout If have or not layout
  42. *
  43. * @return sfTestFunctionalBase|sfTester
  44. */
  45. public function isUriCached($uri, $boolean, $with_layout = false)
  46. {
  47. $cacheManager = $this->viewCacheManager;
  48. // check that cache is enabled
  49. if (!$cacheManager)
  50. {
  51. $this->tester->ok(!$boolean, 'cache is disabled');
  52. return $this->getObjectToReturn();
  53. }
  54. if ($uri == $this->viewCacheManager->getCurrentCacheKey())
  55. {
  56. $main = true;
  57. $type = $with_layout ? 'page' : 'action';
  58. }
  59. else
  60. {
  61. $main = false;
  62. $type = $uri;
  63. }
  64. // check layout configuration
  65. if ($cacheManager->withLayout($uri) && !$with_layout)
  66. {
  67. $this->tester->fail('cache without layout');
  68. $this->tester->skip('cache is not configured properly', 2);
  69. }
  70. else if (!$cacheManager->withLayout($uri) && $with_layout)
  71. {
  72. $this->tester->fail('cache with layout');
  73. $this->tester->skip('cache is not configured properly', 2);
  74. }
  75. else
  76. {
  77. $this->tester->pass('cache is configured properly');
  78. // check page is cached
  79. $ret = $this->tester->is($cacheManager->has($uri), $boolean, sprintf('"%s" %s in cache', $type, $boolean ? 'is' : 'is not'));
  80. // check that the content is ok in cache
  81. if ($boolean)
  82. {
  83. if (!$ret)
  84. {
  85. $this->tester->fail('content in cache is ok');
  86. }
  87. else if ($with_layout)
  88. {
  89. $response = unserialize($cacheManager->get($uri));
  90. $content = $response->getContent();
  91. $this->tester->ok($content == $this->response->getContent(), 'content in cache is ok');
  92. }
  93. else
  94. {
  95. $ret = unserialize($cacheManager->get($uri));
  96. $content = $ret['content'];
  97. $this->tester->ok(false !== strpos($this->response->getContent(), $content), 'content in cache is ok');
  98. }
  99. }
  100. }
  101. return $this->getObjectToReturn();
  102. }
  103. }

Debug toolbar