* added some fixtures for the unit tests
authorJochen Rau <j.rau@web.de>
Tue, 10 Feb 2009 22:35:18 +0000 (22:35 +0000)
committerJochen Rau <j.rau@web.de>
Tue, 10 Feb 2009 22:35:18 +0000 (22:35 +0000)
* added a cache for the table configurations
* removed the require statements from the dispatcher
* added includeLibs to the blog example

typo3/sysext/extbase/Classes/Controller/TX_EXTMVC_Controller_AbstractController.php
typo3/sysext/extbase/Classes/Controller/TX_EXTMVC_Controller_ActionController.php
typo3/sysext/extbase/Classes/Controller/TX_EXTMVC_Controller_Argument.php
typo3/sysext/extbase/Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_TcaMapper.php
typo3/sysext/extbase/Classes/View/TX_EXTMVC_View_DefaultView.php
typo3/sysext/extbase/Tests/Fixtures/TX_EXTMVC_Tests_Fixtures_Entity.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Session_testcase.php
typo3/sysext/extbase/class.tx_extmvc_dispatcher.php

index 447bf03..c1e6154 100755 (executable)
@@ -101,6 +101,18 @@ abstract class TX_EXTMVC_Controller_AbstractController implements TX_EXTMVC_Cont
                // $this->initializeArguments();
                // $this->mapRequestArgumentsToLocalArguments();
        }
+       
+       /**
+        * Initializes (registers / defines) arguments of this controller.
+        *
+        * Override this method to add arguments which can later be accessed
+        * by the action methods.
+        *
+        * @return void
+        * @author Robert Lemke <robert@typo3.org>
+        */
+       protected function initializeArguments() {
+       }
 
        /**
         * Forwards the request to another controller.
@@ -161,32 +173,6 @@ abstract class TX_EXTMVC_Controller_AbstractController implements TX_EXTMVC_Cont
                throw new TX_EXTMVC_Exception_StopAction();
        }
 
-       /**
-        * Returns the arguments which are defined for this controller.
-        *
-        * Use this information if you want to know about what arguments are supported and / or
-        * required by this controller or if you'd like to know about further information about
-        * each argument.
-        *
-        * @return TX_EXTMVC_Controller_Arguments Supported arguments of this controller
-        * @author Robert Lemke <robert@typo3.org>
-        */
-       public function getArguments() {
-               return $this->arguments;
-       }
-
-       /**
-        * Initializes (registers / defines) arguments of this controller.
-        *
-        * Override this method to add arguments which can later be accessed
-        * by the action methods.
-        *
-        * @return void
-        * @author Robert Lemke <robert@typo3.org>
-        */
-       public function initializeArguments() {
-       }
-
        /**
         * Maps arguments delivered by the request object to the local controller arguments.
         *
@@ -196,8 +182,8 @@ abstract class TX_EXTMVC_Controller_AbstractController implements TX_EXTMVC_Cont
        protected function mapRequestArgumentsToLocalArguments() {
                $this->propertyMapper->setTarget($this->arguments);
                foreach ($this->arguments as $argument) {
-                       if ($argument->getFilter() != NULL) $this->propertyMapper->registerFilter($argument->getFilter(), $argument->getName());
-                       if ($argument->getPropertyConverter() != NULL) $this->propertyMapper->registerPropertyConverter($argument->getPropertyConverter(), $argument->getName(), $argument->getPropertyConverterInputFormat());
+                       if ($argument->getFilter() !== NULL) $this->propertyMapper->registerFilter($argument->getFilter(), $argument->getName());
+                       if ($argument->getPropertyConverter() !== NULL) $this->propertyMapper->registerPropertyConverter($argument->getPropertyConverter(), $argument->getName(), $argument->getPropertyConverterInputFormat());
                }
 
                $argumentsValidator = t3lib_div::makeInstance('TX_EXTMVC_Controller_ArgumentsValidator', $this->arguments);
index cfc755b..b49b608 100644 (file)
@@ -32,14 +32,21 @@ require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Con
 class TX_EXTMVC_Controller_ActionController extends TX_EXTMVC_Controller_AbstractController {
 
        /**
-        * @var boolean If initializeView() should be called on an action invocation.
+        * @var TX_EXTMVC_View_AbstractView By default a view with the same name as the current action is provided. Contains NULL if none was found.
         */
-       protected $initializeView = TRUE;
+       protected $view = NULL;
 
        /**
-        * @var TX_EXTMVC_View_AbstractView By default a view with the same name as the current action is provided. Contains NULL if none was found.
+        * By default a matching view will be resolved. If this property is set, automatic resolving is disabled and the specified object is used instead.
+        * @var string
         */
-       protected $view = NULL;
+       protected $viewObjectName = NULL;
+
+       /**
+        * Name of the action method
+        * @var string
+        */
+       protected $actionMethodName = 'indexAction';
 
        /**
         * Handles a request. The result output is returned by altering the given response.
@@ -50,25 +57,80 @@ class TX_EXTMVC_Controller_ActionController extends TX_EXTMVC_Controller_Abstrac
         * @author Robert Lemke <robert@typo3.org>
         */
        public function processRequest(TX_EXTMVC_Request $request, TX_EXTMVC_Response $response) {
-               parent::processRequest($request, $response);
+               $this->request = $request;
+               $this->request->setDispatched(TRUE);
+               $this->response = $response;
+
+               $this->actionMethodName = $this->resolveActionMethodName();
+               // $this->initializeArguments();
+               // $this->mapRequestArgumentsToLocalArguments();
+               $this->initializeView();
+               $this->initializeAction();
+
                $this->callActionMethod();
        }
 
        /**
-        * Determines the name of the requested action and calls the action method accordingly.
-        * If no action was specified, the "default" action is assumed.
+        * Implementation of the arguments initilization in the action controller:
+        * Automatically registers arguments of the current action
+        *
+        * IMPORTANT: If this method is overridden, make sure to call this parent
+        *            _before_ your own code because otherwise the order of automatically
+        *            registered arguments wouldn't match the action method signature.
         *
         * @return void
         * @author Robert Lemke <robert@typo3.org>
+        */
+       protected function initializeArguments() {
+               $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->actionMethodName);
+               $methodTagsAndValues = $this->reflectionService->getMethodTagsValues(get_class($this), $this->actionMethodName);
+               foreach ($methodParameters as $parameterName => $parameterInfo) {
+                       $dataType = 'Text';
+                       if (isset($methodTagsAndValues['param']) && count($methodTagsAndValues['param']) > 0) {
+                               $explodedTagValue = explode(' ', array_shift($methodTagsAndValues['param']));
+                               switch ($explodedTagValue[0]) {
+                                       case 'integer' :
+                                               $dataType = 'Integer';
+                                       break;
+                                       default:
+                                               if (strpos($dataType, '\\') !== FALSE) {
+                                                       $dataType = $explodedTagValue[0];
+                                               }
+                               }
+                       }
+                       $this->arguments->addNewArgument($parameterName, $dataType);
+               }
+       }
+
+       /**
+        * Determines the action method and assures that the method exists.
+        *
+        * @return string The action method name
         * @throws TX_EXTMVC_Exception_NoSuchAction if the action specified in the request object does not exist (and if there's no default action either).
+        * @author Robert Lemke <robert@typo3.org>
         */
-       protected function callActionMethod() {
+       protected function resolveActionMethodName() {
                $actionMethodName = $this->request->getControllerActionName() . 'Action';
+               if (!method_exists($this, $actionMethodName)) throw new TX_EXTMVC_Exception_NoSuchAction('An action "' . $actionMethodName . '" does not exist in controller "' . get_class($this) . '".', 1186669086);
+               return $actionMethodName;
+       }
 
-               if (!method_exists($this, $actionMethodName)) throw new TX_EXTMVC_Exception_NoSuchAction('An action "' . $this->request->getControllerActionName() . '" does not exist in controller "' . get_class($this) . '".', 1186669086);
-               $this->initializeAction();
-               if ($this->initializeView) $this->initializeView();
-               $actionResult = call_user_func_array(array($this, $actionMethodName), array());
+       /**
+        * Calls the specified action method and passes the arguments.
+        * If the action returns a string, it is appended to the content in the
+        * response object.
+        *
+        * @param string $actionMethodName Name of the action method
+        * @return void
+        * @author Robert Lemke <robert@typo3.org>
+        */
+       protected function callActionMethod() {
+               $preparedArguments = array();
+               // foreach ($this->arguments as $argument) {
+               //      $preparedArguments[] = $argument->getValue();
+               // }
+
+               $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
                if (is_string($actionResult) && strlen($actionResult) > 0) {
                        $this->response->appendContent($actionResult);
                }
@@ -83,11 +145,9 @@ class TX_EXTMVC_Controller_ActionController extends TX_EXTMVC_Controller_Abstrac
         * @author Robert Lemke <robert@typo3.org>
         */
        protected function initializeView() {
-               $viewObjectName = $this->request->getViewObjectName();
-               
-               if ($viewObjectName === FALSE) {
-                       $viewObjectName = 'TX_EXTMVC_View_EmptyView';
-               }
+               $viewObjectName = ($this->viewObjectName === NULL) ? $this->request->getViewObjectName() : $this->viewObjectName;
+               if ($viewObjectName === FALSE) $viewObjectName = 'TX_EXTMVC_View_EmptyView';
+
                $this->view = t3lib_div::makeInstance($viewObjectName);
                $this->view->setRequest($this->request);
        }
index a011514..5f15aa6 100644 (file)
@@ -192,9 +192,9 @@ class Argument {
        public function setDataType($dataType) {
                $this->dataType = ($dataType != '' ? $dataType : 'Text');
 
-               $dataTypeValidatorClassname = $this->dataType;
-               if (!$this->objectManager->isObjectRegistered($dataTypeValidatorClassname)) $dataTypeValidatorClassname = 'F3_FLOW3_Validation_Validator\\' . $this->dataType;
-               $this->datatypeValidator = $this->objectManager->getObject($dataTypeValidatorClassname);
+               $dataTypeValidatorClassName = $this->dataType;
+               if (!$this->objectManager->isObjectRegistered($dataTypeValidatorClassName)) $dataTypeValidatorClassName = 'TX_EXTMVC_Validation_Validator_' . $this->dataType;
+               $this->datatypeValidator = $this->objectManager->getObject($dataTypeValidatorClassName);
 
                return $this;
        }
index 1d9145c..0a7f607 100644 (file)
@@ -25,13 +25,13 @@ require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Utility/TX_EXTMVC_Utility_Strings.php');
 
 /**
- * A mapper to map database tables configured in $TCA onto domain objects.
+ * A mapper to map database tables configured in $TCA on domain objects.
  *
  * @version $Id:$
  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  */
 class TX_EXTMVC_Persistence_Mapper_TcaMapper implements t3lib_singleton {
-       
+
        /**
         * The content object
         *
@@ -42,10 +42,17 @@ class TX_EXTMVC_Persistence_Mapper_TcaMapper implements t3lib_singleton {
        /**
         * The persistence session
         *
-        * @var 
+        * @var TX_EXTMVC_Persistence_Session
         **/
        protected $session;
                
+       /**
+        * A hash of table configurations from $TCA
+        *
+        * @var array
+        **/
+       protected $tableConfigurations;
+               
        /**
         * Constructs a new mapper
         *
@@ -411,16 +418,18 @@ class TX_EXTMVC_Persistence_Mapper_TcaMapper implements t3lib_singleton {
        }
        
        /**
-        * Returns all columns configured in $TCA for a given class
+        * Returns all columns configured in $TCA for a given table
         *
-        * @param string $className The class name 
+        * @param string $tableName The table name 
         * @return array The column configurations from $TCA
         * @author Jochen Rau <jochen.rau@typoplanet.de>
         */
-       protected function getColumns($className) {
-               $tableName = $this->getTableName($className);
-               t3lib_div::loadTCA($tableName);
-               return $GLOBALS['TCA'][$tableName]['columns'];
+       protected function getColumns($tableName) {
+               if (empty($this->tableConfigurations[$tableName])) {
+                       t3lib_div::loadTCA($tableName);
+                       $this->tableConfigurations[$tableName] = $GLOBALS['TCA'][$tableName];
+               }
+               return $this->tableConfigurations[$tableName]['columns'];
        }
        
        /**
index 8b5bc2f..6c0838c 100755 (executable)
@@ -29,11 +29,6 @@ declare(ENCODING = 'utf-8');
  */
 class TX_EXTMVC_View_DefaultView extends TX_EXTMVC_View_AbstractView {
 
-       /**
-        * @var TX_EXTMVC_Request
-        */
-       protected $request;
-
        /**
         * Renders the default view
         *
diff --git a/typo3/sysext/extbase/Tests/Fixtures/TX_EXTMVC_Tests_Fixtures_Entity.php b/typo3/sysext/extbase/Tests/Fixtures/TX_EXTMVC_Tests_Fixtures_Entity.php
new file mode 100644 (file)
index 0000000..8c63f26
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+declare(ENCODING = 'utf-8');
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/DomainObject/TX_EXTMVC_DomainObject_Entity.php');
+
+/**
+ * An entity
+ *
+ * @version $Id:$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
+ * @entity
+ */
+class TX_EXTMVC_Tests_Fixtures_Entity extends TX_EXTMVC_DomainObject_Entity {
+
+       /**
+        * The entity's name
+        *
+        * @var string
+        */
+       protected $name;
+
+
+       /**
+        * Constructs this entity
+        *
+        * @param string $name Name of this blog
+        * @return void
+        */
+       public function __construct($name) {
+               $this->setName($name);
+       }
+       
+       /**
+        * Sets this entity's name
+        *
+        * @param string $name The entity's name
+        * @return void
+        * @author Jochen Rau <jochen.rau@typoplanet.de>
+        */
+       public function setName($name) {
+               $this->name = $name;
+       }
+
+       /**
+        * Returns the entity's name
+        *
+        * @return string The entity's name
+        * @author Jochen Rau <jochen.rau@typoplanet.de>
+        */
+       public function getName() {
+               return $this->name;
+       }
+       
+       // /**
+       //  * Mock method
+       //  *
+       //  * @return void
+       //  * @author Jochen Rau <jochen.rau@typoplanet.de>
+       //  */
+       // public function _memorizeCleanState() {
+       // }
+
+}
+?>
\ No newline at end of file
index 2b1c213..51995d7 100644 (file)
@@ -21,10 +21,116 @@ declare(ENCODING = 'utf-8');
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
 
+require_once(PATH_tslib . 'class.tslib_content.php');
 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/DomainObject/TX_EXTMVC_DomainObject_Entity.php');
+require_once(t3lib_extMgm::extPath('extmvc') . 'Tests/Fixtures/TX_EXTMVC_Tests_Fixtures_Entity.php');
 
 class TX_EXTMVC_Persistence_Session_testcase extends tx_phpunit_testcase {
+       
+       public function setUp() {
+               // global $TCA;
+               global $_EXTKEY;
+               
+               $this->cObj = t3lib_div::makeInstance('tslib_cObj');
+               // $GLOBALS['TSFE']->fe_user = $this->getMock('tslib_feUserAuth');
+               $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array('includeTCA'));
+               // $TCA = $this->setupTca();
+               $GLOBALS['TSFE']->expects($this->any())
+                       ->method('includeTCA')
+                       ->will($this->returnValue(NULL));
+               
+               
+               $GLOBALS['TSFE']->fe_user->user['uid'] = 999;
+               $GLOBALS['TSFE']->id = 42;              
+       }
+       
+       public function setupTCA() {
+               global $TCA;
+               $TCA['tx_blogexample_domain_blog'] = array (
+                       'ctrl' => array (
+                               'title'             => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog',
+                               'label'                         => 'name',
+                               'tstamp'            => 'tstamp',
+                               'prependAtCopy'     => 'LLL:EXT:lang/locallang_general.xml:LGL.prependAtCopy',
+                               'delete'            => 'deleted',
+                               'enablecolumns'     => array (
+                                       'disabled' => 'hidden'
+                               ),
+                               'iconfile'          => t3lib_extMgm::extRelPath($_EXTKEY).'Resources/Icons/icon_tx_blogexample_domain_blog.gif'
+                       ),
+                       'interface' => array(
+                               'showRecordFieldList' => 'hidden, name, description, logo, posts'
+                       ),
+                       'columns' => array(
+                               'hidden' => array(
+                                       'exclude' => 1,
+                                       'label'   => 'LLL:EXT:lang/locallang_general.xml:LGL.hidden',
+                                       'config'  => array(
+                                               'type' => 'check'
+                                       )
+                               ),
+                               'name' => array(
+                                       'exclude' => 0,
+                                       'label'   => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.name',
+                                       'config'  => array(
+                                               'type' => 'input',
+                                               'size' => 20,
+                                               'eval' => 'trim,required',
+                                               'max'  => 256
+                                       )
+                               ),
+                               'description' => array(
+                                       'exclude' => 1,
+                                       'label'   => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.description',
+                                       'config'  => array(
+                                               'type' => 'text',
+                                               'eval' => 'required',
+                                               'rows' => 30,
+                                               'cols' => 80,
+                                       )
+                               ),
+                               'logo' => array(
+                                       'exclude' => 1,
+                                       'label'   => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.logo',
+                                       'config'  => array(
+                                               'type'          => 'group',
+                                               'internal_type' => 'file',
+                                               'allowed'       => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
+                                               'max_size'      => 3000,
+                                               'uploadfolder'  => 'uploads/pics',
+                                               'show_thumbs'   => 1,
+                                               'size'          => 1,
+                                               'maxitems'      => 1,
+                                               'minitems'      => 0
+                                       )
+                               ),
+                               'posts' => array(
+                                       'exclude' => 1,
+                                       'label'   => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.posts',
+                                       'config' => array(
+                                               'type' => 'inline',
+                                               // TODO is 'foreign_class' in $TCA the best way?
+                                               'foreign_class' => 'TX_Blogexample_Domain_Post',
+                                               'foreign_table' => 'tx_blogexample_domain_post',
+                                               'foreign_field' => 'blog_uid',
+                                               'foreign_table_field' => 'blog_table',
+                                               'appearance' => array(
+                                                       'newRecordLinkPosition' => 'bottom',
+                                                       'collapseAll' => 1,
+                                                       'expandSingle' => 1,
+                                               ),
+                                       )
+                               ),
+                       ),
+                       'types' => array(
+                               '1' => array('showitem' => 'hidden, name, description, logo, posts')
+                       ),
+                       'palettes' => array(
+                               '1' => array('showitem' => '')
+                       )
+               );
+       }
 
        public function test_NewSessionIsEmpty() {
                $session = new TX_EXTMVC_Persistence_Session;
@@ -204,6 +310,18 @@ class TX_EXTMVC_Persistence_Session_testcase extends tx_phpunit_testcase {
                $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
        }
 
+       public function test_DirtyEntitiesAreReturned() {
+               $session = new TX_EXTMVC_Persistence_Session;
+               $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
+               $entity->expects($this->any())
+                       ->method('_isDirty')
+                       ->will($this->returnValue(TRUE));
+               $session->registerReconstitutedObject($entity);
+               $dirtyObjects = $session->getDirtyObjects();
+               $this->assertEquals(1, count($dirtyObjects), 'There is more than one dirty object.');
+               $this->assertEquals($entity, $dirtyObjects[0], 'The entity doesn\'t equal to the dirty object retrieved from the session.');
+       }
+
        
 }
 ?>
\ No newline at end of file
index 7c54012..b5c4160 100644 (file)
@@ -34,9 +34,7 @@ require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Pe
 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_TcaMapper.php');
 
 // FIXME
-require_once(t3lib_extMgm::extPath('blogexample') . 'Classes/Controller/TX_Blogexample_Controller_PostsController.php');
-require_once(t3lib_extMgm::extPath('blogexample') . 'Classes/View/TX_Blogexample_View_PostsIndex.php');
-require_once(t3lib_extMgm::extPath('blogexample') . 'Classes/View/TX_Blogexample_View_PostsShow.php');
+// require_once(t3lib_extMgm::extPath('blogexample') . 'Classes/Controller/TX_Blogexample_Controller_PostsController.php');
 
 /**
  * Creates a request an dispatches it to the controller which was specified by TS Setup, Flexform,
@@ -89,7 +87,7 @@ class TX_EXTMVC_Dispatcher {
        public function dispatch($content, $configuration) {
                // debug($configuration);
                // TODO instantiate the configurationManager
-               
+
                $request = t3lib_div::makeInstance('TX_EXTMVC_Web_Request');
                $request->setControllerExtensionKey($configuration['extension']);
                $request->setControllerName($configuration['controller']);
@@ -107,7 +105,7 @@ class TX_EXTMVC_Dispatcher {
                // $controller->injectSettings($this->configurationManager->getSettings($request->getControllerExtensionKey()));
                $session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session');
                try {
-                       $controller->processRequest($request, $response);
+                       $controller->processRequest($request, $response);                       
                } catch (TX_EXTMVC_Exception_StopAction $ignoredException) {
                }
                $session->commit();