Skip to content

Commit

Permalink
Refactoring how buffering works.
Browse files Browse the repository at this point in the history
Adding buffer params to method calls.
Adding tests for buffer calls.
  • Loading branch information
markstory committed Mar 29, 2009
1 parent 57ceba5 commit f257831
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
25 changes: 20 additions & 5 deletions cake/libs/view/helpers/js.php
Expand Up @@ -114,10 +114,25 @@ function __construct($settings = array()) {
**/
function call__($method, $params) {
if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) {
$buffer = false;
if (in_array(strtolower($method), $this->{$this->__engineName}->bufferedMethods)) {
$buffer = true;
}
if (count($params) > 0) {
$lastParam = $params[count($params) - 1];
$hasBufferParam = (is_bool($lastParam) || is_array($lastParam) && isset($lastParam['buffer']));
if ($hasBufferParam && is_bool($lastParam)) {
$buffer = $lastParam;
unset($params[count($params) - 1]);
} elseif ($hasBufferParam && is_array($lastParam)) {
$buffer = $lastParam['buffer'];
unset($params['buffer']);
}
}
$out = $this->{$this->__engineName}->dispatchMethod($method, $params);
if ($this->bufferScripts && is_string($out)) {
if ($this->bufferScripts && $buffer && is_string($out)) {
$this->buffer($out);
return $out;
return null;
}
if (is_object($out) && is_a($out, 'JsBaseEngineHelper')) {
return $this;
Expand Down Expand Up @@ -286,10 +301,10 @@ class JsBaseEngineHelper extends AppHelper {
**/
var $_optionMap = array();
/**
* An array of Methods in the Engine that are buffered unless otherwise disabled. This allows specific 'end point'
* methods to be automatically buffered by the JsHelper.
* An array of lowercase method names in the Engine that are buffered unless otherwise disabled.
* This allows specific 'end point' methods to be automatically buffered by the JsHelper.
*
* @var string
* @var array
**/
var $bufferedMethods = array();
/**
Expand Down
65 changes: 58 additions & 7 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -105,16 +105,16 @@ function endTest() {
* @return void
**/
function testConstruction() {
$js = new JsHelper();
$js =& new JsHelper();
$this->assertEqual($js->helpers, array('Html', 'jqueryEngine'));

$js = new JsHelper(array('mootools'));
$js =& new JsHelper(array('mootools'));
$this->assertEqual($js->helpers, array('Html', 'mootoolsEngine'));

$js = new JsHelper('prototype');
$js =& new JsHelper('prototype');
$this->assertEqual($js->helpers, array('Html', 'prototypeEngine'));

$js = new JsHelper('MyPlugin.Dojo');
$js =& new JsHelper('MyPlugin.Dojo');
$this->assertEqual($js->helpers, array('Html', 'MyPlugin.DojoEngine'));
}
/**
Expand All @@ -123,16 +123,67 @@ function testConstruction() {
* @return void
**/
function testMethodDispatching() {
$js = new JsHelper(array('TestJs'));
$js->TestJsEngine = new TestJsEngineHelper();
$js =& new JsHelper(array('TestJs'));
$js->TestJsEngine =& new TestJsEngineHelper();
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));

$js->methodOne();

$js->TestEngine = new StdClass();
$js->TestEngine =& new StdClass();
$this->expectError();
$js->someMethodThatSurelyDoesntExist();
}
/**
* Test that method dispatching respects buffer parameters and bufferedMethods Lists.
*
* @return void
**/
function testMethodDispatchWithBuffering() {
$js =& new JsHelper(array('TestJs'));
$js->TestJsEngine = new TestJsEngineHelper();
$js->TestJsEngine->bufferedMethods = array('event', 'sortables');
$js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));

$js->event('click', 'foo');
$result = $js->getBuffer();
$this->assertEqual(count($result), 1);
$this->assertEqual($result[0], 'This is an event call');

$result = $js->event('click', 'foo', array('buffer' => false));
$buffer = $js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call');

$result = $js->event('click', 'foo', false);
$buffer = $js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call');

$js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));

$result = $js->effect('slideIn');
$buffer = $js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'I am not buffered.');

$result = $js->effect('slideIn', true);
$buffer = $js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');

$result = $js->effect('slideIn', array('speed' => 'slow'), true);
$buffer = $js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');

$result = $js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
$buffer = $js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
}
/**
* test that writeScripts generates scripts inline.
*
Expand Down

0 comments on commit f257831

Please sign in to comment.