[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/lib/ -> factory.php (source)

   1  <?php
   2  class GP_UnitTest_Factory extends WP_UnitTest_Factory {
   3  
   4      /**
   5       * @var GP_UnitTest_Factory_For_Project
   6       */
   7      public $project;
   8  
   9      /**
  10       * @var GP_UnitTest_Factory_For_Original
  11       */
  12      public $original;
  13  
  14      /**
  15       * @var GP_UnitTest_Factory_For_Translation_Set
  16       */
  17      public $translation_set;
  18  
  19      /**
  20       * @var GP_UnitTest_Factory_For_Translation
  21       */
  22      public $translation;
  23  
  24      /**
  25       * @var GP_UnitTest_Factory_For_User
  26       */
  27      public $user;
  28  
  29      /**
  30       * @var GP_UnitTest_Factory_For_Locale
  31       */
  32      public $locale;
  33  
  34  	function __construct() {
  35          $this->project = new GP_UnitTest_Factory_For_Project( $this );
  36          $this->original = new GP_UnitTest_Factory_For_Original( $this );
  37          $this->translation_set = new GP_UnitTest_Factory_For_Translation_Set( $this );
  38          $this->translation = new GP_UnitTest_Factory_For_Translation( $this );
  39          $this->user = new GP_UnitTest_Factory_For_User( $this );
  40          $this->locale = new GP_UnitTest_Factory_For_Locale( $this );
  41      }
  42  }
  43  
  44  class GP_UnitTest_Factory_For_Project extends GP_UnitTest_Factory_For_Thing {
  45  	function __construct( $factory = null, $thing = null ) {
  46          parent::__construct( $factory, $thing? $thing : new GP_Project );
  47          $this->default_generation_definitions = array(
  48              'name' => new GP_UnitTest_Generator_Sequence( 'Project %s' ),
  49              'description' => 'I am a project',
  50              'parent_project_id' => null,
  51              'slug' => false,
  52              'active' => 0,
  53          );
  54      }
  55  }
  56  
  57  class GP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_User {
  58  
  59  	function create_admin( $args = array() ) {
  60          $user = $this->create( $args );
  61          GP::$permission->create( array( 'user_id' => $user, 'action' => 'admin' ) );
  62          return $user;
  63      }
  64  }
  65  
  66  
  67  class GP_UnitTest_Factory_For_Translation_Set extends GP_UnitTest_Factory_For_Thing {
  68  	function __construct( $factory = null, $thing = null ) {
  69          parent::__construct( $factory, $thing? $thing : new GP_Translation_Set );
  70          $this->default_generation_definitions = array(
  71              'name' => new GP_UnitTest_Generator_Sequence( 'Translation Set %s' ),
  72              'slug' => 'default',
  73              'locale' => new GP_UnitTest_Generator_Locale_Name,
  74              'project_id' => 1,
  75          );
  76      }
  77  
  78  	function create_with_project( $args = array(), $project_args = array() ) {
  79          $project = $this->factory->project->create( $project_args );
  80          $set = $this->create( array( 'project_id' => $project->id ) + $args );
  81          $set->project = $project;
  82          return $set;
  83      }
  84  
  85  	function create_with_project_and_locale( $args = array(), $project_args = array() ) {
  86          $locale = $this->factory->locale->create();
  87          $project = $this->factory->project->create( $project_args );
  88          $set = $this->create( array( 'project_id' => $project->id, 'locale' => $locale->slug ) + $args );
  89          $set->project = $project;
  90          $set->locale = $locale->slug;
  91          return $set;
  92      }
  93  }
  94  
  95  class GP_UnitTest_Factory_For_Original extends GP_UnitTest_Factory_For_Thing {
  96  	function __construct( $factory = null, $thing = null ) {
  97          parent::__construct( $factory, $thing? $thing : new GP_Original );
  98          $this->default_generation_definitions = array(
  99              'singular' => new GP_UnitTest_Generator_Sequence( 'Original %s' ),
 100          );
 101      }
 102  }
 103  
 104  class GP_UnitTest_Factory_For_Translation extends GP_UnitTest_Factory_For_Thing {
 105  	function __construct( $factory = null, $thing = null ) {
 106          parent::__construct( $factory, $thing? $thing : new GP_Translation );
 107          $this->default_generation_definitions = array(
 108              'translation_0' => new GP_UnitTest_Generator_Sequence( 'Translation %s' ),
 109          );
 110      }
 111  
 112  	function create_with_original_for_translation_set( $set, $args = array() ) {
 113          $original = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 114          $translation = $this->create( array_merge( $args, array( 'original_id' => $original->id, 'translation_set_id' => $set->id ) ) );
 115          $translation->original = $original;
 116          $translation->translation_set = $set;
 117          return $translation;
 118      }
 119  }
 120  
 121  class GP_UnitTest_Factory_For_Locale extends GP_UnitTest_Factory_For_Thing {
 122  	function __construct( $factory = null, $thing = null ) {
 123          $thing = (object)array( 'field_names' => array_keys( get_object_vars( $thing? $thing : new GP_Locale ) ) );
 124          parent::__construct( $factory, $thing );
 125          $this->default_generation_definitions = array(
 126              'slug' => new GP_UnitTest_Generator_Locale_Name,
 127              'english_name' => new GP_UnitTest_Generator_Sequence( 'Locale %s' ),
 128              'lang_code_iso_639_1' => 'en',
 129              'country_code' => 'US',
 130          );
 131      }
 132  
 133  	function create( $args = array(), $generation_definitions = null ) {
 134          if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions;
 135          $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
 136          $created = new GP_Locale( $generated_args );
 137          if ( $callbacks ) {
 138              $updated_fields = $this->apply_callbacks( $callbacks, $created );
 139              $created = new GP_Locale( $updated_fields );
 140          }
 141          $locales = &GP_Locales::instance();
 142          $locales->locales[$created->slug] = $created;
 143          return $created;
 144      }
 145  }
 146  
 147  class GP_UnitTest_Factory_For_Glossary extends GP_UnitTest_Factory_For_Thing {
 148  	function __construct( $factory = null, $thing = null ) {
 149          parent::__construct( $factory, $thing? $thing : new GP_Glossary );
 150  
 151          $this->default_generation_definitions = array(
 152              'description'        => new GP_UnitTest_Generator_Sequence( 'Glossary Set %s' ),
 153              'translation_set_id' => 1,
 154          );
 155      }
 156  
 157  	function create_with_project_set_and_locale( $args = array(), $project_args = array() ) {
 158          $locale       = $this->factory->locale->create();
 159          $project      = $this->factory->project->create( $project_args );
 160          $set          = $this->factory->set->create( array( 'project_id' => $project->id, 'locale' => $locale->slug ) + $args );
 161          $set->project = $project;
 162          $set->locale  = $locale->slug;
 163          $glossary     = $this->create( array('translation_set_id' => $set->id ) );
 164  
 165          return $glossary;
 166      }
 167  }
 168  
 169  
 170  class GP_UnitTest_Factory_For_Thing {
 171  
 172      var $default_generation_definitions;
 173      var $thing;
 174      var $factory;
 175  
 176      /**
 177       * Creates a new factory, which will create objects of a specific Thing
 178       *
 179       * @param object $factory GLobal factory that can be used to create other objects on the system
 180       * @param object $thing Instance of a GP_Thing subclass. This factory will create objects of this type
 181       * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
 182       * can be generators -- an object with next() method. There are some default generators: {@link GP_UnitTest_Generator_Sequence},
 183       * {@link GP_UnitTest_Generator_Locale_Name}, {@link GP_UnitTest_Factory_Callback_After_Create}.
 184       */
 185  	function __construct( $factory, $thing, $default_generation_definitions = array() ) {
 186          $this->factory = $factory;
 187          $this->default_generation_definitions = $default_generation_definitions;
 188          $this->thing = $thing;
 189      }
 190  
 191  	function create( $args = array(), $generation_definitions = null ) {
 192          if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions;
 193          $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
 194          $created = $this->thing->create( $generated_args );
 195          if ( !$created || is_wp_error( $created ) ) return $created;
 196          if ( $callbacks ) {
 197              $updated_fields = $this->apply_callbacks( $callbacks, $created );
 198              $save_result = $created->save( $updated_fields );
 199              if ( !$save_result || is_wp_error( $save_result ) ) return $save_result;
 200          }
 201          return $created;
 202      }
 203  
 204  	function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
 205          $callbacks = array();
 206          if ( is_null( $generation_definitions ) ) $generation_definitions = $this->default_generation_definitions;
 207          foreach( $this->thing->field_names as $field_name ) {
 208              if ( !isset( $args[$field_name] ) ) {
 209                  if ( !isset( $generation_definitions[$field_name] ) ) {
 210                      continue;
 211                  }
 212                  $generator = $generation_definitions[$field_name];
 213                  if ( is_scalar( $generator ) )
 214                      $args[$field_name] = $generator;
 215                  elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
 216                      $callbacks[$field_name] = $generator;
 217                  } elseif ( is_object( $generator ) )
 218                      $args[$field_name] = $generator->next();
 219                  else
 220                      return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
 221  
 222              }
 223          }
 224          return $args;
 225      }
 226  
 227  	function apply_callbacks( $callbacks, $created ) {
 228          $updated_fields = array();
 229          foreach( $callbacks as $field_name => $generator ) {
 230              $updated_fields[$field_name] = $generator->call( $created );
 231          }
 232          return $updated_fields;
 233      }
 234  
 235  	public static function callback( $function ) {
 236          return new GP_UnitTest_Factory_Callback_After_Create( $function );
 237      }
 238  }
 239  
 240  class GP_UnitTest_Generator_Sequence {
 241      var $next;
 242      var $template_string;
 243  
 244  	function __construct( $template_string = '%s', $start = 1 ) {
 245          $this->next = $start;
 246          $this->template_string = $template_string;
 247      }
 248  
 249  	function next() {
 250          $generated = sprintf( $this->template_string , $this->next );
 251          $this->next++;
 252          return $generated;
 253      }
 254  }
 255  
 256  class GP_UnitTest_Generator_Locale_Name extends GP_UnitTest_Generator_Sequence {
 257  	function __construct() {
 258          parent::__construct( '%s', 'aa' );
 259      }
 260  }
 261  
 262  class GP_UnitTest_Factory_Callback_After_Create {
 263      private $callback;
 264  
 265  	public function __construct( callable $callback ) {
 266          $this->callback = $callback;
 267      }
 268  
 269  	public function call( $object ) {
 270          return ($this->callback)( $object );
 271      }
 272  }


Generated: Sat Nov 23 01:01:06 2024 Cross-referenced by PHPXref 0.7.1