[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/web_app/tests/cases/plain/command/ -> lmbFormCommandTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/web_app/src/command/lmbFormCommand.class.php');
  10  lmb_require('limb/validation/src/lmbValidator.class.php');
  11  lmb_require('limb/validation/src/lmbErrorList.class.php');
  12  
  13  Mock :: generate('lmbValidator', 'MockValidator');
  14  Mock :: generate('lmbErrorList', 'MockErrorList');
  15  
  16  class lmbFormStubDelegate
  17  {
  18    var $calls_order = '';
  19    var $halt_on_before = false;
  20  
  21    function haltOnBefore()
  22    {
  23      $this->halt_on_before = true;
  24    }
  25  
  26    function onBefore($form)
  27    {
  28      if($form instanceof lmbFormCommand)
  29        $this->calls_order .= '|on_before|';
  30  
  31      if($this->halt_on_before)
  32        $form->halt();
  33    }
  34  
  35    function onAfter($form)
  36    {
  37      if($form instanceof lmbFormCommand)
  38        $this->calls_order .= '|on_after|';
  39    }
  40  
  41    function onShow($form)
  42    {
  43      if($form instanceof lmbFormCommand)
  44        $this->calls_order .= '|on_show|';
  45    }
  46  
  47    function onBeforeValidate($form)
  48    {
  49      if($form instanceof lmbFormCommand)
  50        $this->calls_order .= '|on_before_validate|';
  51    }
  52  
  53    function onAfterValidate($form)
  54    {
  55      if($form instanceof lmbFormCommand)
  56        $this->calls_order .= '|on_after_validate|';
  57    }
  58  
  59    function onValid($form)
  60    {
  61      if($form instanceof lmbFormCommand)
  62        $this->calls_order .= '|on_valid|';
  63    }
  64  
  65    function onError($form)
  66    {
  67      if($form instanceof lmbFormCommand)
  68        $this->calls_order .= '|on_error|';
  69    }
  70  }
  71  
  72  class lmbFormStubChild extends lmbFormCommand
  73  {
  74    var $calls_order = '';
  75  
  76    function _onBefore()
  77    {
  78      $this->calls_order .= '|on_before|';
  79    }
  80  
  81    function _onAfter()
  82    {
  83      $this->calls_order .= '|on_after|';
  84    }
  85  
  86    function _onShow()
  87    {
  88      $this->calls_order .= '|on_show|';
  89    }
  90  
  91    function _onBeforeValidate()
  92    {
  93      $this->calls_order .= '|on_before_validate|';
  94    }
  95  
  96    function _onAfterValidate()
  97    {
  98      $this->calls_order .= '|on_after_validate|';
  99    }
 100  
 101    function _onValid()
 102    {
 103      $this->calls_order .= '|on_valid|';
 104    }
 105  
 106    function _onError()
 107    {
 108      $this->calls_order .= '|on_error|';
 109    }
 110  }
 111  
 112  class lmbFormCommandTest extends UnitTestCase
 113  {
 114    var $toolkit;
 115    var $request;
 116  
 117    function setUp()
 118    {
 119      $this->toolkit = lmbToolkit :: save();
 120      $this->request = $this->toolkit->getRequest();
 121    }
 122  
 123    function tearDown()
 124    {
 125      lmbToolkit :: restore();
 126    }
 127  
 128    function testSetViewFormDatasource()
 129    {
 130      $validator = new MockValidator();
 131      $command = new lmbFormCommand('some_template', 'form_id', $validator);
 132      $ds = new lmbSet('whatever');
 133      $command->setFormDatasource($ds);
 134  
 135      $this->assertReference($this->toolkit->getView()->getFormDatasource('form_id'), $ds);
 136    }
 137  
 138    function testCallbacksForNotSubmitted()
 139    {
 140      $command = new lmbFormCommand('some_template', 'form_id');
 141      $delegate = $this->_createFormStubDelegate($command);
 142      $command->perform();
 143      $this->assertEqual($delegate->calls_order, '|on_before||on_show||on_after|');
 144      $this->assertFalse($command->isSubmitted());
 145      $this->assertTrue($command->isValid());
 146    }
 147  
 148    function testOwnMethodsForNotSubmitted()
 149    {
 150      $command = new lmbFormStubChild('some_template', 'form_id');
 151      $command->perform();
 152      $this->assertEqual($command->calls_order, '|on_before||on_show||on_after|');
 153    }
 154  
 155    function testDelegateForSubmittedAndNotValid()
 156    {
 157      $validator = new MockValidator();
 158      $error_list = new MockErrorList();
 159  
 160      $this->toolkit->setRequest($request = new lmbHttpRequest(null, $get = null, $post = array('submitted' => 1)));
 161  
 162      $command = new lmbFormCommand('some_template', 'form_id', $validator);
 163      $command->setErrorList($error_list);
 164  
 165      $validator->expectOnce('validate', array($request));
 166      $validator->expectOnce('setErrorList', array($error_list));
 167      $error_list->setReturnValue('isValid', false);
 168  
 169      $delegate = $this->_createFormStubDelegate($command);
 170      $command->perform();
 171  
 172      $this->assertTrue($command->isSubmitted());
 173      $this->assertFalse($command->isValid());
 174      $this->assertEqual($delegate->calls_order, '|on_before||on_before_validate||on_error||on_after_validate||on_after|');
 175    }
 176  
 177    function testOwnMethodsForSubmittedAndNotValid()
 178    {
 179      $validator = new MockValidator();
 180      $error_list = new MockErrorList();
 181  
 182      $this->toolkit->setRequest($request = new lmbHttpRequest(null, $get = null, $post = array('submitted' => 1)));
 183  
 184      $command = new lmbFormStubChild('some_template', 'form_id', $validator);
 185      $command->setErrorList($error_list);
 186  
 187      $validator->expectOnce('validate', array($request));
 188      $validator->expectOnce('setErrorList', array($error_list));
 189      $error_list->setReturnValue('isValid', false);
 190  
 191      $command->perform();
 192  
 193      $this->assertEqual($command->calls_order, '|on_before||on_before_validate||on_error||on_after_validate||on_after|');
 194    }
 195  
 196    function testDelegateForSubmittedAndValid()
 197    {
 198      $validator = new MockValidator();
 199      $error_list = new MockErrorList();
 200  
 201      $this->toolkit->setRequest($request = new lmbHttpRequest(null, $get = null, $post = array('submitted' => 1)));
 202  
 203      $command = new lmbFormCommand('some_template', 'form_id', $validator);
 204      $command->setErrorList($error_list);
 205  
 206      $validator->expectOnce('validate', array($request));
 207      $validator->expectOnce('setErrorList', array($error_list));
 208      $error_list->setReturnValue('isValid', true);
 209  
 210      $delegate = $this->_createFormStubDelegate($command);
 211  
 212      $command->perform();
 213  
 214      $this->assertTrue($command->isSubmitted());
 215      $this->assertTrue($command->isValid());
 216      $this->assertEqual($delegate->calls_order, '|on_before||on_before_validate||on_valid||on_after_validate||on_after|');
 217    }
 218  
 219    function testOwnMethodsForSubmittedAndValid()
 220    {
 221      $validator = new MockValidator();
 222      $error_list = new MockErrorList();
 223  
 224      $this->toolkit->setRequest($request = new lmbHttpRequest(null, $get = null, $post = array('submitted' => 1)));
 225  
 226      $command = new lmbFormStubChild('some_template', 'form_id', $validator);
 227      $command->setErrorList($error_list);
 228  
 229      $validator->expectOnce('validate', array($request));
 230      $validator->expectOnce('setErrorList', array($error_list));
 231      $error_list->setReturnValue('isValid', true);
 232  
 233      $command->perform();
 234  
 235      $this->assertTrue($command->isSubmitted());
 236      $this->assertTrue($command->isValid());
 237      $this->assertEqual($command->calls_order, '|on_before||on_before_validate||on_valid||on_after_validate||on_after|');
 238    }
 239  
 240    function testHaltInvokeChain()
 241    {
 242      $command = new lmbFormCommand('some_template', 'form_id');
 243  
 244      $delegate = $this->_createFormStubDelegate($command);
 245      $request = lmbToolkit :: instance()->getRequest();
 246      $request->set('submitted', 1);
 247  
 248      $delegate->haltOnBefore();
 249  
 250      $command->perform();
 251  
 252      $this->assertEqual($delegate->calls_order, '|on_before|');
 253    }
 254  
 255    protected function _createFormStubDelegate($command)
 256    {
 257      $delegate = new lmbFormStubDelegate();
 258      $command->registerOnBeforeCallback($delegate, 'onBefore');
 259      $command->registerOnShowCallback($delegate, 'onShow');
 260      $command->registerOnAfterCallback($delegate, 'onAfter');
 261      $command->registerOnBeforeValidateCallback($delegate, 'onBeforeValidate');
 262      $command->registerOnAfterValidateCallback($delegate, 'onAfterValidate');
 263      $command->registerOnValidCallback($delegate, 'onValid');
 264      $command->registerOnErrorCallback($delegate, 'onError');
 265      return $delegate;
 266    }
 267  }
 268  
 269  ?>


Generated: Mon Sep 8 04:35:41 2008 Cross-referenced by PHPXref 0.7