Skip to content
Permalink
Browse files

Update CacheHelper tests

There were a few CacheHelper tests depending on removed properties in
Controller.  Also update CacheHelper to not use deprecated properties on
View as well.
  • Loading branch information...
markstory committed Oct 8, 2012
1 parent 1a6a5f2 commit 91200dd0cd4c4724e5e185ac5326baeafeb1b63b
Showing with 44 additions and 37 deletions.
  1. +22 −27 lib/Cake/Test/TestCase/View/Helper/CacheHelperTest.php
  2. +22 −10 lib/Cake/View/Helper/CacheHelper.php
@@ -1,9 +1,5 @@
<?php
/**
* CacheHelperTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -12,7 +8,6 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.View.Helper
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
@@ -31,7 +26,7 @@
/**
* CacheTestController class
*
* @package Cake.Test.Case.View.Helper
* @package Cake.Test.Case.View.Helper
*/
class CacheTestController extends Controller {
@@ -72,7 +67,7 @@ class CacheHelperTest extends TestCase {
*/
public function skip() {
if (!is_writable(TMP . 'cache/views/')) {
$this->markTestSkipped('TMP/views is not writable %s');
$this->markTestSkipped('TMP/views is not writable.');
}
}
@@ -83,16 +78,16 @@ public function skip() {
*/
public function setUp() {
parent::setUp();
$_GET = array();
$_GET = [];
$request = new Request();
$this->Controller = new CacheTestController($request);
$View = new View($this->Controller);
$this->Cache = new CacheHelper($View);
Configure::write('Cache.check', true);
Configure::write('Cache.disable', false);
App::build(array(
'View' => array(CAKE . 'Test/TestApp/View/')
), App::RESET);
App::build([
'View' => [CAKE . 'Test/TestApp/View/']
], App::RESET);
}
/**
@@ -514,14 +509,14 @@ public function testCacheBaseNameControllerName() {
$this->Controller->cacheAction = array(
'cache_name' => 21600
);
$this->Controller->params = array(
$request = $this->Controller->request;
$request->params = array(
'controller' => 'cacheTest',
'action' => 'cache_name',
'pass' => array(),
);
$this->Controller->here = '/cache/cacheTest/cache_name';
$this->Controller->action = 'cache_name';
$this->Controller->base = '/cache';
$request->here = '/cache/cacheTest/cache_name';
$request->base = '/cache';
$View = new View($this->Controller);
$result = $View->render('index');
@@ -543,7 +538,7 @@ public function testAfterRenderConditions() {
Configure::write('Cache.check', true);
$View = new View($this->Controller);
$View->cacheAction = '+1 day';
$View->output = 'test';
$View->assign('content', 'test');
$Cache = $this->getMock('Cake\View\Helper\CacheHelper', array('_parseContent'), array($View));
$Cache->expects($this->once())
@@ -570,12 +565,12 @@ public function testAfterLayoutConditions() {
Configure::write('Cache.check', true);
$View = new View($this->Controller);
$View->cacheAction = '+1 day';
$View->output = 'test';
$View->set('content', 'test');
$Cache = $this->getMock('Cake\View\Helper\CacheHelper', array('cache'), array($View));
$Cache->expects($this->once())
->method('cache')
->with('posts/index', $View->output)
->with('posts/index', $View->fetch('content'))
->will($this->returnValue(''));
$Cache->afterLayout('posts/index');
@@ -596,22 +591,22 @@ public function testAfterLayoutConditions() {
* @return void
*/
public function testCacheEmptySections() {
Configure::write('Cache.check', true);
$this->Controller->cache_parsing();
$this->Controller->params = array(
$this->Controller->request->addParams([
'controller' => 'cacheTest',
'action' => 'cache_empty_sections',
'pass' => array(),
);
$this->Controller->cacheAction = array('cache_empty_sections' => 21600);
$this->Controller->here = '/cacheTest/cache_empty_sections';
$this->Controller->action = 'cache_empty_sections';
'pass' => [],
]);
$this->Controller->request->here = '/cacheTest/cache_empty_sections';
$this->Controller->cacheAction = ['cache_empty_sections' => 21600];
$this->Controller->layout = 'cache_empty_sections';
$this->Controller->viewPath = 'Posts';
$View = new View($this->Controller);
$result = $View->render('cache_empty_sections');
$this->assertNotRegExp('/nocache/', $result);
$this->assertNotRegExp('/php echo/', $result);
$this->assertNotContains('nocache', $result);
$this->assertNotContains('<?php echo', $result);
$this->assertRegExp(
'@</title>\s*</head>\s*' .
'<body>\s*' .
@@ -622,7 +617,7 @@ public function testCacheEmptySections() {
$filename = CACHE . 'views/cachetest_cache_empty_sections.php';
$this->assertTrue(file_exists($filename));
$contents = file_get_contents($filename);
$this->assertNotRegExp('/nocache/', $contents);
$this->assertNotContains('nocache', $contents);
$this->assertRegExp(
'@<head>\s*<title>Posts</title>\s*' .
'<\?php \$x \= 1; \?>\s*' .
@@ -1,9 +1,5 @@
<?php
/**
* CacheHelper helps create full page view caching.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -12,11 +8,11 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View.Helper
* @since CakePHP(tm) v 1.0.0.2277
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\View\Helper;
use Cake\Core\Configure;
use Cake\Utility\Inflector;
use Cake\View\Helper;
@@ -27,7 +23,7 @@
* When using CacheHelper you don't call any of its methods, they are all automatically
* called by View, and use the $cacheAction settings set in the controller.
*
* @package Cake.View.Helper
* @package Cake.View.Helper
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html
*/
class CacheHelper extends Helper {
@@ -84,9 +80,15 @@ public function afterRenderFile($viewFile, $output) {
*/
public function afterLayout($layoutFile) {
if ($this->_enabled()) {
$this->_View->output = $this->cache($layoutFile, $this->_View->output);
$this->_View->assign(
'content',
$this->cache($layoutFile, $this->_View->fetch('content'))
);
}
$this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
$this->_View->assign(
'content',
preg_replace('/<!--\/?nocache-->/', '', $this->_View->fetch('content'))
);
}
/**
@@ -181,8 +183,18 @@ protected function _parseFile($file, $cache) {
} elseif ($file = fileExistsInPath($file)) {
$file = file_get_contents($file);
}
preg_match_all('/(<!--nocache:\d{3}-->(?<=<!--nocache:\d{3}-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
preg_match_all('/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i', $file, $fileResult, PREG_PATTERN_ORDER);
preg_match_all(
'/(<!--nocache:\d{3}-->(?<=<!--nocache:\d{3}-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i',
$cache,
$outputResult,
PREG_PATTERN_ORDER
);
preg_match_all(
'/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i',
$file,
$fileResult,
PREG_PATTERN_ORDER
);
$fileResult = $fileResult[0];
$outputResult = $outputResult[0];

0 comments on commit 91200dd

Please sign in to comment.
You can’t perform that action at this time.