[limb-svn] r6347 - in 3.x/trunk/limb/web_app: src/controller src/filter src/toolkit tests/cases/plain/controller

svn at limb-project.com svn at limb-project.com
Mon Oct 1 17:17:18 MSD 2007


Author: pachanga
Date: 2007-10-01 17:17:18 +0400 (Mon, 01 Oct 2007)
New Revision: 6347
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6347

Modified:
   3.x/trunk/limb/web_app/src/controller/lmbAbstractController.class.php
   3.x/trunk/limb/web_app/src/controller/lmbController.class.php
   3.x/trunk/limb/web_app/src/filter/lmbViewRenderingFilter.class.php
   3.x/trunk/limb/web_app/src/toolkit/lmbWebAppTools.class.php
   3.x/trunk/limb/web_app/tests/cases/plain/controller/lmbControllerTest.class.php
Log:
-- lmbAbstractController :: _findTemplateForAction() refactored and unified, now it's not hardcoded to use WACT templating engine. Apart from that mapping action to template cache added, this cache is used if LIMB_CONTROLLER_CACHE_ENABLED is true
-- lmbController :: setTemplate($tpl) substitutes the internal $view attribute with more appropriate one
-- etc minor improvements


Modified: 3.x/trunk/limb/web_app/src/controller/lmbAbstractController.class.php
===================================================================
--- 3.x/trunk/limb/web_app/src/controller/lmbAbstractController.class.php	2007-10-01 09:31:24 UTC (rev 6346)
+++ 3.x/trunk/limb/web_app/src/controller/lmbAbstractController.class.php	2007-10-01 13:17:18 UTC (rev 6347)
@@ -9,6 +9,7 @@
 
 lmb_require('limb/core/src/lmbClassPath.class.php');
 lmb_require('limb/core/src/lmbMixable.class.php');
+lmb_require('limb/fs/src/lmbFs.class.php');
 
 /**
  * Base class for all controllers
@@ -38,6 +39,18 @@
    * @var object lmbMixable instance
    */
   protected $mixed;
+  /**
+   * @var object lmbToolkit instance
+   */
+  protected $toolkit;
+  /**
+   * @var array a action to template cached map
+   */
+  protected $action_template_map = array();
+  /**
+   * @var boolean
+   */
+  protected $map_changed = false;
 
   /**
    *  Constructor.
@@ -45,6 +58,8 @@
    */
   function __construct()
   {
+    $this->toolkit = lmbToolkit :: instance();
+
     if(!$this->name)
      $this->name = $this->_guessName();
 
@@ -52,8 +67,35 @@
     $this->mixed->setOwner($this);
     foreach($this->mixins as $mixin)
       $this->mixed->mixin($mixin);
+
+    $this->_loadCache();
   }
 
+  function __destruct()
+  {
+    $this->_saveCache();
+  }
+
+  function isCacheEnabled()
+  {
+    return (defined('LIMB_CONTROLLER_CACHE_ENABLED') && constant('LIMB_CONTROLLER_CACHE_ENABLED'));
+  }
+
+  function _loadCache()
+  {
+    if($this->isCacheEnabled() && file_exists($cache = LIMB_VAR_DIR . '/locators/controller_action2tpl.cache'))
+      $this->action_template_map = unserialize(file_get_contents($cache));
+  }
+
+  function _saveCache()
+  {
+    if($this->map_changed && $this->isCacheEnabled())
+    {
+      lmbFs :: safeWrite(LIMB_VAR_DIR . '/locators/controller_action2tpl.cache', 
+                         serialize($this->action_template_map));
+    }
+  }
+
   /**
    * Using this hacky method mixins can access controller variables
    * @param string variable name
@@ -96,17 +138,26 @@
   }
 
   abstract function performAction();
+
   abstract function actionExists($action);
 
   protected function _findTemplateForAction($action)
   {
-    $template_path = $this->getName() . '/' . $action . '.html';
+    if(isset($this->action_template_map[$this->name]) && isset($this->action_template_map[$this->name][$action]))
+      return $this->action_template_map[$this->name][$action];
 
-    $wact_locator = lmbToolkit :: instance()->getWactLocator();
+    $template_format = $this->getName() . '/' . $action . '%s';
 
-    if($wact_locator->locateSourceTemplate($template_path))
-      return $template_path;
-    return null;
+    foreach($this->toolkit->getSupportedViewExtensions() as $ext)
+    {
+      if($template_path = $this->toolkit->locateTemplateByAlias(sprintf($template_format, $ext)))
+      {
+        $this->map_changed = true;
+        $this->action_template_map[$this->name][$action] = $template_path;
+        return $template_path;
+      }
+    }
+    $this->action_template_map[$this->name][$action] = false;
   }
 
   static function performCommand()

Modified: 3.x/trunk/limb/web_app/src/controller/lmbController.class.php
===================================================================
--- 3.x/trunk/limb/web_app/src/controller/lmbController.class.php	2007-10-01 09:31:24 UTC (rev 6346)
+++ 3.x/trunk/limb/web_app/src/controller/lmbController.class.php	2007-10-01 13:17:18 UTC (rev 6347)
@@ -9,6 +9,7 @@
 lmb_require('limb/web_app/src/controller/lmbAbstractController.class.php');
 lmb_require('limb/validation/src/lmbErrorList.class.php');
 lmb_require('limb/validation/src/lmbValidator.class.php');
+lmb_require('limb/view/src/lmbDummyView.class.php');
 
 /**
  * class lmbController.
@@ -18,7 +19,6 @@
  */
 class lmbController extends lmbAbstractController
 {
-  protected $toolkit;
   protected $request;
   protected $response;
   protected $session;
@@ -32,15 +32,19 @@
   {
     parent :: __construct();
 
-    $this->toolkit = lmbToolkit :: instance();
     $this->request = $this->toolkit->getRequest();
     $this->response = $this->toolkit->getResponse();
     $this->session = $this->toolkit->getSession();
-    $this->view = $this->toolkit->getView();
+    $this->view = $this->toolkit->getView();//this is a dummy view, which will be replaced with a concrete one
     $this->error_list = new lmbErrorList();
     $this->validator = new lmbValidator($this->error_list);
   }
 
+  function getView()
+  {
+    return $this->view;
+  }
+
   function validate($dataspace)
   {
     $this->validator->validate($dataspace);
@@ -63,7 +67,9 @@
     if(method_exists($this, $this->_mapCurrentActionToMethod()))
     {
       if($template_path = $this->_findTemplateForAction($this->current_action))
+      {
         $this->setTemplate($template_path);
+      }
 
       $method = $this->_mapCurrentActionToMethod($this->_mapCurrentActionToMethod());
       $res = $this->$method();
@@ -72,8 +78,8 @@
 
       if(is_string($res))
         $this->response->write($res);
-      elseif($this->response->isEmpty() && !$this->view->getTemplate())
-        $this->response->write('Default empty output for controller "' .
+      elseif($this->response->isEmpty() && is_a($this->view, 'lmbDummyView'))
+        $this->response->write('Default dummy output for controller "' .
                                get_class($this) . '" action "' . $this->current_action . '"');
 
       return $res;
@@ -98,7 +104,12 @@
 
   function setTemplate($template_path)
   {
-    $this->view->setTemplate($template_path);
+    $view = $this->toolkit->createViewByTemplate($template_path);
+    //copying stuff from dummy view, do we need this?
+    $view->copy($this->view);
+    $view->setTemplate($template_path);
+    $this->view = $view;
+    $this->toolkit->setView($view);
   }
 
   protected function _passLocalAttributesToView()

Modified: 3.x/trunk/limb/web_app/src/filter/lmbViewRenderingFilter.class.php
===================================================================
--- 3.x/trunk/limb/web_app/src/filter/lmbViewRenderingFilter.class.php	2007-10-01 09:31:24 UTC (rev 6346)
+++ 3.x/trunk/limb/web_app/src/filter/lmbViewRenderingFilter.class.php	2007-10-01 13:17:18 UTC (rev 6347)
@@ -27,9 +27,8 @@
       return;
     }
 
-    if(is_object($toolkit->getView()))
+    if(is_object($view = $toolkit->getView()))
     {
-      $view = $toolkit->getView();
       $view->set('request', $toolkit->getRequest());
       $view->set('session', $toolkit->getSession());
       $view->set('toolkit', $toolkit);

Modified: 3.x/trunk/limb/web_app/src/toolkit/lmbWebAppTools.class.php
===================================================================
--- 3.x/trunk/limb/web_app/src/toolkit/lmbWebAppTools.class.php	2007-10-01 09:31:24 UTC (rev 6346)
+++ 3.x/trunk/limb/web_app/src/toolkit/lmbWebAppTools.class.php	2007-10-01 13:17:18 UTC (rev 6347)
@@ -1,147 +1,147 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- */
-lmb_require('limb/toolkit/src/lmbAbstractTools.class.php');
-
- at define('LIMB_CONTROLLERS_INCLUDE_PATH', 'src/controller;limb/*/src/controller');
-
-/**
- * class lmbWebAppTools.
- *
- * @package web_app
- * @version $Id$
- */
-class lmbWebAppTools extends lmbAbstractTools
-{
-  protected $session;
-  protected $view;
-  protected $dispatched_controller;
-  protected $routes;
-  protected $flash_box;
-
-  function getSession()
-  {
-    if(is_object($this->session))
-      return $this->session;
-
-    lmb_require('limb/session/src/lmbSession.class.php');
-    $this->session = new lmbSession();
-
-    return $this->session;
-  }
-
-  function setSession($session)
-  {
-    $this->session = $session;
-  }
-
-  function setView($view)
-  {
-    $this->view = $view;
-  }
-
-  function getView()
-  {
-    if(is_object($this->view))
-      return $this->view;
-
-    lmb_require('limb/view/src/lmbWactView.class.php');
-    $this->view = new lmbWactView();
-    $this->view->setCacheDir(LIMB_VAR_DIR . '/compiled/');
-
-    return $this->view;
-  }
-
-  function renderView($template)
-  {
-    $view = $this->toolkit->getView();
-    $response = $this->toolkit->getResponse();
-    $view->setTemplate($template);
-    $response->write($view->render());
-  }
-
-  function setDispatchedController($dispatched)
-  {
-    $this->dispatched_controller = $dispatched;
-  }
-
-  function getDispatchedController()
-  {
-    return $this->dispatched_controller;
-  }
-
-  function getRoutesUrl($params = array(), $route_name = '', $skip_controller = false)
-  {
-    $routes = $this->toolkit->getRoutes();
-    if(!isset($params['controller']) && !$skip_controller)
-      $params['controller'] = $this->toolkit->getDispatchedController()->getName();
-
-    return LIMB_HTTP_GATEWAY_PATH . ltrim($routes->toUrl($params, $route_name), '/');
-  }
-
-  function getRoutes()
-  {
-    if(!$this->routes)
-    {
-      $config = $this->toolkit->getConf('routes');
-
-      lmb_require('limb/web_app/src/request/lmbRoutes.class.php');
-      $this->routes = new lmbRoutes($config->export());
-    }
-
-    return $this->routes;
-  }
-
-  function setRoutes($routes)
-  {
-    $this->routes = $routes;
-  }
-
-  function getFlashBox()
-  {
-    if(!is_object($this->flash_box))
-    {
-      lmb_require('limb/web_app/src/util/lmbFlashBox.class.php');
-      $this->flash_box = lmbFlashBox :: create($this->toolkit->getSession());
-    }
-
-    return $this->flash_box;
-  }
-
-  function flashError($message)
-  {
-    $this->toolkit->getFlashBox()->addError($message);
-  }
-
-  function flashMessage($message)
-  {
-    $this->toolkit->getFlashBox()->addMessage($message);
-  }
-
-  function createController($controller_name)
-  {
-    $class_name = lmb_camel_case($controller_name) . 'Controller';
-    if(!class_exists($class_name))
-    {
-      $file = $this->toolkit->findFileByAlias("$class_name.class.php", LIMB_CONTROLLERS_INCLUDE_PATH, 'controller');
-      lmb_require($file);
-    }
-    return new $class_name;
-  }
-
-  function redirect($params_or_url = array(), $route_url = null, $append = '')
-  {
-    $toolkit = $this->toolkit;
-
-    if(is_array($params_or_url))
-      $toolkit->getResponse()->redirect($toolkit->getRoutesUrl($params_or_url, $route_url) . $append);
-    else
-      $toolkit->getResponse()->redirect($params_or_url . $append);
-  }
-}
-
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/toolkit/src/lmbAbstractTools.class.php');
+
+ at define('LIMB_CONTROLLERS_INCLUDE_PATH', 'src/controller;limb/*/src/controller');
+
+/**
+ * class lmbWebAppTools.
+ *
+ * @package web_app
+ * @version $Id$
+ */
+class lmbWebAppTools extends lmbAbstractTools
+{
+  protected $session;
+  protected $view;
+  protected $dispatched_controller;
+  protected $routes;
+  protected $flash_box;
+
+  function getSession()
+  {
+    if(is_object($this->session))
+      return $this->session;
+
+    lmb_require('limb/session/src/lmbSession.class.php');
+    $this->session = new lmbSession();
+
+    return $this->session;
+  }
+
+  function setSession($session)
+  {
+    $this->session = $session;
+  }
+
+  function setView($view)
+  {
+    $this->view = $view;
+  }
+
+  function getView()
+  {
+    if(is_object($this->view))
+      return $this->view;
+
+    //default view
+    lmb_require('limb/view/src/lmbDummyView.class.php');
+    $this->view = new lmbDummyView();
+
+    return $this->view;
+  }
+
+  function renderView($template)
+  {
+    $view = $this->toolkit->getView();
+    $response = $this->toolkit->getResponse();
+    $view->setTemplate($template);
+    $response->write($view->render());
+  }
+
+  function setDispatchedController($dispatched)
+  {
+    $this->dispatched_controller = $dispatched;
+  }
+
+  function getDispatchedController()
+  {
+    return $this->dispatched_controller;
+  }
+
+  function getRoutesUrl($params = array(), $route_name = '', $skip_controller = false)
+  {
+    $routes = $this->toolkit->getRoutes();
+    if(!isset($params['controller']) && !$skip_controller)
+      $params['controller'] = $this->toolkit->getDispatchedController()->getName();
+
+    return LIMB_HTTP_GATEWAY_PATH . ltrim($routes->toUrl($params, $route_name), '/');
+  }
+
+  function getRoutes()
+  {
+    if(!$this->routes)
+    {
+      $config = $this->toolkit->getConf('routes');
+
+      lmb_require('limb/web_app/src/request/lmbRoutes.class.php');
+      $this->routes = new lmbRoutes($config->export());
+    }
+
+    return $this->routes;
+  }
+
+  function setRoutes($routes)
+  {
+    $this->routes = $routes;
+  }
+
+  function getFlashBox()
+  {
+    if(!is_object($this->flash_box))
+    {
+      lmb_require('limb/web_app/src/util/lmbFlashBox.class.php');
+      $this->flash_box = lmbFlashBox :: create($this->toolkit->getSession());
+    }
+
+    return $this->flash_box;
+  }
+
+  function flashError($message)
+  {
+    $this->toolkit->getFlashBox()->addError($message);
+  }
+
+  function flashMessage($message)
+  {
+    $this->toolkit->getFlashBox()->addMessage($message);
+  }
+
+  function createController($controller_name)
+  {
+    $class_name = lmb_camel_case($controller_name) . 'Controller';
+    if(!class_exists($class_name))
+    {
+      $file = $this->toolkit->findFileByAlias("$class_name.class.php", LIMB_CONTROLLERS_INCLUDE_PATH, 'controller');
+      lmb_require($file);
+    }
+    return new $class_name;
+  }
+
+  function redirect($params_or_url = array(), $route_url = null, $append = '')
+  {
+    $toolkit = $this->toolkit;
+
+    if(is_array($params_or_url))
+      $toolkit->getResponse()->redirect($toolkit->getRoutesUrl($params_or_url, $route_url) . $append);
+    else
+      $toolkit->getResponse()->redirect($params_or_url . $append);
+  }
+}
+

Modified: 3.x/trunk/limb/web_app/tests/cases/plain/controller/lmbControllerTest.class.php
===================================================================
--- 3.x/trunk/limb/web_app/tests/cases/plain/controller/lmbControllerTest.class.php	2007-10-01 09:31:24 UTC (rev 6346)
+++ 3.x/trunk/limb/web_app/tests/cases/plain/controller/lmbControllerTest.class.php	2007-10-01 13:17:18 UTC (rev 6347)
@@ -1,221 +1,209 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- */
-lmb_require('limb/web_app/src/controller/lmbController.class.php');
-lmb_require('limb/view/src/wact/lmbWactTemplateLocator.class.php');
-lmb_require('limb/validation/src/rule/lmbValidationRule.interface.php');
-
-Mock :: generate('lmbWactTemplateLocator', 'MockWactTemplateLocator');
-Mock :: generate('lmbValidationRule', 'MockValidationRule');
-
-class TestingController extends lmbController
-{
-  protected $name = 'foo';
-  public $display_performed = false;
-  public $template_name;
-
-  function doDisplay()
-  {
-    $this->display_performed = true;
-    $this->template_name = $this->view->getTemplate();
-  }
-
-  function doWrite()
-  {
-    return "Hi!";
-  }
-
-  function doSetVars()
-  {
-    $this->item = 'item';
-  }
-
-  function doPopup()
-  {
-    $this->closePopup();
-  }
-
-  function doWithoutPopup()
-  {
-    $this->in_popup = false;
-    $this->closePopup();
-  }
-
-  function addValidatorRule($r)
-  {
-    $this->validator->addRule($r);
-  }
-
-  function getErrorList()
-  {
-    return $this->error_list;
-  }
-
-  function set($name, $value)
-  {
-    $this->$name = $value;
-  }
-}
-
-class SecondTestingController extends lmbController {}
-
-class lmbControllerTest extends UnitTestCase
-{
-  protected $toolkit;
-
-  function setUp()
-  {
-    $this->toolkit = lmbToolkit :: save();
-  }
-
-  function tearDown()
-  {
-    lmbToolkit :: restore();
-  }
-
-  function testActionExists()
-  {
-    $controller = new TestingController();
-    $this->assertTrue($controller->actionExists('display'));
-    $this->assertFalse($controller->actionExists('no_such_action'));
-  }
-
-  function testGuessControllerName()
-  {
-    $controller = new SecondTestingController();
-    $this->assertEqual($controller->getName(), 'second_testing');
-  }
-
-  function testPerformCommand()
-  {
-    $controller = new TestingController();
-    $controller->setCurrentAction('display');
-    $controller->performAction();
-    $this->assertTrue($controller->display_performed);
-  }
-
-  function testPerformedActionStringResultIsWrittenToResponse()
-  {
-    $controller = new TestingController();
-    $controller->setCurrentAction('write');
-    $controller->performAction();
-    $this->assertEqual($this->toolkit->getResponse()->getResponseString(), "Hi!");
-  }
-
-  function testSetTemplateOnlyIfMethodIsNotFound()
-  {
-    $mock_locator = new MockWactTemplateLocator();
-    $mock_locator->expectOnce('locateSourceTemplate', array('foo/detail.html'));
-    $mock_locator->setReturnValue('locateSourceTemplate', true, array('foo/detail.html'));
-    $this->toolkit->setWactLocator($mock_locator);
-
-    $controller = new TestingController();
-    $controller->setCurrentAction('detail');
-
-    $controller->performAction();
-    $this->assertTrue($this->toolkit->getView()->getTemplate(), 'testing/detail.html');
-  }
-
-  function testControllerAttributesAutomaticallyPassedToView()
-  {
-    $mock_locator = new MockWactTemplateLocator();
-    $mock_locator->expectOnce('locateSourceTemplate', array('foo/set_vars.html'));
-    $mock_locator->setReturnValue('locateSourceTemplate', true, array('foo/set_vars.html'));
-    $this->toolkit->setWactLocator($mock_locator);
-
-    $controller = new TestingController();
-    $controller->set('foo', 'FOO');
-    $controller->set('bar', 'BAR');
-    $controller->set('_nope', 'NO');
-    $controller->setCurrentAction('set_vars');
-
-    $controller->performAction();
-    $view = $this->toolkit->getView();
-    $this->assertEqual($view->get('item'), 'item');//this one is set in action
-    $this->assertEqual($view->get('foo'), 'FOO');
-    $this->assertEqual($view->get('bar'), 'BAR');
-    $this->assertNull($view->get('_nope'));//this one is ignored, since it's "protected" with _
-  }
-
-  function testActionExistsReturnsTrueIsTemplateFound()
-  {
-    $mock_locator = new MockWactTemplateLocator();
-    $mock_locator->expectOnce('locateSourceTemplate', array('foo/detail.html'));
-    $mock_locator->setReturnValue('locateSourceTemplate', true, array('foo/detail.html'));
-    $this->toolkit->setWactLocator($mock_locator);
-
-    $controller = new TestingController();
-    $this->assertTrue($controller->actionExists('detail'));
-  }
-
-  function setTemplateIfItExistsBeforePerformingAction()
-  {
-    $mock_locator = new MockWactTemplateLocator();
-    $mock_locator->expectOnce('locateSourceTemplate', array('foo/detail.html'));
-    $mock_locator->setReturnValue('locateSourceTemplate', true, array('foo/detail.html'));
-    $this->toolkit->setWactLocator($mock_locator);
-
-    $controller = new TestingController();
-    $controller->setCurrentAction('detail');
-
-    $controller->performAction();
-    $this->assertTrue($controller->template_name, 'testing/detail.html');
-  }
-
-  function testValidateOk()
-  {
-    $controller = new TestingController();
-    $error_list = $controller->getErrorList();
-
-    $ds = new lmbSet();
-
-    $r1 = new MockValidationRule();
-    $r1->expectOnce('validate', array($ds, $error_list));
-
-    $r2 = new MockValidationRule();
-    $r2->expectOnce('validate', array($ds, $error_list));
-
-    $controller->addValidatorRule($r1);
-    $controller->addValidatorRule($r2);
-
-    $this->assertTrue($controller->validate($ds));
-  }
-
-  function testValidateFailed()
-  {
-    $controller = new TestingController();
-    $error_list = $controller->getErrorList();
-    $error_list->addError('blah!');
-
-    $this->assertFalse($controller->validate(new lmbSet()));
-  }
-
-  function testForward()
-  {
-    $controller = new lmbController();
-    $this->assertEqual($controller->forward('testing', 'write'), "Hi!");
-  }
-
-  function testClosePopup()
-  {
-    $controller = new TestingController();
-    $controller->setCurrentAction('popup');
-    $controller->performAction();
-    $this->assertPattern('~^<html><script>~', $this->toolkit->getResponse()->getResponseString());
-  }
-
-  function testDoNotClosePopup()
-  {
-    $controller = new TestingController();
-    $controller->setCurrentAction('without_popup');
-    $controller->performAction();
-    $this->assertPattern('~^Default empty output~', $this->toolkit->getResponse()->getResponseString());
-  }
-}
-
-
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/web_app/src/controller/lmbController.class.php');
+lmb_require('limb/view/src/wact/lmbWactTemplateLocator.class.php');
+lmb_require('limb/validation/src/rule/lmbValidationRule.interface.php');
+
+Mock :: generate('lmbWactTemplateLocator', 'MockWactTemplateLocator');
+Mock :: generate('lmbValidationRule', 'MockValidationRule');
+
+class TestingController extends lmbController
+{
+  protected $name = 'foo';
+  public $display_performed = false;
+  public $template_name;
+
+  function doDisplay()
+  {
+    $this->display_performed = true;
+    $this->template_name = $this->view->getTemplate();
+  }
+
+  function doWrite()
+  {
+    return "Hi!";
+  }
+
+  function doSetVars()
+  {
+    $this->item = 'item';
+  }
+
+  function doPopup()
+  {
+    $this->closePopup();
+  }
+
+  function doWithoutPopup()
+  {
+    $this->in_popup = false;
+    $this->closePopup();
+  }
+
+  function addValidatorRule($r)
+  {
+    $this->validator->addRule($r);
+  }
+
+  function getErrorList()
+  {
+    return $this->error_list;
+  }
+
+  function set($name, $value)
+  {
+    $this->$name = $value;
+  }
+}
+
+class SecondTestingController extends lmbController {}
+
+class lmbControllerTest extends UnitTestCase
+{
+  protected $toolkit;
+
+  function setUp()
+  {
+    $this->toolkit = lmbToolkit :: save();
+  }
+
+  function tearDown()
+  {
+    lmbToolkit :: restore();
+  }
+
+  function testActionExists()
+  {
+    $controller = new TestingController();
+    $this->assertTrue($controller->actionExists('display'));
+    $this->assertFalse($controller->actionExists('no_such_action'));
+  }
+
+  function testGuessControllerName()
+  {
+    $controller = new SecondTestingController();
+    $this->assertEqual($controller->getName(), 'second_testing');
+  }
+
+  function testPerformCommand()
+  {
+    $controller = new TestingController();
+    $controller->setCurrentAction('display');
+    $controller->performAction();
+    $this->assertTrue($controller->display_performed);
+  }
+
+  function testPerformedActionStringResultIsWrittenToResponse()
+  {
+    $controller = new TestingController();
+    $controller->setCurrentAction('write');
+    $controller->performAction();
+    $this->assertEqual($this->toolkit->getResponse()->getResponseString(), "Hi!");
+  }
+
+  function testSetTemplateOnlyIfMethodIsNotFound()
+  {
+    $this->toolkit->setSupportedViewTypes(array('.html' => 'lmbDummyView'));
+
+    $controller = new TestingController();
+    $controller->setCurrentAction('detail');
+
+    $controller->performAction();
+    $this->assertTrue($this->toolkit->getView()->getTemplate(), 'testing/detail.html');
+  }
+
+  function testControllerAttributesAutomaticallyPassedToView()
+  {
+    $this->toolkit->setSupportedViewTypes(array('.html' => 'lmbDummyView'));
+
+    $controller = new TestingController();
+    $controller->set('foo', 'FOO');
+    $controller->set('bar', 'BAR');
+    $controller->set('_nope', 'NO');
+    $controller->setCurrentAction('set_vars');
+
+    $controller->performAction();
+    $view = $this->toolkit->getView();
+    $this->assertEqual($view->get('item'), 'item');//this one is set in action
+    $this->assertEqual($view->get('foo'), 'FOO');
+    $this->assertEqual($view->get('bar'), 'BAR');
+    $this->assertNull($view->get('_nope'));//this one is ignored, since it's "protected" with _
+  }
+
+  function testActionExistsReturnsTrueIsTemplateFound()
+  {
+    $this->toolkit->setSupportedViewTypes(array('.html' => 'lmbDummyView'));
+
+    $controller = new TestingController();
+    $this->assertTrue($controller->actionExists('detail'));
+  }
+
+  function setTemplateIfItExistsBeforePerformingAction()
+  {
+    $this->toolkit->setSupportedViewTypes(array('.html' => 'lmbDummyView'));
+
+    $controller = new TestingController();
+    $controller->setCurrentAction('detail');
+
+    $controller->performAction();
+    $this->assertTrue($controller->template_name, 'testing/detail.html');
+  }
+
+  function testValidateOk()
+  {
+    $controller = new TestingController();
+    $error_list = $controller->getErrorList();
+
+    $ds = new lmbSet();
+
+    $r1 = new MockValidationRule();
+    $r1->expectOnce('validate', array($ds, $error_list));
+
+    $r2 = new MockValidationRule();
+    $r2->expectOnce('validate', array($ds, $error_list));
+
+    $controller->addValidatorRule($r1);
+    $controller->addValidatorRule($r2);
+
+    $this->assertTrue($controller->validate($ds));
+  }
+
+  function testValidateFailed()
+  {
+    $controller = new TestingController();
+    $error_list = $controller->getErrorList();
+    $error_list->addError('blah!');
+
+    $this->assertFalse($controller->validate(new lmbSet()));
+  }
+
+  function testForward()
+  {
+    $controller = new lmbController();
+    $this->assertEqual($controller->forward('testing', 'write'), "Hi!");
+  }
+
+  function testClosePopup()
+  {
+    $controller = new TestingController();
+    $controller->setCurrentAction('popup');
+    $controller->performAction();
+    $this->assertPattern('~^<html><script>~', $this->toolkit->getResponse()->getResponseString());
+  }
+
+  function testDoNotClosePopup()
+  {
+    $controller = new TestingController();
+    $controller->setCurrentAction('without_popup');
+    $controller->performAction();
+    $this->assertPattern('~^Default dummy output~', $this->toolkit->getResponse()->getResponseString());
+  }
+}
+
+



More information about the limb-svn mailing list