Changeset 7389
- Timestamp:
- 02/07/08 14:11:48 (10 months ago)
- Files:
-
- plugins/sfPropelVersionableBehaviorPlugin/trunk/README (modified) (4 diffs)
- plugins/sfPropelVersionableBehaviorPlugin/trunk/config/config.php (modified) (1 diff)
- plugins/sfPropelVersionableBehaviorPlugin/trunk/lib/sfPropelVersionableBehavior.class.php (modified) (5 diffs)
- plugins/sfPropelVersionableBehaviorPlugin/trunk/test/unit/PropelVersionableBehaviorTest.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelVersionableBehaviorPlugin/trunk/README
r7387 r7389 32 32 <column name="version" type="INTEGER" required="true" /> 33 33 or 34 <!-- schema.yml --> 34 # config/schema.yml 35 35 version: { type: integer, required: true } 36 36 }}} … … 168 168 }}} 169 169 170 Alternativley, you can choose to disable the automated creation of a new version at each save for all models by changing the application configuration: 171 172 {{{ 173 # config/app.yml 174 all: 175 sfPropelVersionableBehaviorPlugin: 176 auto_versioning: false 177 }}} 178 179 In this case, you still have the way to manually create a new version of an object: 180 181 {{{ 182 #!php 183 <?php 184 185 $article->setTitle('Please version me even thoug auto_versioning is false'); 186 $article->addVersion(); 187 $article->save(); // article is saved and a new version is created 188 }}} 189 170 190 == Public API == 171 191 … … 178 198 * `void toVersion(integer $version_number)`: Populates the properties of the current object with values from the requested version. Beware that saving the object afterwards will create a new version (and not update the previous version). 179 199 * `array getAllVersions()`: Returns all versions of the object in an ordered array 200 * `void addVersion()`: Increments the object's version number (without saving it) and creates a new ResourceVersion record. To be used when versionConditionMet() is false 180 201 * `ResourceVersion getLastResourceVersion()`: Returns the object's last version object 181 202 * `array getAllResourceVersions()`: Returns all version objects in an array … … 201 222 == Changelog == 202 223 203 === 2008-02-06 | Trunk === 204 224 === 2008-02-07 | Trunk === 225 226 * francois: Added `addVersion` method and refactored the behavior to keep D.R.Y. 205 227 * francois: More explicit documentation on installation 206 228 * francois: Added a few unit tests plugins/sfPropelVersionableBehaviorPlugin/trunk/config/config.php
r7387 r7389 40 40 'getVersion' 41 41 ), 42 array ( 43 'sfPropelVersionableBehavior', 44 'addVersion' 45 ), 42 46 )); plugins/sfPropelVersionableBehaviorPlugin/trunk/lib/sfPropelVersionableBehavior.class.php
r7387 r7389 141 141 return $objects; 142 142 } 143 144 /** 145 * Increments the object's version number (without saving it) and creates a new ResourceVersion record. 146 * To be used when versionConditionMet() is false 147 * 148 * @param BaseObject $resource 149 */ 150 public function addVersion(BaseObject $resource) 151 { 152 if (self::versionConditionMet($resource)) 153 { 154 throw new Exception("Impossible to use addVersion() when auto_versioning is on and versionConditionMet() is true"); 155 } 156 self::incrementVersion($resource); 157 self::createResourceVersion($resource); 158 } 143 159 144 160 # ---- GETTERS & SETTERS … … 179 195 if (self::versionConditionMet($resource)) 180 196 { 181 if ($version = $resource->getLastResourceVersion()) 182 { 183 $resource->setVersion($version->getNumber() + 1); 184 } 185 else 186 { 187 $resource->setVersion(1); 188 } 189 } 190 } 191 197 self::incrementVersion($resource); 198 } 199 } 200 192 201 /** 193 202 * This hook is called juste after object is saved. It takes care of creating a new version of resource. … … 199 208 if (self::versionConditionMet($resource)) 200 209 { 201 $version = new ResourceVersion(); 202 $version->populateFromObject($resource); 203 $version->setNumber($resource->getVersion()); 204 $version->save(); 205 } 206 } 207 210 self::createResourceVersion($resource); 211 } 212 } 213 208 214 /** 209 215 * This hook is called just after a resource is deleted and takes care of deleting its version history. … … 219 225 # ---- HELPER METHODS 220 226 227 /** 228 * Increments the version number of the current object or initializes it 229 * 230 * @param BaseObject $resource 231 */ 232 public function incrementVersion(BaseObject $resource) 233 { 234 if ($version = $resource->getLastResourceVersion()) 235 { 236 $resource->setVersion($version->getNumber() + 1); 237 } 238 else 239 { 240 $resource->setVersion(1); 241 } 242 } 243 244 /** 245 * Creates a new ResourceVersion record based on the object 246 * 247 * @param BaseObject $resource 248 */ 249 public function createResourceVersion(BaseObject $resource) 250 { 251 $version = new ResourceVersion(); 252 $version->populateFromObject($resource); 253 $version->setNumber($resource->getVersion()); 254 $version->save(); 255 } 256 221 257 /** 222 258 * Returns a resource populated with attribute values of given version. … … 285 321 public static function versionConditionMet(BaseObject $resource) 286 322 { 323 if(!sfConfig::get('app_sfPropelVersionableBehaviorPlugin_auto_versioning', true)) 324 { 325 return false; 326 } 327 287 328 if (!$method = self::$condition_method) 288 329 { plugins/sfPropelVersionableBehaviorPlugin/trunk/test/unit/PropelVersionableBehaviorTest.php
r7387 r7389 78 78 ))); 79 79 80 $t = new lime_test( 26, new lime_output_color());80 $t = new lime_test(30, new lime_output_color()); 81 81 82 82 // save() … … 108 108 $t->is($r->getLastResourceVersion()->getResourceInstance()->getByName($test_class_title_column, BasePeer::TYPE_FIELDNAME), 'V2', 'getLastVersion() returns last version of resource'); 109 109 110 // conditional versioning 111 $t->diag('conditional versioning'); 110 112 $r->setByName($test_class_title_column, 'do not version me', BasePeer::TYPE_FIELDNAME); 111 113 $r->save(); 112 $t->is($r->getByName($test_class_version_column, BasePeer::TYPE_FIELDNAME), 2, 'save() complies with conditional versioning feature'); 114 $t->is($r->getByName($test_class_version_column, BasePeer::TYPE_FIELDNAME), 2, 'save() hooks can be deactivated by a versionConditionMet() method'); 115 116 sfConfig::set('app_sfPropelVersionableBehaviorPlugin_auto_versioning', false); 117 $r->setByName($test_class_title_column, 'do not version me either, but for another reason', BasePeer::TYPE_FIELDNAME); 118 $r->save(); 119 $t->is($r->getByName($test_class_version_column, BasePeer::TYPE_FIELDNAME), 2, 'save() hooks can be deactivated by changing app_sfPropelVersionableBehaviorPlugin_auto_versioning to off'); 120 sfConfig::set('app_sfPropelVersionableBehaviorPlugin_auto_versioning', true); 121 122 // addVersion() 123 $t->diag('addVersion()'); 124 sfConfig::set('app_sfPropelVersionableBehaviorPlugin_auto_versioning', false); 125 $r->setByName($test_class_title_column, 'this time, please version me, but manually', BasePeer::TYPE_FIELDNAME); 126 $r->addVersion(); 127 $r->save(); 128 $t->is($r->getByName($test_class_version_column, BasePeer::TYPE_FIELDNAME), 3, 'addVersion() creates a new version even when app_sfPropelVersionableBehaviorPlugin_auto_versioning is set to off'); 129 sfConfig::set('app_sfPropelVersionableBehaviorPlugin_auto_versioning', true); 130 try 131 { 132 $r->addVersion(); 133 $t->fail('calling addVersion() when save hooks are activated throws an exception'); 134 } catch (Exception $e) { 135 $t->pass('calling addVersion() when save hooks are activated throws an exception'); 136 } 113 137 114 138 // toVersion() … … 119 143 $t->is($r->getByName($test_class_title_column, BasePeer::TYPE_FIELDNAME), 'V1', 'toVersion() sets resource attributes to appropriate values'); 120 144 $r->save(); 121 $t->is($r->getByName($test_class_version_column, BasePeer::TYPE_FIELDNAME), 3, 'save() correctly increments version number after toVersion() call');145 $t->is($r->getByName($test_class_version_column, BasePeer::TYPE_FIELDNAME), 4, 'save() correctly increments version number after toVersion() call'); 122 146 try 123 147 { 124 148 $r->toVersion(0); 125 149 $t->fail('toVersion() throws an exception when requested version does not exist'); 126 } 127 catch (Exception $e) 128 { 150 } catch (Exception $e) { 129 151 $t->pass('toVersion() throws an exception when requested version does not exist'); 130 152 } … … 133 155 $t->diag('getAllResourceVersions()'); 134 156 135 $r->setByName($test_class_title_column, 'V 4', BasePeer::TYPE_FIELDNAME);157 $r->setByName($test_class_title_column, 'V5', BasePeer::TYPE_FIELDNAME); 136 158 $r->save(); 137 159 $all_versions = $r->getAllResourceVersions(); 138 $target_versions = array('V1', 'V2', ' V1', 'V4');139 $t->is(count($all_versions), 4, 'getAllResourceVersions() returns right count of versions');160 $target_versions = array('V1', 'V2', 'this time, please version me, but manually', 'V1', 'V5'); 161 $t->is(count($all_versions), 5, 'getAllResourceVersions() returns right count of versions'); 140 162 $versions_titles = array(); 141 163 foreach($all_versions as $v) … … 148 170 $t->diag('getAllVersions()'); 149 171 $all_object_versions = $r->getAllVersions(); 150 $t->is(count($all_object_versions), 4, 'getAllVersions() returns right count of objects');172 $t->is(count($all_object_versions), 5, 'getAllVersions() returns right count of objects'); 151 173 $versions_titles = array(); 152 174 $versions_versions = array(); … … 157 179 } 158 180 $t->is($versions_titles, $target_versions, 'getAllVersions() returns the right versions'); 159 $t->is($versions_versions, array(1, 2, 3, 4 ), 'getAllVersions() returns the array of ordered versions');181 $t->is($versions_versions, array(1, 2, 3, 4, 5), 'getAllVersions() returns the array of ordered versions'); 160 182 161 183 // delete()