/Template/tests/cache_manager_test.php

https://github.com/activeingredient/ezComponents · PHP · 246 lines · 142 code · 44 blank · 60 comment · 0 complexity · 05ef2534c6e9f2eac027ad9fac4bdaf5 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  4. * @license http://ez.no/licenses/new_bsd New BSD License
  5. * @version //autogentag//
  6. * @filesource
  7. * @package Template
  8. * @subpackage Tests
  9. */
  10. require "db_cache_manager.php";
  11. require "fetch.php";
  12. /**
  13. * @package Template
  14. * @subpackage Tests
  15. */
  16. class ezcTemplateCacheManagerTest extends ezcTestCase
  17. {
  18. private $tempDir;
  19. private $basePath;
  20. private $templatePath;
  21. public static function suite()
  22. {
  23. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  24. }
  25. protected function setUp()
  26. {
  27. $tables = array( 'user', 'cache_templates', 'cache_values' );
  28. // Get the DB instance
  29. try
  30. {
  31. $db = ezcDbInstance::get();
  32. }
  33. catch ( Exception $e )
  34. {
  35. $this->markTestSkipped( 'No database handler defined' );
  36. }
  37. $this->basePath = realpath( dirname( __FILE__ ) ) . '/';
  38. // Setup the template engine
  39. $config = ezcTemplateConfiguration::getInstance();
  40. $this->tempDir = $config->compilePath = $this->createTempDir( "ezcTemplate_" );
  41. $config->templatePath = $this->basePath . 'templates/';
  42. $config->disableCache = false;
  43. $config->cacheManager = new DbCacheManager();
  44. // Create tables.
  45. foreach ( $tables as $table )
  46. {
  47. try
  48. {
  49. $db->exec( "DROP TABLE $table" );
  50. }
  51. catch ( Exception $e )
  52. {
  53. } // eat
  54. }
  55. $schema = ezcDbSchema::createFromFile( 'xml', dirname( __FILE__ ) . '/cache-manager-schema.xml' );
  56. $schema->writeToDb( $db );
  57. // insert some data
  58. $iq = $db->createInsertQuery();
  59. $s = $iq->insertInto( $db->quoteIdentifier( 'user' ) )
  60. ->set( $db->quoteIdentifier( 'id' ), 1 )
  61. ->set( $db->quoteIdentifier( 'name' ), $iq->bindValue( 'Raymond' ) )
  62. ->set( $db->quoteIdentifier( 'nickname' ), $iq->bindValue( 'sunRay' ) )
  63. ->prepare();
  64. $s->execute();
  65. $iq = $db->createInsertQuery();
  66. $s = $iq->insertInto( $db->quoteIdentifier( 'user' ) )
  67. ->set( $db->quoteIdentifier( 'id' ), 2 )
  68. ->set( $db->quoteIdentifier( 'name' ), $iq->bindValue( 'Derick' ) )
  69. ->set( $db->quoteIdentifier( 'nickname' ), $iq->bindValue( 'Tiger' ) )
  70. ->prepare();
  71. $s->execute();
  72. $iq = $db->createInsertQuery();
  73. $s = $iq->insertInto( $db->quoteIdentifier( 'user' ) )
  74. ->set( $db->quoteIdentifier( 'id' ), 3 )
  75. ->set( $db->quoteIdentifier( 'name' ), $iq->bindValue( 'Jan' ) )
  76. ->set( $db->quoteIdentifier( 'nickname' ), $iq->bindValue( 'Amos' ) )
  77. ->prepare();
  78. $s->execute();
  79. }
  80. protected function tearDown()
  81. {
  82. // Remove tables.
  83. $db = ezcDbInstance::get();
  84. // $db->exec( 'DROP TABLE cache_templates' );
  85. // $db->exec( 'DROP TABLE cache_values' );
  86. // $db->exec( 'DROP TABLE user' );
  87. $this->removeTempDir();
  88. }
  89. public function testRenewIncludedTemplates()
  90. {
  91. $t = new ezcTemplate();
  92. $t->configuration->templatePath = $this->tempDir;
  93. copy( $this->basePath."/templates/cache_simple_include.tpl", $this->tempDir . "/cache_simple_include.tpl" );
  94. copy( $this->basePath."/templates/hello_world.tpl", $this->tempDir . "/hello_world.tpl" );
  95. $t->send->a = "Bernard";
  96. $r = $t->process( "cache_simple_include.tpl" );
  97. $this->assertEquals( "\nBernard\nHello world\n", $r );
  98. // Old value expected. $a is not a cache key.
  99. $t->send->a = "Bla";
  100. $r = $t->process( "cache_simple_include.tpl" );
  101. $this->assertEquals( "\nBernard\nHello world\n", $r );
  102. // Simulate someone edits the template
  103. sleep(1); // Otherwise the mtime is the same.
  104. file_put_contents( $this->tempDir . "/hello_world.tpl", "Goodbye cruel world!");
  105. $t->send->a = "Bla";
  106. $r = $t->process( "cache_simple_include.tpl" );
  107. $this->assertEquals( "\nBla\nGoodbye cruel world!", $r );
  108. }
  109. public function testRenewUserViaTemplateFetch()
  110. {
  111. $t = new ezcTemplate();
  112. $t->configuration->addExtension("Fetch");
  113. $r = $t->process("show_users.ezt");
  114. $this->assertEquals( "\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  115. // Update a single user.
  116. $db = ezcDbInstance::get();
  117. $db->exec( 'UPDATE user SET nickname="bla" WHERE id=1' );
  118. // Still cached.
  119. $r = $t->process("show_users.ezt");
  120. $this->assertEquals( "\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  121. // Send a update signal to the configuration manager.
  122. ezcTemplateConfiguration::getInstance()->cacheManager->update("user", 1 );
  123. $r = $t->process("show_users.ezt");
  124. $this->assertEquals( "\n\n\n\n1 Raymond bla\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  125. }
  126. // Test if the read() call is send to all the cache files.
  127. public function testMultiCache()
  128. {
  129. $t = new ezcTemplate();
  130. $t->configuration->addExtension("Fetch");
  131. $r = $t->process("cached_page_includes_show_users.ezt");
  132. $this->assertEquals( "\nCached:\n\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  133. $db = ezcDbInstance::get();
  134. $db->exec( 'UPDATE user SET nickname="bla" WHERE id=1' );
  135. ezcTemplateConfiguration::getInstance()->cacheManager->update("user", 1 );
  136. $r = $t->process("cached_page_includes_show_users.ezt");
  137. $this->assertEquals( "\nCached:\n\n\n\n\n1 Raymond bla\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  138. }
  139. public function testCleanExpired()
  140. {
  141. $t = new ezcTemplate();
  142. $t->configuration->addExtension("Fetch");
  143. $r = $t->process("cached_page_includes_show_users.ezt");
  144. $this->assertEquals( "\nCached:\n\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  145. $db = ezcDbInstance::get();
  146. $db->exec( 'UPDATE user SET nickname="bla" WHERE id=1' );
  147. ezcTemplateConfiguration::getInstance()->cacheManager->update("user", 1 );
  148. // Clean up.
  149. ezcTemplateConfiguration::getInstance()->cacheManager->cleanExpired();
  150. // Set the values to the old stuff, without notifying the cache manager.
  151. $db->exec( 'UPDATE user SET nickname="sunRay" WHERE id=1' );
  152. // This should show the old template again.
  153. $r = $t->process("cached_page_includes_show_users.ezt");
  154. $this->assertEquals( "\nCached:\n\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  155. }
  156. public function testCacheBlock()
  157. {
  158. $t = new ezcTemplate();
  159. $t->configuration->addExtension("Fetch");
  160. $r = $t->process("show_users_cache_block.ezt");
  161. $this->assertEquals( "\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  162. // Update a single user.
  163. $db = ezcDbInstance::get();
  164. $db->exec( 'UPDATE user SET nickname="bla" WHERE id=1' );
  165. // Still cached.
  166. $r = $t->process("show_users_cache_block.ezt");
  167. $this->assertEquals( "\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  168. // Send a update signal to the configuration manager.
  169. ezcTemplateConfiguration::getInstance()->cacheManager->update("user", 1 );
  170. $r = $t->process("show_users_cache_block.ezt");
  171. $this->assertEquals( "\n\n\n\n1 Raymond bla\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r );
  172. }
  173. /*
  174. public function testCacheKeys()
  175. {
  176. $t = new ezcTemplate();
  177. $t->configuration->addExtension("Fetch");
  178. $t->send->id = 1;
  179. $t->send->name = "aaa";
  180. $r = $t->process("cache_manager_with_keys.tpl");
  181. ezcTemplateConfiguration::getInstance()->cacheManager->register("user", 1 );
  182. $this->assertEquals( "\n\n\n1\naaa\n", $r );
  183. // Update a single user.
  184. $t->send->id = 1;
  185. $t->send->name = "bla";
  186. // Still cached.
  187. $r = $t->process("cache_manager_with_keys.tpl");
  188. $this->assertEquals( "\n\n\n1\naaa\n", $r );
  189. // Send a update signal to the configuration manager.
  190. ezcTemplateConfiguration::getInstance()->cacheManager->update("user", 1 );
  191. $r = $t->process("cache_manager_with_keys.tpl");
  192. $this->assertEquals( "\n\n\n1\nbla\n", $r );
  193. }
  194. */
  195. }
  196. ?>