sfTesterViewCache.class.php 3.5 KB

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