* added first sketch of an auto loader (will become obsolete at time the new TYPO3...
authorJochen Rau <j.rau@web.de>
Thu, 19 Feb 2009 06:01:57 +0000 (06:01 +0000)
committerJochen Rau <j.rau@web.de>
Thu, 19 Feb 2009 06:01:57 +0000 (06:01 +0000)
* refactored Data Map for better testability
* added test case for Data Map

typo3/sysext/extbase/Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_DataMap.php
typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Mapper_DataMap_testcase.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 17a9186..3f53f8a 100644 (file)
@@ -78,58 +78,68 @@ class TX_EXTMVC_Persistence_Mapper_DataMap {
        public function initialize() {
                $columns = $GLOBALS['TCA'][$this->getTableName()]['columns'];
                if (is_array($columns)) {
-                       $this->addColumn('uid', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
-                       $this->addColumn('pid', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
-                       $this->addColumn('tstamp', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
-                       $this->addColumn('crdate', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
-                       $this->addColumn('cruser_id', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
-                       if ($this->getDeletedColumnName() !== NULL) {
-                               $this->addColumn($this->getDeletedColumnName(), TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
-                       }
-                       if ($this->getHiddenColumnName() !== NULL) {
-                               $this->addColumn($this->getHiddenColumnName(), TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
-                       }
-
+                       $this->addCommonColumns();                      
                        $columnMapClassName = t3lib_div::makeInstanceClassName('TX_EXTMVC_Persistence_Mapper_ColumnMap');                       
                        foreach ($columns as $columnName => $columnConfiguration) {
                                $columnMap = new $columnMapClassName($columnName, $this);
-                               if (strpos($columnConfiguration['config']['eval'], 'date') !== FALSE
-                                       || strpos($columnConfiguration['config']['eval'], 'datetime') !== FALSE) {
-                                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
-                               } elseif ($columnConfiguration['config']['type'] === 'check' && empty($columnConfiguration['config']['items'])) {
-                                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
-                               } elseif (strpos($columnConfiguration['config']['eval'], 'int') !== FALSE) {
-                                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
-                               } elseif (strpos($columnConfiguration['config']['eval'], 'double2') !== FALSE) {
-                                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_FLOAT);
-                               } else {
-                                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_STRING);
-                               }
-                               
+                               $this->setTypeOfValue($columnMap, $columnConfiguration);
                                // TODO support for IRRE
                                // TODO support for MM_insert_fields and MM_match_fields
-                               if (array_key_exists('foreign_table', $columnConfiguration['config']) && !array_key_exists('MM', $columnConfiguration['config'])) {
-                                       $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
-                                       $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
-                                       $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
-                                       $columnMap->setChildTableWhere($columnConfiguration['config']['foreign_table_where']);
-                                       $columnMap->setChildSortbyFieldName($columnConfiguration['config']['foreign_sortby']);
-                                       $columnMap->setParentKeyFieldName($columnConfiguration['config']['foreign_field']);
-                                       $columnMap->setParentTableFieldName($columnConfiguration['config']['foreign_table_field']);                                     
-                               } elseif (array_key_exists('MM', $columnConfiguration['config'])) {
-                                       $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
-                                       $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
-                                       $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
-                                       $columnMap->setRelationTableName($columnConfiguration['config']['MM']);
-                               } else {
-                                       $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_NONE);                                   
-                               }
+                               $this->setRelations($columnMap, $columnConfiguration);
                                $this->addColumnMap($columnMap);
                        }
                } else {
                        // TODO Throw exception 
                }
        }
+       
+       protected function addCommonColumns() {
+               $this->addColumn('uid', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
+               $this->addColumn('pid', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
+               $this->addColumn('tstamp', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
+               $this->addColumn('crdate', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
+               $this->addColumn('cruser_id', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
+               if ($this->getDeletedColumnName() !== NULL) {
+                       $this->addColumn($this->getDeletedColumnName(), TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
+               }
+               if ($this->getHiddenColumnName() !== NULL) {
+                       $this->addColumn($this->getHiddenColumnName(), TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
+               }
+       }
+       
+       protected function setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
+               if (strpos($columnConfiguration['config']['eval'], 'date') !== FALSE
+                       || strpos($columnConfiguration['config']['eval'], 'datetime') !== FALSE) {
+                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
+               } elseif ($columnConfiguration['config']['type'] === 'check' && empty($columnConfiguration['config']['items'])) {
+                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
+               } elseif (strpos($columnConfiguration['config']['eval'], 'int') !== FALSE) {
+                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
+               } elseif (strpos($columnConfiguration['config']['eval'], 'double2') !== FALSE) {
+                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_FLOAT);
+               } else {
+                       $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_STRING);
+               }
+       }
+       
+       protected function setRelations(TX_EXTMVC_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
+               if (array_key_exists('foreign_table', $columnConfiguration['config']) && !array_key_exists('MM', $columnConfiguration['config'])) {
+                       $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
+                       $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
+                       $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
+                       $columnMap->setChildTableWhere($columnConfiguration['config']['foreign_table_where']);
+                       $columnMap->setChildSortbyFieldName($columnConfiguration['config']['foreign_sortby']);
+                       $columnMap->setParentKeyFieldName($columnConfiguration['config']['foreign_field']);
+                       $columnMap->setParentTableFieldName($columnConfiguration['config']['foreign_table_field']);                                     
+               } elseif (array_key_exists('MM', $columnConfiguration['config'])) {
+                       $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
+                       $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
+                       $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
+                       $columnMap->setRelationTableName($columnConfiguration['config']['MM']);
+               } else {
+                       $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_NONE);                                   
+               }
+       }
 
        public function setColumnMaps(array $columnMaps) {
                $this->columnMaps = $columnMaps;
diff --git a/typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Mapper_DataMap_testcase.php b/typo3/sysext/extbase/Tests/TX_EXTMVC_Persistence_Mapper_DataMap_testcase.php
new file mode 100644 (file)
index 0000000..e53bc92
--- /dev/null
@@ -0,0 +1,139 @@
+<?php
+declare(ENCODING = 'utf-8');
+
+/*                                                                        *
+ * This script belongs to the FLOW3 framework.                            *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License as published by the *
+ * Free Software Foundation, either version 3 of the License, or (at your *
+ * option) any later version.                                             *
+ *                                                                        *
+ * 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 Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+require_once(PATH_tslib . 'class.tslib_content.php');
+require_once(t3lib_extMgm::extPath('blogexample') . 'Classes/Domain/TX_Blogexample_Domain_Blog.php');
+
+class TX_EXTMVC_Persistence_Mapper_DataMap_testcase extends tx_phpunit_testcase {
+       
+       public function setUp() {
+               $this->cObj = t3lib_div::makeInstance('tslib_cObj');
+               $GLOBALS['TSFE']->fe_user = $this->getMock('tslib_feUserAuth');
+               $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array('includeTCA'));
+               $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;
+               global $_EXTKEY;
+               $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',
+                                               '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_DataMapCanBeInitialized() {
+               $dataMap = new TX_EXTMVC_Persistence_Mapper_DataMap('TX_Blogexample_Domain_Blog');
+               $dataMap->initialize();
+               $columnMaps = $dataMap->getColumnMaps();
+               $this->assertEquals(11, count($columnMaps), 'The data map was not initialized (wrong number of column maps set).');
+       }
+       
+}
+?>
\ No newline at end of file
index 3a69cee..4c8e1fe 100644 (file)
@@ -29,109 +29,8 @@ require_once(t3lib_extMgm::extPath('extmvc') . 'Tests/Fixtures/TX_EXTMVC_Tests_F
 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',
-                                               '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;
                $addedObjects = $session->getAddedObjects();
index ae47ac8..f6a4acd 100644 (file)
@@ -72,6 +72,7 @@ class TX_EXTMVC_Dispatcher {
         */
        public function __construct() {
                $this->arguments = new ArrayObject;
+               spl_autoload_register(array($this, 'autoLoadClasses'));
        }
 
        /**
@@ -116,5 +117,27 @@ class TX_EXTMVC_Dispatcher {
                return $response->getContent();
        }
 
+       /**
+        * Loads php files containing classes or interfaces found in the classes directory of
+        * an extension.
+        *
+        * @param string $className: Name of the class/interface to load
+        * @return void
+        * @author Robert Lemke <robert@typo3.org>
+        * @author Jochen Rau <jochen.rau@typoplanet.de>
+        */
+       protected function autoLoadClasses($className) {
+               $classNameParts = explode('_', $className);
+               if ($classNameParts[0] === 'ux') {
+                       array_shift($classNameParts);
+               }
+               if (count($classNameParts) > 2 && $classNameParts[0] === 'TX') {
+                       $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
+                       $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
+                       $classFilePathAndName .= implode('_', $classNameParts) . '.php';
+               }
+               if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) require_once($classFilePathAndName);                    
+       }
+
 }
 ?>
\ No newline at end of file