Permalink
Browse files

Refactoring how buffering works.

Adding buffer params to method calls.
Adding tests for buffer calls.
  • Loading branch information...
1 parent 57ceba5 commit f2578315e749143cc6589f80b73a4ba4c0406d36 @markstory markstory committed Mar 29, 2009
Showing with 78 additions and 12 deletions.
  1. +20 −5 cake/libs/view/helpers/js.php
  2. +58 −7 cake/tests/cases/libs/view/helpers/js.test.php
@@ -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;
@@ -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();
/**
@@ -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'));
}
/**
@@ -123,17 +123,68 @@ 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.
*
* @return void

0 comments on commit f257831

Please sign in to comment.