Skip to content

Commit

Permalink
Removing dispatchMethod from JsHelper, its not really that useful, an…
Browse files Browse the repository at this point in the history
…d is good at hiding bugs in mock tests.

Updating test cases.
  • Loading branch information
markstory committed Aug 11, 2010
1 parent 0e933e8 commit 4672cee
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 97 deletions.
10 changes: 6 additions & 4 deletions cake/libs/view/helpers/js.php
Expand Up @@ -121,9 +121,10 @@ public function __construct(View $View, $settings = array()) {
* @return mixed Depends on the return of the dispatched method, or it could be an instance of the EngineHelper
*/
public function __call($method, $params) {
if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) {
if ($this->{$this->__engineName} && method_exists($this->{$this->__engineName}, $method)) {
$buffer = false;
if (in_array(strtolower($method), $this->{$this->__engineName}->bufferedMethods)) {
$engineHelper = $this->{$this->__engineName};
if (in_array(strtolower($method), $engineHelper->bufferedMethods)) {
$buffer = true;
}
if (count($params) > 0) {
Expand All @@ -137,7 +138,8 @@ public function __call($method, $params) {
unset($params['buffer']);
}
}
$out = $this->{$this->__engineName}->dispatchMethod($method, $params);

$out = call_user_func_array(array(&$engineHelper, $method), $params);
if ($this->bufferScripts && $buffer && is_string($out)) {
$this->buffer($out);
return null;
Expand All @@ -148,7 +150,7 @@ public function __call($method, $params) {
return $out;
}
if (method_exists($this, $method . '_')) {
return $this->dispatchMethod($method . '_', $params);
return call_user_func(array(&$this, $method . '_'), $params);
}
trigger_error(sprintf(__('JsHelper:: Missing Method %s is undefined'), $method), E_USER_WARNING);
}
Expand Down
171 changes: 78 additions & 93 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -93,14 +93,11 @@ function setUp() {
$this->_asset = Configure::read('Asset.timestamp');
Configure::write('Asset.timestamp', false);

$this->Js = new JsHelper('Option');
$this->Js->Html = new HtmlHelper();
$this->Js->Form = new FormHelper();
$this->Js->Form->Html = new HtmlHelper();
$this->Js->OptionEngine = new OptionEngineHelper();
$controller = null;
$this->View = $this->getMock('View', array('addScript'), array(&$controller));
$this->Js = new JsHelper($this->View, 'Option');

$view = $this->getMock('View', array(), array(), '', false);
ClassRegistry::addObject('view', $view);
ClassRegistry::addObject('view', $this->View);
}

/**
Expand All @@ -124,11 +121,9 @@ function _useMock() {
if (!class_exists('TestJsEngineHelper', false)) {
$this->getMock('JsBaseEngineHelper', array(), array(), 'TestJsEngineHelper');
}
$this->Js = new JsHelper(array('TestJs'));
$this->Js->TestJsEngine = new TestJsEngineHelper($this);
$this->Js->Html = new HtmlHelper();
$this->Js->Form = new FormHelper();
$this->Js->Form->Html = new HtmlHelper();
$this->Js = new JsHelper($this->View, array('TestJs'));
$this->Js->TestJsEngine = new TestJsEngineHelper($this->View);
$this->mockObjects[] = $this->Js->TestJsEngine;
}

/**
Expand All @@ -137,16 +132,16 @@ function _useMock() {
* @return void
*/
function testConstruction() {
$js = new JsHelper();
$js = new JsHelper($this->View);
$this->assertEqual($js->helpers, array('Html', 'Form', 'JqueryEngine'));

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

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

$js = new JsHelper('MyPlugin.Dojo');
$js = new JsHelper($this->View, 'MyPlugin.Dojo');
$this->assertEqual($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
}

Expand All @@ -160,9 +155,10 @@ function testMethodDispatching() {

$this->Js->TestJsEngine
->expects($this->once())
->method('dispatchMethod')
->with($this->matchesRegularExpression('/event/i'));
$this->Js->event();
->method('event')
->with('click', 'callback');

$this->Js->event('click', 'callback');

$this->Js->TestJsEngine = new StdClass();
$this->expectError();
Expand All @@ -179,8 +175,7 @@ function testEventDispatchWithBuffering() {

$this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
$this->Js->TestJsEngine->expects($this->exactly(3))
->method('dispatchMethod')
->with($this->equalTo('event'))
->method('event')
->will($this->returnValue('This is an event call'));

$this->Js->event('click', 'foo');
Expand All @@ -207,8 +202,7 @@ function testEventDispatchWithBuffering() {
function testEffectDispatchWithBuffering() {
$this->_useMock();
$this->Js->TestJsEngine->expects($this->exactly(4))
->method('dispatchMethod')
->with($this->equalTo('effect'))
->method('effect')
->will($this->returnValue('I am not buffered.'));

$result = $this->Js->effect('slideIn');
Expand Down Expand Up @@ -261,7 +255,7 @@ function testWriteScriptsNoFile() {
$view = $this->getMock('View', array(), array(), '', false);
ClassRegistry::addObject('view', $view);

$this->Js->TestJsEngine->expects($this->once())
$view->expects($this->once())
->method('addScript')
->with($this->matchesRegularExpression('/one\s\=\s1;\ntwo\s\=\s2;/'));
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
Expand Down Expand Up @@ -295,7 +289,7 @@ function testWriteBufferAndXhr() {
$this->_useMock();
$this->Js->params['isAjax'] = true;
$this->Js->buffer('alert("test");');
$this->Js->TestJsEngine->expects($this->never())->method('dispatchMethod');
$this->Js->TestJsEngine->expects($this->never())->method('domReady');
$result = $this->Js->writeBuffer();
}

Expand Down Expand Up @@ -334,21 +328,17 @@ function testLinkWithMock() {

$options = array('update' => '#content');

$this->Js->TestJsEngine->expects($this->any())
->method('dispatchMethod')
->will($this->returnValue('ajax code'));

$this->Js->TestJsEngine->expects($this->at(0))
->method('dispatchMethod')
->with($this->equalTo('get'));
->method('get');

$this->Js->TestJsEngine->expects($this->at(1))
->method('dispatchMethod')
->with($this->equalTo('request'), $this->equalTo(array('/posts/view/1', $options)));
->method('request')
->with('/posts/view/1', $options)
->will($this->returnValue('--ajax code--'));

$this->Js->TestJsEngine->expects($this->at(2))
->method('dispatchMethod')
->with($this->equalTo('event'), $this->equalTo( array('click', 'ajax code', $options + array('buffer' => null))));
->method('event')
->with('click', '--ajax code--', $options + array('buffer' => null));

$result = $this->Js->link('test link', '/posts/view/1', $options);
$expected = array(
Expand All @@ -374,23 +364,18 @@ function testLinkWithMockAndConfirm() {
'id' => 'custom-id',
'escape' => false
);
$this->Js->TestJsEngine->expects($this->at(0))
->method('confirm')
->with($this->equalTo($options['confirm']));
$this->Js->TestJsEngine->expects($this->once())
->method('confirmReturn')
->with($options['confirm'])
->will($this->returnValue('--confirm script--'));

$this->Js->TestJsEngine->expects($this->at(1))
$this->Js->TestJsEngine->expects($this->once())
->method('request')
->with($this->equalTo('/posts/view/1'));
->with('/posts/view/1');

$code = <<<CODE
var _confirm = confirm("Are you sure?");
if (!_confirm) {
return false;
}
CODE;
$this->Js->TestJsEngine->expects($this->at(1))
$this->Js->TestJsEngine->expects($this->once())
->method('event')
->with($this->equalTo('click'), $this->equalTo($code));
->with('click', '--confirm script--');

$result = $this->Js->link('test link »', '/posts/view/1', $options);
$expected = array(
Expand Down Expand Up @@ -429,13 +414,12 @@ function testLinkWithNoBuffering() {
$this->_useMock();

$this->Js->TestJsEngine->expects($this->at(1))
->method('dispatchMethod')
->with('request', array('/posts/view/1', array('update' => '#content')))
->method('request')
->with('/posts/view/1', array('update' => '#content'))
->will($this->returnValue('ajax code'));

$this->Js->TestJsEngine->expects($this->at(2))
->method('dispatchMethod')
->with('event')
->method('event')
->will($this->returnValue('-event handler-'));

$options = array('update' => '#content', 'buffer' => false);
Expand All @@ -462,13 +446,12 @@ function testLinkWithNoBufferingAndSafe() {
$this->_useMock();

$this->Js->TestJsEngine->expects($this->at(1))
->method('dispatchMethod')
->with('request', array('/posts/view/1', array('update' => '#content')))
->method('request')
->with('/posts/view/1', array('update' => '#content'))
->will($this->returnValue('ajax code'));

$this->Js->TestJsEngine->expects($this->at(2))
->method('dispatchMethod')
->with('event')
->method('event')
->will($this->returnValue('-event handler-'));

$options = array('update' => '#content', 'buffer' => false, 'safe' => false);
Expand Down Expand Up @@ -496,17 +479,14 @@ function testSubmitWithMock() {
$options = array('update' => '#content', 'id' => 'test-submit');

$this->Js->TestJsEngine->expects($this->at(0))
->method('dispatchMethod')
->with('get');
->method('get');

$this->Js->TestJsEngine->expects($this->at(1))
->method('dispatchMethod')
->with('serializeForm')
->method('serializeForm')
->will($this->returnValue('serialize-code'));

$this->Js->TestJsEngine->expects($this->at(2))
->method('dispatchMethod')
->with('request')
->method('request')
->will($this->returnValue('ajax-code'));

$params = array(
Expand All @@ -515,8 +495,8 @@ function testSubmitWithMock() {
);

$this->Js->TestJsEngine->expects($this->at(3))
->method('dispatchMethod')
->with('event', $this->equalTo( array('click', "ajax-code", $params)));
->method('event')
->with('click', "ajax-code", $params);

$result = $this->Js->submit('Save', $options);
$expected = array(
Expand All @@ -525,36 +505,43 @@ function testSubmitWithMock() {
'/div'
);
$this->assertTags($result, $expected);
}

$this->Js->TestJsEngine->expects($this->at(4))
->method('dispatchMethod')
->with('get');
/**
* test submit() with a mock
*
* @return void
*/
function testSubmitWithMockRequestParams() {
$this->_useMock();

$this->Js->TestJsEngine->expects($this->at(5))
->method('dispatchMethod')
->with($this->matchesRegularExpression('/serializeForm/i'));
$this->Js->TestJsEngine->expects($this->at(0))
->method('get');

$this->Js->TestJsEngine->expects($this->at(1))
->method('serializeForm')
->will($this->returnValue('serialize-code'));

$requestParams = array(
'/custom/url', array(
'update' => '#content',
'data' => 'serialize-code',
'method' => 'post',
'dataExpression' => true
)
'update' => '#content',
'data' => 'serialize-code',
'method' => 'post',
'dataExpression' => true
);

$this->Js->TestJsEngine->expects($this->at(6))
->method('dispatchMethod')
->with($this->equalTo('request'), $this->equalTo($requestParams));
$this->Js->TestJsEngine->expects($this->at(2))
->method('request')
->with('/custom/url', $requestParams)
->will($this->returnValue('ajax-code'));

$params = array(
'update' => '#content', 'data' => 'serialize-code',
'method' => 'post', 'dataExpression' => true, 'buffer' => null
);

$this->Js->TestJsEngine->expects($this->at(7))
->method('dispatchMethod')
->with($this->equalTo('event'), $this->equalTo(array('click', "ajax-code", $params)));
$this->Js->TestJsEngine->expects($this->at(3))
->method('event')
->with('click', "ajax-code", $params);

$options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
$result = $this->Js->submit('Save', $options);
Expand All @@ -576,22 +563,18 @@ function testSubmitWithNoBuffer() {
$options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false);

$this->Js->TestJsEngine->expects($this->at(0))
->method('dispatchMethod')
->with($this->equalTo('get'));
->method('get');

$this->Js->TestJsEngine->expects($this->at(1))
->method('dispatchMethod')
->with('serializeForm')
->method('serializeForm')
->will($this->returnValue('serialize-code'));

$this->Js->TestJsEngine->expects($this->at(2))
->method('dispatchMethod')
->with('request')
->method('request')
->will($this->returnValue('ajax-code'));

$this->Js->TestJsEngine->expects($this->at(3))
->method('dispatchMethod')
->with('event')
->method('event')
->will($this->returnValue('event-handler'));

$params = array(
Expand All @@ -600,8 +583,8 @@ function testSubmitWithNoBuffer() {
);

$this->Js->TestJsEngine->expects($this->at(3))
->method('dispatchMethod')
->with($this->equalTo('event'), $this->equalTo(array('click', "ajax-code", $params)));
->method('event')
->with('click', "ajax-code", $params);

$result = $this->Js->submit('Save', $options);
$expected = array(
Expand Down Expand Up @@ -696,7 +679,9 @@ class JsBaseEngineTest extends CakeTestCase {
* @return void
*/
function startTest() {
$this->JsEngine = new OptionEngineHelper();
$controller = null;
$this->View = new View($controller);
$this->JsEngine = new OptionEngineHelper($this->View);
}
/**
* endTest method
Expand Down

0 comments on commit 4672cee

Please sign in to comment.