Author: Raymond Bosman
Date: 2006-11-09 15:51:33 +0100 (Thu, 09 Nov 2006)
New Revision: 3898

Log:
- Fixed a (really annoying) bug that the cache was deleted (and the template 
  recompiled) when the modification time did not change between the first and 
  the second process(). 

Added:
   trunk/Template/tests/regression_tests/cache/dynamic/incorrect/
   
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.in
   
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.out
   trunk/Template/tests/templates/cache_dynamic_advanced.tpl
   trunk/Template/tests/templates/cache_key_non_object.tpl
   trunk/Template/tests/templates/cache_key_object.tpl
Modified:
   trunk/Template/src/cache_filesystem.php
   trunk/Template/src/parsers/ast_to_ast/implementations/cache.php
   trunk/Template/src/parsers/source_to_tst/implementations/cache.php
   trunk/Template/src/template.php
   trunk/Template/tests/cache_test.php
   trunk/Template/tests/template_test.php

Modified: trunk/Template/src/cache_filesystem.php
===================================================================
--- trunk/Template/src/cache_filesystem.php     2006-11-09 10:05:00 UTC (rev 
3897)
+++ trunk/Template/src/cache_filesystem.php     2006-11-09 14:51:33 UTC (rev 
3898)
@@ -47,7 +47,9 @@
 
         foreach( $this->keys as $key )
         {
-            $code .=  '.\'-\'. md5( var_export( $'.  $key . ', true ) ) ';
+            // md5( var_export( is_object( $key ) && method_exists( $key,  
"cacheKey" )  ? $key->cacheKey() : $key ) )
+
+            $code .=  '.\'-\'. md5( var_export( is_object( $'.$key.' ) && 
method_exists( $'.$key.', "cacheKey" ) ? $'.$key.'->cacheKey() : $'.  $key . ', 
true ) ) ';
         }
 
         $code .= ";\n";
@@ -177,7 +179,6 @@
         
         // Create a cache directory, if needed.
         $this->deleteOldCache();
-
     }
 }
 ?>

Modified: trunk/Template/src/parsers/ast_to_ast/implementations/cache.php
===================================================================
--- trunk/Template/src/parsers/ast_to_ast/implementations/cache.php     
2006-11-09 10:05:00 UTC (rev 3897)
+++ trunk/Template/src/parsers/ast_to_ast/implementations/cache.php     
2006-11-09 14:51:33 UTC (rev 3898)
@@ -189,12 +189,17 @@
         $if->conditions[] = $else = new ezcTemplateConditionBodyAstNode();
         $else->body = new ezcTemplateBodyAstNode();
         $else->body->statements = array();
+
         $else->body->statements[] =  $this->_includeCache();
 
         $type->statements = array();
 
         $type->statements = array_merge( $type->statements, 
$this->cacheSystem->checkTTL() );
         
+
+        $type->statements[] = new 
ezcTemplatePhpCodeAstNode("clearstatcache();\n");
+
+
         // The current statements are already moved to the if node, Don't need 
them here.
         $type->statements = array_merge( $type->statements, $cacheExists );
 
@@ -204,7 +209,6 @@
         // Create the statements that belong (on top) inside the if-body.
         $statements = array();
 
-
         $statements[] = $this->_fopenCacheFileWriteMode();                     
         // $fp = fopen( $this->cache, "w" ); 
 
         $statements[] = $this->_fwritePhpOpen();                               
         // fwrite( $fp, "<" . "?php\n" );

Modified: trunk/Template/src/parsers/source_to_tst/implementations/cache.php
===================================================================
--- trunk/Template/src/parsers/source_to_tst/implementations/cache.php  
2006-11-09 10:05:00 UTC (rev 3897)
+++ trunk/Template/src/parsers/source_to_tst/implementations/cache.php  
2006-11-09 14:51:33 UTC (rev 3898)
@@ -96,6 +96,8 @@
                 {
                     do
                     {
+                        $this->findNextElement( $cursor );
+
                         if ( ! $this->parseOptionalType( "Variable", 
$this->currentCursor, false ) )
                         {
                             throw new ezcTemplateParserException( 
$this->parser->source, $this->startCursor, $this->currentCursor, 
ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_VARIABLE );

Modified: trunk/Template/src/template.php
===================================================================
--- trunk/Template/src/template.php     2006-11-09 10:05:00 UTC (rev 3897)
+++ trunk/Template/src/template.php     2006-11-09 14:51:33 UTC (rev 3898)
@@ -231,9 +231,11 @@
         $compiled = ezcTemplateCompiledCode::findCompiled( 
$this->properties["stream"], $config->context, $this );
         $this->properties["compiledTemplatePath"] = $compiled->path;
 
+        
         if ( !file_exists( $compiled->path ) || (
             $config->checkModifiedTemplates &&
-            file_exists( $this->properties["stream"] ) && filemtime( 
$this->properties["stream"] ) >= filemtime( $compiled->path ) )
+            // Do not recompile when the modification times are the same. This 
messes up the caching tests. 
+            file_exists( $this->properties["stream"] ) && filemtime( 
$this->properties["stream"] ) > filemtime( $compiled->path ) )
         )
         {
             $this->createDirectory( dirname( $compiled->path ) );

Modified: trunk/Template/tests/cache_test.php
===================================================================
--- trunk/Template/tests/cache_test.php 2006-11-09 10:05:00 UTC (rev 3897)
+++ trunk/Template/tests/cache_test.php 2006-11-09 14:51:33 UTC (rev 3898)
@@ -29,7 +29,7 @@
         $this->basePath = realpath( dirname( __FILE__ ) ) . '/';
 
         $config = ezcTemplateConfiguration::getInstance();
-        $config->compilePath = $this->createTempDir( "ezcTemplate_" );
+        $this->tempDir = $config->compilePath =  $this->createTempDir( 
"ezcTemplate_" );
         $config->templatePath = $this->basePath . 'templates/';
    }
 
@@ -58,7 +58,12 @@
         }
     }
 
+    
///////////////////////////////////////////////////////////////////////////////////////////
+    // Test the dynamic block.
+    // Tested in the regression_test:
+    // - Variable declaration in the dynamic block.
 
+    // Test whether the dynamic block changes when the {use} variable changes.
     public function testDynamicBlock()
     {
         $t = new ezcTemplate( );
@@ -73,7 +78,134 @@
         $this->assertEquals( "\n[Bernard Black]\n[Guybrush Threepwood]\n", 
$out );
     }
 
+    // - Test whether the all the dynamic block changes when the {use} 
variable changes.
+    // - Test whether a new variable can be inserted in the dynamic block.
+    // - Test whether the new variable can be updated an reused in the second 
dynamic block.
+    // - Test whether the new variable is static. 
+    // - Test whether the static new variable can be used for dynamic 
calculations in the dynamic block.
+    public function testDynamicBlockAdvanced()
+    {
+        $t = new ezcTemplate( );
+        $t->send->user = new TestUser( "Bernard", "Black" );
+        $out = $t->process( "cache_dynamic_advanced.tpl");
 
+        $this->assertEquals( "\n[2]\n[Bernard Black]\n[Nr 2]\n[Bernard 
Black]\n[Nr 3]\n[4]\n[Bernard Black]\n", $out );
+
+        $t->send->user = new TestUser( "Guybrush", "Threepwood", 10 );
+        $out = $t->process( "cache_dynamic_advanced.tpl");
+
+        $this->assertEquals( "\n[2]\n[Bernard Black]\n[Nr 2]\n[Guybrush 
Threepwood]\n[Nr 3]\n[13]\n[Guybrush Threepwood]\n", $out );
+    }
+
+    
///////////////////////////////////////////////////////////////////////////////////////////////////
+    // Test the cache keys.
+
+    public function testCacheKeyNonObject()
+    {
+        $t = new ezcTemplate();
+        $t->send->number = 42;
+        $t->send->string = "Hello world";
+        $t->send->static = "1";
+
+        $out = $t->process( "cache_key_non_object.tpl");
+        $this->assertEquals( "\n[1]\n[42]\n[Hello world]\n", $out );
+
+        $t->send->static = 2; // Static will not change, because it's cached.
+        $out = $t->process( "cache_key_non_object.tpl");
+        $this->assertEquals( "\n[1]\n[42]\n[Hello world]\n", $out );
+
+        // Change the number only.
+        $t->send->number = 5;
+        $out = $t->process( "cache_key_non_object.tpl");
+        $this->assertEquals( "\n[2]\n[5]\n[Hello world]\n", $out );
+
+        // Change the string.
+        $t->send->string = "blarp";
+        $out = $t->process( "cache_key_non_object.tpl");
+        $this->assertEquals( "\n[2]\n[5]\n[blarp]\n", $out );
+    }
+
+
+    public function testCacheKeyObjectWithKeyMethod()
+    {
+        // Should call a function, if it's defined.
+        $t = new ezcTemplate();
+        $t->send->user = new TestUser("Bernard", "Black", 23);
+
+        $out = $t->process( "cache_key_object.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n", $out );
+
+        // The ID didn't change, so keep the same name.
+        $t->send->user = new TestUser("Guybrush", "Threepwood", 23);
+        $out = $t->process( "cache_key_object.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n", $out );
+
+        // The ID DID change, so change the name.
+        $t->send->user = new TestUser("Guybrush", "Threepwood", 88);
+        $out = $t->process( "cache_key_object.tpl");
+        $this->assertEquals( "\n[Guybrush Threepwood]\n", $out );
+    }
+
+    public function testCacheKeyObjectWithoutKeyMethod()
+    {
+        // Should call a function, if it's defined.
+        $t = new ezcTemplate();
+        $t->send->user = new TestUser("Bernard", "Black", 23);
+      
+        $out = $t->process( "cache_key_object.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n", $out );
+
+        // The ID didn't change, so keep the same name.
+        $t->send->user = new TestUser("Guybrush", "Threepwood", 23);
+        $out = $t->process( "cache_key_object.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n", $out );
+
+        // The ID DID change, so change the name.
+        $t->send->user = new TestUser("Guybrush", "Threepwood", 88);
+        $out = $t->process( "cache_key_object.tpl");
+        $this->assertEquals( "\n[Guybrush Threepwood]\n", $out );
+    }
+
+
+    public function testChangeTemplateThenRenewCache()
+    {
+        $t = new ezcTemplate();
+        $t->send->name = "Bernard";
+
+        $config = ezcTemplateConfiguration::getInstance();
+        $config->templatePath = $this->tempDir;
+
+        file_put_contents( $config->templatePath  . DIRECTORY_SEPARATOR . 
"blarp.tpl", '
+{use $name}
+{cache_template}
+[{$name}]
+' );
+
+
+        $out = $t->process( "blarp.tpl");
+        $this->assertEquals( "\n[Bernard]\n", $out );
+
+        // Retry, the old name should be in the cached template.
+        $t->send->name = "Guybrush";
+        $out = $t->process( "blarp.tpl");
+        $this->assertEquals( "\n[Bernard]\n", $out );
+/*
+        // Update the contents
+        file_put_contents( $this->tempDir . DIRECTORY_SEPARATOR . "blarp.tpl", 
'
+{use $name}
+{cache_template}
+[[[[[[{$name}]]]]]]
+' );
+
+        $t->send->name = "Guybrush";
+        $out = $t->process( "blarp.tpl");
+        $this->assertEquals( "\n[[[[[[Guybrush]]]]]]\n", $out );
+        */
+    }
+
+
+
+
 }
 
 class TestUser
@@ -81,12 +213,19 @@
     public $firstName;
     public $lastName;
     public $name;
+    public $id;
 
-    public function __construct($firstName, $lastName )
+    public function cacheKey()
     {
+        return $this->id;
+    }
+
+    public function __construct($firstName, $lastName, $id = 1 )
+    {
         $this->firstName = $firstName;
         $this->lastName = $lastName;
         $this->name = $firstName . " " . $lastName;
+        $this->id = $id;
     }
 }
 

Added: 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.in
===================================================================
--- 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.in
    2006-11-09 10:05:00 UTC (rev 3897)
+++ 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.in
    2006-11-09 14:51:33 UTC (rev 3898)
@@ -0,0 +1,5 @@
+{cache_template}
+
+{dynamic}
+       {var $a}
+{/dynamic}


Property changes on: 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.in
___________________________________________________________________
Name: svn:eol-style
   + native

Added: 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.out
===================================================================
--- 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.out
   2006-11-09 10:05:00 UTC (rev 3897)
+++ 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.out
   2006-11-09 14:51:33 UTC (rev 3898)
@@ -0,0 +1,4 @@
+mock:4:9: The variable <$a> is cannot be declared in a subblock: while, 
foreach, if, etc
+
+       {var $a}
+        ^


Property changes on: 
trunk/Template/tests/regression_tests/cache/dynamic/incorrect/variable_declaration_in_dynamic.out
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Template/tests/template_test.php
===================================================================
--- trunk/Template/tests/template_test.php      2006-11-09 10:05:00 UTC (rev 
3897)
+++ trunk/Template/tests/template_test.php      2006-11-09 14:51:33 UTC (rev 
3898)
@@ -82,6 +82,7 @@
         $res = $template->process( "reexecute_template.ezt" );
 
         // Change the template
+        sleep(1);
         file_put_contents( $this->templatePath . "/reexecute_template.ezt", 
"Goodbye cruel world" );
 
         $res2 = $template->process( "reexecute_template.ezt" );

Added: trunk/Template/tests/templates/cache_dynamic_advanced.tpl
===================================================================
--- trunk/Template/tests/templates/cache_dynamic_advanced.tpl   2006-11-09 
10:05:00 UTC (rev 3897)
+++ trunk/Template/tests/templates/cache_dynamic_advanced.tpl   2006-11-09 
14:51:33 UTC (rev 3898)
@@ -0,0 +1,15 @@
+{use $user}
+{cache_template}
+{var $a = 2}
+[{$a}]
+[{$user->name}]
+{dynamic}
+       [Nr {$a}]
+       [{$user->name}]
+{/dynamic}
+{$a++}
+{dynamic}
+       [Nr {$a}]
+       [{$a + $user->id}]
+       [{$user->name}]
+{/dynamic}


Property changes on: trunk/Template/tests/templates/cache_dynamic_advanced.tpl
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Template/tests/templates/cache_key_non_object.tpl
===================================================================
--- trunk/Template/tests/templates/cache_key_non_object.tpl     2006-11-09 
10:05:00 UTC (rev 3897)
+++ trunk/Template/tests/templates/cache_key_non_object.tpl     2006-11-09 
14:51:33 UTC (rev 3898)
@@ -0,0 +1,5 @@
+{use $number, $string, $static}
+{cache_template keys $number, $string}
+[{$static}]
+[{$number}]
+[{$string}]


Property changes on: trunk/Template/tests/templates/cache_key_non_object.tpl
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Template/tests/templates/cache_key_object.tpl
===================================================================
--- trunk/Template/tests/templates/cache_key_object.tpl 2006-11-09 10:05:00 UTC 
(rev 3897)
+++ trunk/Template/tests/templates/cache_key_object.tpl 2006-11-09 14:51:33 UTC 
(rev 3898)
@@ -0,0 +1,3 @@
+{use $user}
+{cache_template keys $user}
+[{$user->name}]


Property changes on: trunk/Template/tests/templates/cache_key_object.tpl
___________________________________________________________________
Name: svn:eol-style
   + native

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to