Skip to:
Content

BuddyPress.org

Changeset 11063


Ignore:
Timestamp:
09/06/2016 03:31:21 AM (10 years ago)
Author:
boonebgorges
Message:

Tests: Add Friendship factory.

Props jdgrimes.
Fixes #7243.

Location:
trunk/tests/phpunit
Files:
2 edited

Legend:

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

    r11042 r11063  
    1414                $this->notification = new BP_UnitTest_Factory_For_Notification( $this );
    1515                $this->signup = new BP_UnitTest_Factory_For_Signup( $this );
     16                $this->friendship = new BP_UnitTest_Factory_For_Friendship( $this );
    1617        }
    1718}
     
    269270        }
    270271}
     272
     273/**
     274 * Factory for friendships.
     275 *
     276 * @since 2.7.0
     277 */
     278class BP_UnitTest_Factory_For_Friendship extends WP_UnitTest_Factory_For_Thing {
     279        /**
     280         * Constructor.
     281         *
     282         * @since 2.7.0
     283         *
     284         * @param $factory WP_UnitTest_Factory
     285         */
     286        public function __construct( $factory = null ) {
     287                parent::__construct( $factory );
     288        }
     289
     290        /**
     291         * Create friendship object.
     292         *
     293         * @since 2.7.0
     294         *
     295         * @param array $args Array of arguments.
     296         * @return int Friendship ID.
     297         */
     298        public function create_object( $args ) {
     299                $friendship = new BP_Friends_Friendship();
     300
     301                foreach ( array( 'initiator_user_id', 'friend_user_id' ) as $arg ) {
     302                        if ( isset( $args[ $arg ] ) ) {
     303                                $friendship->$arg = $args[ $arg ];
     304                        } else {
     305                                $friendship->$arg = $this->factory->user->create();
     306                        }
     307                }
     308
     309                foreach ( array( 'is_confirmed', 'is_limited', 'date_created' ) as $arg ) {
     310                        if ( isset( $args[ $arg ] ) ) {
     311                                $friendship->$arg = $args[ $arg ];
     312                        }
     313                }
     314
     315                $friendship->save();
     316
     317                return $friendship->id;
     318        }
     319
     320        /**
     321         * Update a friendship object.
     322         *
     323         * @since 2.7.0
     324         *
     325         * @todo Implement.
     326         *
     327         * @param int   $id     ID of the friendship.
     328         * @param array $fields Fields to update.
     329         */
     330        public function update_object( $id, $fields ) {}
     331
     332        /**
     333         * Get a friendship object by its ID.
     334         *
     335         * @since 2.7.0
     336         *
     337         * @param int $id
     338         * @return BP_Friends_Friendship
     339         */
     340        public function get_object_by_id( $id ) {
     341                return new BP_Friends_Friendship( $id );
     342        }
     343}
  • trunk/tests/phpunit/testcases/testsuite/factory.php

    r11042 r11063  
    2323                $this->assertNotEmpty( $m->get_recipients() );
    2424        }
     25
     26        /**
     27         * @ticket BP7243
     28         */
     29        public function test_friendship_should_create_default_initiator_and_friend() {
     30                $f = $this->factory->friendship->create_and_get();
     31
     32                $u1 = new WP_User( $f->initiator_user_id );
     33                $u2 = new WP_User( $f->friend_user_id );
     34
     35                $this->assertTrue( $u1->exists() );
     36                $this->assertTrue( $u2->exists() );
     37        }
     38
     39        /**
     40         * @ticket BP7243
     41         */
     42        public function test_friendship_should_respect_passed_initiator_and_friend() {
     43                $u1 = $this->factory->user->create();
     44                $u2 = $this->factory->user->create();
     45
     46                $f = $this->factory->friendship->create_and_get( array(
     47                        'initiator_user_id' => $u1,
     48                        'friend_user_id' => $u2,
     49                ) );
     50
     51                $this->assertSame( $u1, $f->initiator_user_id );
     52                $this->assertSame( $u2, $f->friend_user_id );
     53        }
    2554}
Note: See TracChangeset for help on using the changeset viewer.