<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>tests/test_app/webroot/css/style1.css</filename>
    </added>
    <added>
      <filename>tests/test_app/webroot/css/style2.css</filename>
    </added>
    <added>
      <filename>tests/test_app/webroot/js/script1.js</filename>
    </added>
    <added>
      <filename>tests/test_app/webroot/js/script2.js</filename>
    </added>
    <added>
      <filename>vendors/css/style3.css</filename>
    </added>
    <added>
      <filename>vendors/js/script3.js</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,16 +1,23 @@
 &lt;?php
 App::import('Helper', array('Asset.Asset', 'Javascript', 'Html'));
-App::import('Core', array('Folder'));
+App::import('Core', array('Folder', 'View'));
 
 class AssetTestCase extends CakeTestCase {
   var $Asset = null;
   var $Folder = null;
+  var $View = null;
+  
   var $www_root = null;
+  var $jsCache = null;
+  var $cssCache = null;
   
-  function start() {
-    parent::start();
-    
+  function startCase() {
     $this-&gt;www_root = ROOT . DS . 'app' . DS . 'plugins' . DS . 'asset' . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS;
+    $this-&gt;jsCache = $this-&gt;www_root . 'cjs' . DS;
+    $this-&gt;cssCache = $this-&gt;www_root . 'ccss' . DS;
+
+    $controller = null;
+    $this-&gt;View = new View($controller);
     
     $this-&gt;Asset = new AssetHelper(array('www_root' =&gt; $this-&gt;www_root, 'js' =&gt; $this-&gt;www_root . 'js' . DS, 'css' =&gt; $this-&gt;www_root . 'css' . DS));
     $this-&gt;Asset-&gt;Javascript = new JavascriptHelper();
@@ -18,29 +25,57 @@ class AssetTestCase extends CakeTestCase {
     
     $this-&gt;Folder = new Folder();
   }
+  
+  function endCase() {
+    $this-&gt;Folder-&gt;delete($this-&gt;jsCache);
+    $this-&gt;Folder-&gt;delete($this-&gt;cssCache);
+  }
+  
+  function startTest() {
+    $this-&gt;Asset-&gt;js = array();
+    $this-&gt;Asset-&gt;css = array();
+  }
 
   function testInstances() {
     $this-&gt;assertTrue(is_a($this-&gt;Asset, 'AssetHelper'));
     $this-&gt;assertTrue(is_a($this-&gt;Asset-&gt;Javascript, 'JavascriptHelper'));
     $this-&gt;assertTrue(is_a($this-&gt;Asset-&gt;Html, 'HtmlHelper'));
+    $this-&gt;assertTrue(is_a($this-&gt;View, 'View'));
+    $this-&gt;assertTrue(is_a(ClassRegistry::getObject('view'), 'View'));
+  }
+  
+  function testVendors() {
+    if(PHP5) {
+      App::import('Vendor', 'jsmin/jsmin');
+      $this-&gt;assertTrue(class_exists('JSMin'));
+    }
+    
+    App::import('Vendor', 'csstidy', array('file' =&gt; 'class.csstidy.php'));
+    $this-&gt;assertTrue(class_exists('csstidy'));
+  }
+  
+  function testAfterRender() {
+    $this-&gt;View-&gt;__scripts = array('script1', 'script2', 'script3');
+    $this-&gt;Asset-&gt;afterRender();
+    $this-&gt;assertEqual(3, $this-&gt;Asset-&gt;viewScriptCount);
   }
   
   function testGenerateFileName() {
-    $files = array('test1', 'test2', 'test3');
+    $files = array('script1', 'script2', 'script3');
     $name = $this-&gt;Asset-&gt;__generateFileName($files);
-    $this-&gt;assertEqual('test1_test2_test3', $name);
+    $this-&gt;assertEqual('script1_script2_script3', $name);
   }
   
   function testGenerateFileNameMd5() {
     $this-&gt;Asset-&gt;md5FileName = true;
-    $files = array('test1', 'test2', 'test3');
+    $files = array('script1', 'script2', 'script3');
     $name = $this-&gt;Asset-&gt;__generateFileName($files);
-    $this-&gt;assertEqual('658f623f5f77d24124bb35c576304bf3', $name);
+    $this-&gt;assertEqual('4991a54c1356544e1188bf6c8b9e7ae9', $name);
     $this-&gt;Asset-&gt;md5FileName = false;
   }
   
   function testGetFileContents() {
-    $contents = $this-&gt;Asset-&gt;__getFileContents(array('plugin' =&gt; '', 'script' =&gt; 'test1'), 'js');
+    $contents = $this-&gt;Asset-&gt;__getFileContents(array('plugin' =&gt; '', 'script' =&gt; 'script1'), 'js');
     $expected = &lt;&lt;&lt;END
 var str = &quot;I'm a string&quot;;
 alert(str);
@@ -49,7 +84,7 @@ END;
   }
   
   function testGetFileContentsPlugin() {
-    $contents = $this-&gt;Asset-&gt;__getFileContents(array('plugin' =&gt; 'asset', 'script' =&gt; 'test3'), 'js');
+    $contents = $this-&gt;Asset-&gt;__getFileContents(array('plugin' =&gt; 'asset', 'script' =&gt; 'script3'), 'js');
     $expected = &lt;&lt;&lt;END
 $(function(){
   $(&quot;#nav&quot;).show();
@@ -59,43 +94,38 @@ END;
   }
   
   function testProcessJsNew() {
-    $outputDir = $this-&gt;www_root . 'cjs' . DS;
-    if(is_dir($outputDir)) {
-      $this-&gt;Folder-&gt;delete($outputDir);
-    }
-    $this-&gt;assertFalse(is_dir($outputDir));
+    $this-&gt;assertFalse(is_dir($this-&gt;jsCache));
     
-    $js = array(array('plugin' =&gt; '', 'script' =&gt; 'test1'),
-                array('plugin' =&gt; '', 'script' =&gt; 'test2'),
-                array('plugin' =&gt; 'asset', 'script' =&gt; 'test3'));
+    $js = array(array('plugin' =&gt; '', 'script' =&gt; 'script1'),
+                array('plugin' =&gt; '', 'script' =&gt; 'script2'),
+                array('plugin' =&gt; 'asset', 'script' =&gt; 'script3'));
     
     $fileName = $this-&gt;Asset-&gt;__process('js', $js);
     $expected = &lt;&lt;&lt;END
-/* test1.js (91%) */
+/* script1.js (91%) */
 var str=&quot;I'm a string&quot;;alert(str);
 
-/* test2.js (69%) */
+/* script2.js (73%) */
 var sum=0;for(i=0;i&lt;100;i++){sum+=i;}
 alert(i);
 
-/* test3.js (89%) */
+/* script3.js (89%) */
 \$(function(){\$(&quot;#nav&quot;).show();});
 END;
-    $contents = file_get_contents($outputDir . $fileName);
+    $contents = file_get_contents($this-&gt;jsCache . $fileName);
     $this-&gt;assertEqual($expected, $contents);
   }
   
   function testProcessJsExistingNoChanges() {
-    $outputDir = $this-&gt;www_root . 'cjs' . DS;
-    $this-&gt;Folder-&gt;cd($outputDir);
-    $files = $this-&gt;Folder-&gt;find('test1_test2_test3_([0-9]{10}).js');
+    $this-&gt;Folder-&gt;cd($this-&gt;jsCache);
+    $files = $this-&gt;Folder-&gt;find('script1_script2_script3_([0-9]{10}).js');
     
     $this-&gt;assertTrue(!empty($files[0]));
     $origFileName = $files[0];
 
-    $js = array(array('plugin' =&gt; '', 'script' =&gt; 'test1'),
-                array('plugin' =&gt; '', 'script' =&gt; 'test2'),
-                array('plugin' =&gt; 'asset', 'script' =&gt; 'test3'));
+    $js = array(array('plugin' =&gt; '', 'script' =&gt; 'script1'),
+                array('plugin' =&gt; '', 'script' =&gt; 'script2'),
+                array('plugin' =&gt; 'asset', 'script' =&gt; 'script3'));
     
     $this-&gt;Asset-&gt;checkTs = true;
     $fileName = $this-&gt;Asset-&gt;__process('js', $js);
@@ -103,23 +133,98 @@ END;
   }
   
   function testProcessJsExistingWithChanges() {
-    $outputDir = $this-&gt;www_root . 'cjs' . DS;
-    $this-&gt;Folder-&gt;cd($outputDir);
-    $files = $this-&gt;Folder-&gt;find('test1_test2_test3_([0-9]{10}).js');
+    $this-&gt;Folder-&gt;cd($this-&gt;jsCache);
+    $files = $this-&gt;Folder-&gt;find('script1_script2_script3_([0-9]{10}).js');
     
     $this-&gt;assertTrue(!empty($files[0]));
     $origFileName = $files[0];
 
     sleep(1);
-    $touched = touch($this-&gt;www_root . 'js' . DS . 'test1.js');
+    $touched = touch($this-&gt;www_root . 'js' . DS . 'script1.js');
     $this-&gt;assertTrue($touched);
     
-    $js = array(array('plugin' =&gt; '', 'script' =&gt; 'test1'),
-                array('plugin' =&gt; '', 'script' =&gt; 'test2'),
-                array('plugin' =&gt; 'asset', 'script' =&gt; 'test3'));
+    $js = array(array('plugin' =&gt; '', 'script' =&gt; 'script1'),
+                array('plugin' =&gt; '', 'script' =&gt; 'script2'),
+                array('plugin' =&gt; 'asset', 'script' =&gt; 'script3'));
     
     $this-&gt;Asset-&gt;checkTs = true;
     $fileName = $this-&gt;Asset-&gt;__process('js', $js);
     $this-&gt;assertNotEqual($origFileName, $fileName);
   }
+  
+  function testProcessCssNew() {
+    $this-&gt;assertFalse(is_dir($this-&gt;cssCache));
+    
+    $css = array(array('plugin' =&gt; '', 'script' =&gt; 'style1'),
+                array('plugin' =&gt; '', 'script' =&gt; 'style2'),
+                array('plugin' =&gt; 'asset', 'script' =&gt; 'style3'));
+    
+    $fileName = $this-&gt;Asset-&gt;__process('css', $css);
+    $expected = &lt;&lt;&lt;END
+/* style1.css (70%) */
+*{margin:0;padding:0;}
+
+/* style2.css (85%) */
+body{background:#003d4c;color:#fff;font-family:'lucida grande',verdana,helvetica,arial,sans-serif;font-size:90%;margin:0;}
+
+/* style3.css (69%) */
+h1,h2,h3,h4{font-weight:400;}
+END;
+    $contents = file_get_contents($this-&gt;cssCache . $fileName  . '.css');
+    $this-&gt;assertEqual($expected, $contents);
+  }
+  
+  function testInit() {
+    $this-&gt;View-&gt;__scripts = array ('&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/style1.css&quot; /&gt;',
+                      '&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/style2.css&quot; /&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script1.js&quot;&gt;&lt;/script&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script2.js&quot;&gt;&lt;/script&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;/asset/js/script3.js&quot;&gt;&lt;/script&gt;'
+    );
+    
+    
+    $this-&gt;Asset-&gt;__init();
+    
+    $this-&gt;assertEqual($this-&gt;Asset-&gt;js, array(array('plugin' =&gt; '', 'script' =&gt; 'script1'),
+                                             array('plugin' =&gt; '', 'script' =&gt; 'script2'),
+                                             array('plugin' =&gt; 'asset', 'script' =&gt; 'script3')));
+    $this-&gt;assertEqual($this-&gt;Asset-&gt;css, array(array('plugin' =&gt; '', 'script' =&gt; 'style1'),
+                                              array('plugin' =&gt; '', 'script' =&gt; 'style2')));
+  }
+  
+  function testScriptsForLayout() {
+    $this-&gt;View-&gt;__scripts = array ('&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/style1.css&quot; /&gt;',
+                      '&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/style2.css&quot; /&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script1.js&quot;&gt;&lt;/script&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script2.js&quot;&gt;&lt;/script&gt;',
+                      '&lt;script type=&quot;text/javascript&quot; src=&quot;/asset/js/script3.js&quot;&gt;&lt;/script&gt;'
+    );
+    
+    $scripts = $this-&gt;Asset-&gt;scripts_for_layout();
+    $expected = '/&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;\/ccss\/style1_style2_[0-9]{10}.css&quot; \/&gt;' . &quot;\n\t&quot; .
+                '&lt;script type=&quot;text\/javascript&quot; src=&quot;\/cjs\/script1_script2_script3_[0-9]{10}.js&quot;&gt;&lt;\/script&gt;/';
+                
+    $this-&gt;assertPattern($expected, $scripts);
+  }
+  
+  function testWithCodeBlock() {
+    $this-&gt;View-&gt;__scripts = array ('&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script1.js&quot;&gt;&lt;/script&gt;',
+                                    '&lt;script type=&quot;text/javascript&quot;&gt;//&lt;![CDATA[alert(&quot;test&quot;);//]]&gt;&lt;/script&gt;',
+                                    '&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script2.js&quot;&gt;&lt;/script&gt;'
+    );
+    $scripts = $this-&gt;Asset-&gt;scripts_for_layout();
+    $expected = '/&lt;script type=&quot;text\/javascript&quot; src=&quot;\/cjs\/script1_script2_[0-9]{10}.js&quot;&gt;&lt;\/script&gt;&lt;script type=&quot;text\/javascript&quot;&gt;\/\/&lt;!\[CDATA\[alert\(&quot;test&quot;\);\/\/]]&gt;&lt;\/script&gt;/';
+    $this-&gt;assertPattern($expected, $scripts);
+  }
+  
+  function testWithScriptsInLayout() {
+    $this-&gt;View-&gt;__scripts = array ('&lt;script type=&quot;text/javascript&quot; src=&quot;/js/script1.js&quot;&gt;&lt;/script&gt;',
+                                    '&lt;script type=&quot;text/javascript&quot; src=&quot;/js/layout.js&quot;&gt;&lt;/script&gt;');
+    $this-&gt;Asset-&gt;viewScriptCount = 1;
+    $scripts = $this-&gt;Asset-&gt;scripts_for_layout();
+    $expected = '/&lt;script type=&quot;text\/javascript&quot; src=&quot;\/cjs\/layout_script1_[0-9]{10}.js&quot;&gt;&lt;\/script&gt;/';
+    $this-&gt;assertPattern($expected, $scripts);
+  }
 }
\ No newline at end of file</diff>
      <filename>tests/cases/helpers/asset.test.php</filename>
    </modified>
    <modified>
      <diff>@@ -47,16 +47,18 @@ class AssetHelper extends Helper {
   var $initialized = false;
   var $js = array();
   var $css = array();
+  
+  var $View = null;
 
   function __construct($paths=array()) {
     $this-&gt;paths = am($this-&gt;paths, $paths);
+    $this-&gt;view =&amp; ClassRegistry::getObject('view');
   }
 
   //flag so we know the view is done rendering and it's the layouts turn
   function afterRender() {
-    $view =&amp; ClassRegistry::getObject('view');
-    if ($view) {
-      $this-&gt;viewScriptCount = count($view-&gt;__scripts);
+    if ($this-&gt;view) {
+      $this-&gt;viewScriptCount = count($this-&gt;view-&gt;__scripts);
     }
   }
 
@@ -82,18 +84,16 @@ class AssetHelper extends Helper {
     }
 
     //anything leftover is outputted directly
-    if (!empty($view-&gt;__scripts)) {
-      $scripts_for_layout .= join(&quot;\n\t&quot;, $view-&gt;__scripts);
+    if (!empty($this-&gt;view-&gt;__scripts)) {
+      $scripts_for_layout .= join(&quot;\n\t&quot;, $this-&gt;view-&gt;__scripts);
     }
 
     return $scripts_for_layout;
   }
 
   function __init() {
-    $view =&amp; ClassRegistry::getObject('view');
-
     //nothing to do
-    if (!$view-&gt;__scripts) {
+    if (!$this-&gt;view-&gt;__scripts) {
       return false;
     }
 
@@ -106,18 +106,18 @@ class AssetHelper extends Helper {
     }
 
     //compatible with DebugKit
-    if (!empty($view-&gt;viewVars['debugToolbarPanels'])) {
-      $this-&gt;viewScriptCount += 1 + count($view-&gt;viewVars['debugToolbarJavascript']);
+    if (!empty($this-&gt;view-&gt;viewVars['debugToolbarPanels'])) {
+      $this-&gt;view-&gt;viewScriptCount += 1 + count($this-&gt;view-&gt;viewVars['debugToolbarJavascript']);
     }
 
     //move the layout scripts to the front
-    $view-&gt;__scripts = array_merge(
-                         array_slice($view-&gt;__scripts, $this-&gt;viewScriptCount),
-                         array_slice($view-&gt;__scripts, 0, $this-&gt;viewScriptCount)
+    $this-&gt;view-&gt;__scripts = array_merge(
+                         array_slice($this-&gt;view-&gt;__scripts, $this-&gt;viewScriptCount),
+                         array_slice($this-&gt;view-&gt;__scripts, 0, $this-&gt;viewScriptCount)
                        );
 
     //split the scripts into js and css
-    foreach ($view-&gt;__scripts as $i =&gt; $script) {
+    foreach ($this-&gt;view-&gt;__scripts as $i =&gt; $script) {
       if (preg_match('/src=&quot;\/?(.*\/)?js\/(.*).js&quot;/', $script, $match)) {
         $temp = array();
         $temp['script'] = $match[2];
@@ -125,7 +125,7 @@ class AssetHelper extends Helper {
         $this-&gt;js[] = $temp;
 
         //remove the script since it will become part of the merged script
-        unset($view-&gt;__scripts[$i]);
+        unset($this-&gt;view-&gt;__scripts[$i]);
       } else if (preg_match('/href=&quot;\/?(.*\/)css\/(.*).css/', $script, $match)) {
         $temp = array();
         $temp['script'] = $match[2];
@@ -133,7 +133,7 @@ class AssetHelper extends Helper {
         $this-&gt;css[] = $temp;
 
         //remove the script since it will become part of the merged script
-        unset($view-&gt;__scripts[$i]);
+        unset($this-&gt;view-&gt;__scripts[$i]);
       }
     }
 
@@ -176,7 +176,18 @@ class AssetHelper extends Helper {
     //file doesn't exist.  create it.
     if (!$fileName) {
       $ts = time();
-
+      switch($type) {
+        case 'js':
+          if (PHP5) {
+            App::import('Vendor', 'jsmin/jsmin');
+          }
+        case 'css':
+          App::import('Vendor', 'csstidy', array('file' =&gt; 'class.csstidy.php'));
+          $tidy = new csstidy();
+          $tidy-&gt;load_template($this-&gt;cssCompression);
+          break;
+      }
+      
       //merge the script
       $scriptBuffer = '';
       foreach($assets as $asset) {
@@ -187,21 +198,21 @@ class AssetHelper extends Helper {
           case 'js':
             //jsmin only works with PHP5
             if (PHP5) {
-              App::import('Vendor', 'jsmin/jsmin');
               $buffer = trim(JSMin::minify($buffer));
             }
             break;
 
           case 'css':
-            App::import('Vendor', 'csstidy', array('file' =&gt; 'class.csstidy.php'));
-            $tidy = new csstidy();
-            $tidy-&gt;load_template($this-&gt;cssCompression);
             $tidy-&gt;parse($buffer);
             $buffer = $tidy-&gt;print-&gt;plain();
             break;
         }
 
-        $scriptBuffer .= sprintf(&quot;/* %s.%s (%d%%) */\n&quot;, $asset['script'], $type, (strlen($buffer) / $origSize) * 100);
+        $delta = 0;
+        if ($origSize &gt; 0) {
+          $delta = (strlen($buffer) / $origSize) * 100;
+        }
+        $scriptBuffer .= sprintf(&quot;/* %s.%s (%d%%) */\n&quot;, $asset['script'], $type, $delta);
         $scriptBuffer .= $buffer . &quot;\n\n&quot;;
       }
 </diff>
      <filename>views/helpers/asset.php</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>tests/test_app/webroot/js/test1.js</filename>
    </removed>
    <removed>
      <filename>tests/test_app/webroot/js/test2.js</filename>
    </removed>
    <removed>
      <filename>vendors/js/test3.js</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>9c27108296c3dbdfdac731d87b891d2c39647357</id>
    </parent>
  </parents>
  <author>
    <name>unknown</name>
    <email>matt@mcurry.net</email>
  </author>
  <url>http://github.com/mcurry/asset/commit/e04210fa473a992a6e50e39a581f585543800314</url>
  <id>e04210fa473a992a6e50e39a581f585543800314</id>
  <committed-date>2009-05-08T18:57:06-07:00</committed-date>
  <authored-date>2009-05-08T18:57:06-07:00</authored-date>
  <message>more unit tests</message>
  <tree>0b74cc9daf76b6b684f10ed33206fc3caeee1fb7</tree>
  <committer>
    <name>unknown</name>
    <email>matt@mcurry.net</email>
  </committer>
</commit>
