| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 class GP_UnitTest_Factory { 3 function __construct() { 4 $this->project = new GP_UnitTest_Factory_For_Project( $this ); 5 $this->original = new GP_UnitTest_Factory_For_Original( $this ); 6 $this->translation_set = new GP_UnitTest_Factory_For_Translation_Set( $this ); 7 $this->translation = new GP_UnitTest_Factory_For_Translation( $this ); 8 $this->user = new GP_UnitTest_Factory_For_User( $this ); 9 $this->locale = new GP_UnitTest_Factory_For_Locale( $this ); 10 } 11 } 12 13 class GP_UnitTest_Factory_For_Project extends GP_UnitTest_Factory_For_Thing { 14 function __construct( $factory = null, $thing = null ) { 15 parent::__construct( $factory, $thing? $thing : new GP_Project ); 16 $this->default_generation_definitions = array( 17 'name' => new GP_UnitTest_Generator_Sequence( 'Project %s' ), 18 'description' => 'I am a project', 19 'parent_project_id' => null, 20 'slug' => false, 21 'active' => 0, 22 ); 23 } 24 } 25 26 class GP_UnitTest_Factory_For_User extends GP_UnitTest_Factory_For_Thing { 27 function __construct( $factory = null, $thing = null ) { 28 parent::__construct( $factory, $thing? $thing : new GP_User ); 29 $this->default_generation_definitions = array( 30 'user_login' => new GP_UnitTest_Generator_Sequence( 'User %s' ), 31 'user_pass' => 'a', 32 'user_email' => new GP_UnitTest_Generator_Sequence( 'user_%s@example.org' ), 33 ); 34 } 35 36 function create_admin( $args = array() ) { 37 $user = $this->create( $args ); 38 GP::$permission->create( array( 'user_id' => $user->id, 'action' => 'admin' ) ); 39 return $user; 40 } 41 } 42 43 44 class GP_UnitTest_Factory_For_Translation_Set extends GP_UnitTest_Factory_For_Thing { 45 function __construct( $factory = null, $thing = null ) { 46 parent::__construct( $factory, $thing? $thing : new GP_Translation_Set ); 47 $this->default_generation_definitions = array( 48 'name' => new GP_UnitTest_Generator_Sequence( 'Translation Set %s' ), 49 'slug' => 'default', 50 'locale' => new GP_UnitTest_Generator_Locale_Name, 51 'project_id' => 1, 52 ); 53 } 54 55 function create_with_project_and_locale( $args = array(), $project_args = array() ) { 56 $locale = $this->factory->locale->create(); 57 $project = $this->factory->project->create( $project_args ); 58 $set = $this->create( array( 'project_id' => $project->id, 'locale' => $locale->slug ) + $args ); 59 $set->project = $project; 60 $set->locale = $locale->slug; 61 return $set; 62 } 63 } 64 65 class GP_UnitTest_Factory_For_Original extends GP_UnitTest_Factory_For_Thing { 66 function __construct( $factory = null, $thing = null ) { 67 parent::__construct( $factory, $thing? $thing : new GP_Original ); 68 $this->default_generation_definitions = array( 69 'singular' => new GP_UnitTest_Generator_Sequence( 'Original %s' ), 70 ); 71 } 72 } 73 74 class GP_UnitTest_Factory_For_Translation extends GP_UnitTest_Factory_For_Thing { 75 function __construct( $factory = null, $thing = null ) { 76 parent::__construct( $factory, $thing? $thing : new GP_Translation ); 77 $this->default_generation_definitions = array( 78 'translation_0' => new GP_UnitTest_Generator_Sequence( 'Translation %s' ), 79 ); 80 } 81 82 function create_with_original_for_translation_set( $set, $args = array() ) { 83 $original = $this->factory->original->create( array( 'project_id' => $set->project_id ) ); 84 $translation = $this->create( array_merge( $args, array( 'original_id' => $original->id, 'translation_set_id' => $set->id ) ) ); 85 $translation->original = $original; 86 $translation->translation_set = $set; 87 return $translation; 88 } 89 } 90 91 class GP_UnitTest_Factory_For_Locale extends GP_UnitTest_Factory_For_Thing { 92 function __construct( $factory = null, $thing = null ) { 93 $thing = (object)array( 'field_names' => array_keys( get_object_vars( $thing? $thing : new GP_Locale ) ) ); 94 parent::__construct( $factory, $thing ); 95 $this->default_generation_definitions = array( 96 'slug' => new GP_UnitTest_Generator_Locale_Name, 97 'english_name' => new GP_UnitTest_Generator_Sequence( 'Locale %s' ), 98 ); 99 } 100 101 function create( $args = array(), $generation_definitions = null ) { 102 if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions; 103 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); 104 $created = new GP_Locale( $generated_args ); 105 if ( $callbacks ) { 106 $updated_fields = $this->apply_callbacks( $callbacks, $created ); 107 $created = new GP_Locale( $updated_fields ); 108 } 109 $locales = &GP_Locales::instance(); 110 $locales->locales[$created->slug] = $created; 111 return $created; 112 } 113 } 114 115 class GP_UnitTest_Factory_For_Thing { 116 117 var $default_generation_definitions; 118 var $thing; 119 var $factory; 120 121 /** 122 * Creates a new factory, which will create objects of a specific Thing 123 * 124 * @param object $factory GLobal factory that can be used to create other objects on the system 125 * @param object $thing Instance of a GP_Thing subclass. This factory will create objects of this type 126 * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values 127 * can be generators -- an object with next() method. There are some default generators: {@link GP_UnitTest_Generator_Sequence}, 128 * {@link GP_UnitTest_Generator_Locale_Name}, {@link GP_UnitTest_Factory_Callback_After_Create}. 129 */ 130 function __construct( $factory, $thing, $default_generation_definitions = array() ) { 131 $this->factory = $factory; 132 $this->default_generation_definitions = $default_generation_definitions; 133 $this->thing = $thing; 134 } 135 136 function create( $args = array(), $generation_definitions = null ) { 137 if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions; 138 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); 139 $created = $this->thing->create( $generated_args ); 140 if ( !$created || is_wp_error( $created ) ) return $created; 141 if ( $callbacks ) { 142 $updated_fields = $this->apply_callbacks( $callbacks, $created ); 143 $save_result = $created->save( $updated_fields ); 144 if ( !$save_result || is_wp_error( $save_result ) ) return $save_result; 145 } 146 return $created; 147 } 148 149 function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { 150 $callbacks = array(); 151 if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions; 152 foreach( $this->thing->field_names as $field_name ) { 153 if ( !isset( $args[$field_name] ) ) { 154 if ( !isset( $generation_definitions[$field_name] ) ) { 155 continue; 156 } 157 $generator = $generation_definitions[$field_name]; 158 if ( is_scalar( $generator ) ) 159 $args[$field_name] = $generator; 160 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { 161 $callbacks[$field_name] = $generator; 162 } elseif ( is_object( $generator ) ) 163 $args[$field_name] = $generator->next(); 164 else 165 return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' ); 166 167 } 168 } 169 return $args; 170 } 171 172 function apply_callbacks( $callbacks, $created ) { 173 $updated_fields = array(); 174 foreach( $callbacks as $field_name => $generator ) { 175 $updated_fields[$field_name] = $generator->call( $created ); 176 } 177 return $updated_fields; 178 } 179 180 function callback( $function ) { 181 return new GP_UnitTest_Factory_Callback_After_Create( $function ); 182 } 183 } 184 185 class GP_UnitTest_Generator_Sequence { 186 var $next; 187 var $template_string; 188 189 function __construct( $template_string = '%s', $start = 1 ) { 190 $this->next = $start; 191 $this->template_string = $template_string; 192 } 193 194 function next() { 195 $generated = sprintf( $this->template_string , $this->next ); 196 $this->next++; 197 return $generated; 198 } 199 } 200 201 class GP_UnitTest_Generator_Locale_Name extends GP_UnitTest_Generator_Sequence { 202 function __construct() { 203 parent::__construct( '%s', 'aa' ); 204 } 205 } 206 207 class GP_UnitTest_Factory_Callback_After_Create { 208 var $callback; 209 210 function __construct( $callback ) { 211 $this->callback = $callback; 212 } 213 214 function call( $object ) { 215 return call_user_func( $this->callback, $object ); 216 } 217 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed May 22 03:59:55 2013 | Hosted by follow the white rabbit. |