Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/27/2024 04:30:23 PM (2 years ago)
Author:
espellcaste
Message:

Unit Tests: Conducted a thorough review of existing unit tests files and improved them to ensure robustness.

Props imath.

Closes https://github.com/buddypress/buddypress/pull/295
Fixes #9081

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/factory.php

    r13874 r13980  
    22#[AllowDynamicProperties]
    33class BP_UnitTest_Factory extends WP_UnitTest_Factory {
    4         public $activity = null;
    5 
    6         function __construct() {
     4        public function __construct() {
    75                parent::__construct();
    86
    9                 $this->user = new BP_UnitTest_Factory_For_User( $this );
    10                 $this->activity = new BP_UnitTest_Factory_For_Activity( $this );
    11                 $this->group = new BP_UnitTest_Factory_For_Group( $this );
    12                 $this->message = new BP_UnitTest_Factory_For_Message( $this );
     7                $this->user           = new BP_UnitTest_Factory_For_User( $this );
     8                $this->activity       = new BP_UnitTest_Factory_For_Activity( $this );
     9                $this->group          = new BP_UnitTest_Factory_For_Group( $this );
     10                $this->message        = new BP_UnitTest_Factory_For_Message( $this );
    1311                $this->xprofile_group = new BP_UnitTest_Factory_For_XProfileGroup( $this );
    1412                $this->xprofile_field = new BP_UnitTest_Factory_For_XProfileField( $this );
    15                 $this->notification = new BP_UnitTest_Factory_For_Notification( $this );
    16                 $this->signup = new BP_UnitTest_Factory_For_Signup( $this );
    17                 $this->friendship = new BP_UnitTest_Factory_For_Friendship( $this );
     13                $this->notification   = new BP_UnitTest_Factory_For_Notification( $this );
     14                $this->signup         = new BP_UnitTest_Factory_For_Signup( $this );
     15                $this->friendship     = new BP_UnitTest_Factory_For_Friendship( $this );
    1816        }
    1917}
     
    5250class BP_UnitTest_Factory_For_Activity extends WP_UnitTest_Factory_For_Thing {
    5351
    54         function __construct( $factory = null ) {
     52        public function __construct( $factory = null ) {
    5553                parent::__construct( $factory );
    5654
     
    6462        }
    6563
    66         function create_object( $args ) {
    67                 if ( ! isset( $args['user_id'] ) )
     64        public function create_object( $args ) {
     65                if ( ! isset( $args['user_id'] ) ) {
    6866                        $args['user_id'] = get_current_user_id();
     67                }
    6968
    7069                return bp_activity_add( $args );
    7170        }
    7271
    73         function update_object( $activity_id, $fields ) {
     72        public function update_object( $activity_id, $fields ) {
    7473                $activity = new BP_Activity_Activity( $activity_id );
    7574
    7675                foreach ( $fields as $field_name => $value ) {
    77                         if ( isset( $activity->$field_name ) )
     76                        if ( isset( $activity->$field_name ) ) {
    7877                                $activity->$field_name = $value;
     78                        }
    7979                }
    8080
    8181                $activity->save();
     82
    8283                return $activity;
    8384        }
    8485
    85         function get_object_by_id( $user_id ) {
     86        public function get_object_by_id( $user_id ) {
    8687                return new BP_Activity_Activity( $user_id );
    8788        }
     
    9091class BP_UnitTest_Factory_For_Group extends WP_UnitTest_Factory_For_Thing {
    9192
    92         function __construct( $factory = null ) {
     93        public function __construct( $factory = null ) {
    9394                parent::__construct( $factory );
    9495
     
    104105        }
    105106
    106         function create_object( $args ) {
     107        public function create_object( $args ) {
    107108                if ( ! isset( $args['creator_id'] ) ) {
    108109                        if ( is_user_logged_in() ) {
     
    112113                        } else {
    113114                                $last_activity      = date( 'Y-m-d H:i:s', strtotime( bp_core_current_time() ) - 60 * 60 * 24 * 365 );
    114                                 $user_factory       = new WP_UnitTest_Factory_For_User();
    115115                                $args['creator_id'] = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    116116
     
    138138        }
    139139
    140         function update_object( $group_id, $fields ) {
     140        public function update_object( $group_id, $fields ) {
    141141                $group = new BP_Groups_Group( $group_id );
    142142
    143143                foreach ( $fields as $field_name => $value ) {
    144                         if ( isset( $group->field_name ) )
     144                        if ( isset( $group->field_name ) ) {
    145145                                $group->field_name = $value;
     146                        }
    146147                }
    147148
    148149                $group->save();
     150
    149151                return $group;
    150152        }
    151153
    152         function get_object_by_id( $group_id ) {
     154        public function get_object_by_id( $group_id ) {
    153155                return new BP_Groups_Group( $group_id );
    154156        }
     
    157159class BP_UnitTest_Factory_For_Message extends WP_UnitTest_Factory_For_Thing {
    158160
    159         function __construct( $factory = null ) {
     161        public function __construct( $factory = null ) {
    160162                parent::__construct( $factory );
    161163
     
    169171        }
    170172
    171         function create_object( $args ) {
     173        public function create_object( $args ) {
    172174                if ( empty( $args['sender_id'] ) ) {
    173175                        $args['sender_id'] = $this->factory->user->create();
     
    185187        }
    186188
    187         function update_object( $message_id, $fields ) {
    188                 // todo
    189         }
    190 
    191         function get_object_by_id( $message_id ) {
     189        public function update_object( $message_id, $fields ) {}
     190
     191        public function get_object_by_id( $message_id ) {
    192192                return new BP_Messages_Message( $message_id );
    193193        }
     
    196196class BP_UnitTest_Factory_For_XProfileGroup extends WP_UnitTest_Factory_For_Thing {
    197197
    198         function __construct( $factory = null ) {
     198        public function __construct( $factory = null ) {
    199199                parent::__construct( $factory );
    200200
     
    206206        }
    207207
    208         function create_object( $args ) {
    209                 $group_id = xprofile_insert_field_group( $args );
    210                 return $group_id;
    211         }
    212 
    213         function update_object( $group_id, $fields ) {
    214         }
    215 
    216         function get_object_by_id( $group_id ) {
     208        public function create_object( $args ) {
     209                return xprofile_insert_field_group( $args );
     210        }
     211
     212        public function update_object( $group_id, $fields ) {}
     213
     214        public function get_object_by_id( $group_id ) {
    217215                return new BP_XProfile_Group( $group_id );
    218216        }
     
    221219class BP_UnitTest_Factory_For_XProfileField extends WP_UnitTest_Factory_For_Thing {
    222220
    223         function __construct( $factory = null ) {
     221        public function __construct( $factory = null ) {
    224222                parent::__construct( $factory );
    225223
     
    231229        }
    232230
    233         function create_object( $args ) {
    234                 $field_id = xprofile_insert_field( $args );
    235                 return $field_id;
    236         }
    237 
    238         function update_object( $field_id, $fields ) {
    239         }
    240 
    241         function get_object_by_id( $field_id ) {
     231        public function create_object( $args ) {
     232                return xprofile_insert_field( $args );
     233        }
     234
     235        public function update_object( $field_id, $fields ) {}
     236
     237        public function get_object_by_id( $field_id ) {
    242238                return new BP_XProfile_Field( $field_id );
    243239        }
     
    245241
    246242class BP_UnitTest_Factory_For_Notification extends WP_UnitTest_Factory_For_Thing {
    247         public function __construct( $factory = null ) {
    248                 parent::__construct( $factory );
    249         }
    250 
    251243        public function create_object( $args ) {
    252244                return bp_notifications_add_notification( $args );
     
    261253
    262254class BP_UnitTest_Factory_For_Signup extends WP_UnitTest_Factory_For_Thing {
    263         public function __construct( $factory = null ) {
    264                 parent::__construct( $factory );
    265         }
    266 
    267255        public function create_object( $args ) {
    268256                return BP_Signup::add( $args );
     
    282270 */
    283271class BP_UnitTest_Factory_For_Friendship extends WP_UnitTest_Factory_For_Thing {
    284         /**
    285          * Constructor.
    286          *
    287          * @since 2.7.0
    288          *
    289          * @param $factory WP_UnitTest_Factory
    290          */
    291         public function __construct( $factory = null ) {
    292                 parent::__construct( $factory );
    293         }
    294 
    295         /**
    296          * Create friendship object.
    297          *
    298          * @since 2.7.0
    299          *
    300          * @param array $args Array of arguments.
    301          * @return int Friendship ID.
    302          */
     272
    303273        public function create_object( $args ) {
    304274                $friendship = new BP_Friends_Friendship();
     
    323293        }
    324294
    325         /**
    326          * Update a friendship object.
    327          *
    328          * @since 2.7.0
    329          *
    330          * @todo Implement.
    331          *
    332          * @param int   $id     ID of the friendship.
    333          * @param array $fields Fields to update.
    334          */
    335295        public function update_object( $id, $fields ) {}
    336296
    337         /**
    338          * Get a friendship object by its ID.
    339          *
    340          * @since 2.7.0
    341          *
    342          * @param int $id
    343          * @return BP_Friends_Friendship
    344          */
    345297        public function get_object_by_id( $id ) {
    346298                return new BP_Friends_Friendship( $id );
Note: See TracChangeset for help on using the changeset viewer.